我正在使用带有 glassfish3 服务器的 NetBeans IDE 和 Java EE 编写程序。目前我有 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);
}
}
...
}