ActimeMQ具有两个队列(NormalJob和Emergenecy Job)的两个生产者(两个队列分别)的一个生产者

时间:2018-07-21 06:17:06

标签: java jms activemq

我有两种类型的作业(紧急作业和普通作业)要发送进行处理。只有一个处理引擎实例,并且一次只能处理一个作业。现在的要求是,应先发送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();
    } 
}

此代码的问题是,当两个队列都为空时,它正在连续运行:(。我想等待队列为空时,并在任何队列收到消息时以某种方式重新触发。请帮助获得一些更好的信息解决方案。

0 个答案:

没有答案