【问题标题】:Apache ActiveMQ fall back mechanismApache ActiveMQ 回退机制
【发布时间】:2012-07-31 05:30:33
【问题描述】:

我正在使用带有 Spring 的 Apache Active MQ ......我面临的问题是我在一台机器上创建生产者,比如说 Machine1,我正在第二台机器上创建一个消费者,比如说 Machine2...... 我通过调用简单的 servlet 调用在 Machine1 上创建生产者......然后在 Machine2 上创建消费者...... 我面临的问题是,无论如何,如果我的生产者无法在指定的时间内发送任何数据包,那么我想从 Machine2 中删除我的消费者和队列...... 无论如何,如果我在指定的时间内没有从生产者那里收到任何数据包,我可以设置我的消费者和队列来自动删除并执行一些业务逻辑....

connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,ActiveMQConnection.DEFAULT_PASSWORD,ConnectorURL);
connection = connectionFactory.createConnection();
connection.start();
session = connection.createSession(transacted, Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue(queueID+"");
connection = connectionFactory.createConnection();
connection.start();
consumer = session.createConsumer(destination);

基本上这段代码为我的应用程序创建消费者......然后我将此消费者分配给我的应用程序监听器,监听生产者是否向消费者发送任何消息......

ScenarioExecutionQueueListenerImpl executionQueueListener = new ScenarioExecutionQueueListenerImpl(scenario,result, host);
beanFactory.autowireBean(executionQueueListener);
connection.setExceptionListener(executionQueueListener);
Message message = consumer.receive();
consumer.setMessageListener(executionQueueListener);
executionQueueListener.setConsumer(consumer);
executionQueueListener.onMessage(message);

【问题讨论】:

    标签: java spring activemq


    【解决方案1】:

    我不会为这种情况设置消息监听器,而只是使用 consumer.receive() 方法。 MessageListener 更适合时间独立/异步消耗。

    public void run(){
      Message m = consumer.receive(timeout_value_in_millisec);
      if( m != null ){
         // got a message, handle it.
         processMessage(msg);
      }else{
         // no message received in specified time, 
      }
      // close session, connection etc.
    }
    
    public void processMessage(Message msg){
    
    }
    

    【讨论】:

    • 感谢您的快速回复...我会检查这个...:) 我想知道的另一件事是如何删除我为接收消息而创建的队列。 ...从消费者端或生产者端
    • 感谢您的快速回复...但在我的情况下,消息始终是异步且多条消息...我只想确保我的侦听器应该触发并回滚所有内容(如果没有)在指定的持续时间内接收任何消息......以及如何删除我创建的队列以接收来自生产者端或消费者端的消息......
    • ActiveMQ 中的队列不需要显式删除或创建。它们仅在使用时才存在。阅读:activemq.apache.org/how-do-i-create-new-destinations.html
    猜你喜欢
    • 2013-09-19
    • 1970-01-01
    • 2012-07-05
    • 2012-02-21
    • 2014-11-16
    • 1970-01-01
    • 2018-11-02
    • 2017-01-29
    • 2020-10-09
    相关资源
    最近更新 更多