我有两种类型的作业(紧急作业和普通作业)要发送进行处理。只有一个处理引擎实例,并且一次只能处理一个作业。现在的要求是,应先发送4个紧急作业进行处理,然后再发送2个正常作业。
我用两个队列(N.Q和E.Q)和一个生产者发送作业完成了poc。
public void receive() throws JsonParseException, JsonMappingException, IOException {
try {
Connection connectionEmergency = factory.createConnection();
connectionEmergency.start();
Session sessionEmergency = connectionEmergency.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destinationEmergency = sessionEmergency.createQueue("EMERGENCYQUEUE");
MessageConsumer consumerEmergency = sessionEmergency.createConsumer(destinationEmergency);
Connection connectionNormal = factory.createConnection();
connectionNormal.start();
Session sessionNormal = connectionNormal.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destinationNormal = sessionNormal.createQueue("NORMALQUEUE");
MessageConsumer consumerNormal = sessionNormal.createConsumer(destinationNormal);
int countNormal = 0;
int countEmergency = 0;
while (true) {
while (countEmergency < 4) {
TextMessage message = (TextMessage) consumerEmergency.receive(1000);
if (message != null) {
sendForDeployment(message.getText(), (countEmergency + 1));
} else {
System.out.println("************Message is null in Emergency**********");
break;
}
countEmergency++;
}
countEmergency = 0;
while (countNormal < 2) {
TextMessage message2 = (TextMessage) consumerNormal.receive(1000);
if (message2 != null) {
sendForDeployment(message2.getText(), (countNormal + 1));
} else {
System.out.println("*************Message is null in Normal***********");
break;
}
countNormal++;
}
countNormal = 0;
}
} catch (JMSException e) {
e.printStackTrace();
}
}
此代码的问题是,当两个队列都为空时,它正在连续运行:(。我想等待队列为空时,并在任何队列收到消息时以某种方式重新触发。请帮助获得一些更好的信息解决方案。