我正在使用NetBeans IDE和Java EE与glassfish3服务器编写程序。目前我有两个线程,A和b。它们都在彼此之间来回发送消息。我遇到的问题是:
1. A sends a message, M1, to B
2. B receives M1
3. B sends M2 to A
4. A hangs waiting on M2
下面是我的代码:
线程:
//required declarations
producer = session.createProducer(AtoBQueue);
m1 = session.createMapMessage();
m1.setString("m1", "m1Val");
m1.setJMSReplyTo(BtoAQueue);
flag = true;
producer.send(m1);
consumer = session.createConsumer(BtoAQueue);
connection.start();
if(flag){
m2 = (MapMessage) consumer.receive();
String m2Val = m2.getString("m2");
if(m2Val.equals("true")){
flag2 = true;
}
}
...
线程B:
consumer = session.createConsumer(AtoBQueue);
connection.start();
while(true){
m1 = (MapMessage) consumer.receive();
m1Val = m1.getString("m1");
if(m1Val != null){
flag = true;
}
MessageProducer producer = session.createProducer((Queue m1.getJMSReplyTo());
m2 = session.createMapMessage();
if(m1Val != null){
if(m1Val.equals("m1Val")){
m2.setString("m2", "m2Val");
m2.setJMSReplyTo(AtoBQueue);
producer.send(m2);
}
}
...
}
您的线程A将密钥发送为"报告",而您期望线程B为"m1",因此它不会将回复发送回队列。在发送消息之前,还可以尝试在A中创建Consumer。
事实证明,我需要在只为线程b执行producer.send(message)之后添加session.commit()。