我正在使用NetBeans IDE和Java EE与glassfish3服务器编写程序。目前我有2个线程,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
这是我的代码:
主题A:
//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);
}
}
...
}
答案 0 :(得分:0)
您的线程A将密钥作为“报告”发送,并且您在线程B中期望为“m1”,因此它不会将回复发送回队列。在发送邮件之前,请尝试在A中创建使用者。
答案 1 :(得分:0)
结果我需要在为线程B执行producer.send(message)之后添加session.commit()。