等待在ActiveMQ中接收异步消息的更好方法

时间:2016-09-07 08:31:48

标签: java asynchronous jms activemq

我已经使用ActiveMQ发送消息并以异步方式接收它们。

在那里,我在确定等待消息的最佳方式时遇到了问题。 循环中的睡眠线程是一种选择。但感觉对我来说并不好看。

任何人都可以提出更好的方法。

AsyncReceiver.java

public class AsyncReceiver implements MessageListener, ExceptionListener{

    public static void main(String[] args) throws Exception{

        Properties env = new Properties();                                  
        env.put(Context.INITIAL_CONTEXT_FACTORY,"org.apache.activemq.jndi.ActiveMQInitialContextFactory");
        env.put(Context.PROVIDER_URL, "tcp://localhost:61616");
        env.put("queue.queueSampleQueue","MyNewQueue");

        InitialContext ctx = new InitialContext(env);
        Queue queue = (Queue) ctx.lookup("queueSampleQueue");
        QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx.lookup("QueueConnectionFactory");
        QueueConnection queueConn = connFactory.createQueueConnection();
        QueueSession queueSession = queueConn.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);

        QueueReceiver queueReceiver = queueSession.createReceiver(queue);
        AsyncReceiver asyncReceiver = new AsyncReceiver();
        queueReceiver.setMessageListener(asyncReceiver);
        queueConn.setExceptionListener(asyncReceiver);
        queueConn.start();

        // Waiting for messages
        System.out.print("waiting for messages");
        for (int i = 0; i < 10; i++) {
            Thread.sleep(1000);
        }

        queueConn.close();
    }

    public void onMessage(Message message){
        TextMessage msg = (TextMessage) message;
        try {
            System.out.println("received: " + msg.getText());
        } catch (JMSException ex) {
            ex.printStackTrace();
        }
    }

    public void onException(JMSException exception){
        System.err.println("an error occurred: " + exception);
    }
}

Sender.java

public class Sender{

    public static void main(String[] args) throws Exception{

        Properties env = new Properties();
        env.put(Context.INITIAL_CONTEXT_FACTORY,"org.apache.activemq.jndi.ActiveMQInitialContextFactory");
        env.put(Context.PROVIDER_URL, "tcp://localhost:61616");
        env.put("queue.queueSampleQueue", "MyNewQueue");

        InitialContext ctx = new InitialContext(env);
        Queue queue = (Queue) ctx.lookup("queueSampleQueue");
        QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx.lookup("QueueConnectionFactory");
        QueueConnection queueConn = connFactory.createQueueConnection();
        QueueSession queueSession = queueConn.createQueueSession(false,Session.DUPS_OK_ACKNOWLEDGE);

        QueueSender queueSender = queueSession.createSender(queue);
        queueSender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
        TextMessage message = queueSession.createTextMessage("Hello");
        queueSender.send(message);
        System.out.println("sent: " + message.getText());

        queueConn.close();
    }
}

1 个答案:

答案 0 :(得分:3)

在队列中处理/消耗按摩有两种方法。

  1. 定期检查队列中的新按摩 - 如果您定期运行程序,这是合适的。您可以通过实现具有一些线程休眠的循环来实现此目的。防爆。每天两次,每天一次等。

  2. 向队列注册使用者(使用MessageListener)。你可以这样做,如下例所示。

  3. <强> Consumer.java

            javax.jms.Connection connection = null;
            Session session = null;
            Destination destination = null;
            MessageConsumer consumer = null;
    
    
            ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(URL);
            connection = connectionFactory.createConnection();
            connection.start();
    
    
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            destination = session.createQueue(queueName);
    
    
            consumer = session.createConsumer(destination);
            consumer.setMessageListener(new YourClass());
    

    <强> YourClass.java

    public class YourClass implements MessageListener {
    @Override 
    public void onMessage(Message message) {
                    TextMessage textMessage = (TextMessage) message;
                    inputJsonString = textMessage.getText();
                   //do what ever you want with inputJsonString
                    message.acknowledge(); 
               }
    

    }