【问题标题】:I am unable to stop or kill consumer receive(0), please help me how to shutdown activemq consumer if the waiting time is zero millisec我无法停止或杀死消费者接收(0),如果等待时间为零毫秒,请帮助我如何关闭 activemq 消费者
【发布时间】:2023-03-12 22:12:02
【问题描述】:
private boolean isRunning=false;
          try {
 ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("username","password",ActiveMQConnection.DEFAULT_BROKER_URL);
        // Create a Connection
        Connection connection = connectionFactory.createConnection();
        connection.start();
        // Create a Session
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        // Create the destination
        Destination destination = session.createQueue("testQ");
        consumer = (ActiveMQMessageConsumer) session.createConsumer(destination);
       do {
            System.out.println("Waiting for message...");
            Message message = consumer.receive(0);
            if (message != null && message instanceof TextMessage) {
                TextMessage txtMsg = (TextMessage) message;
                System.out.println("Received: " + txtMsg.getText());
            }
            isRunning=true;
        } while (isRunning);

} catch (JMSException e) {
        System.out.println(e);
    } catch (Exception e) {
        System.out.println(e);
    }

【问题讨论】:

    标签: java activemq


    【解决方案1】:

    免责声明:我对activemq不熟悉,我只是调查并阅读了the documentation。对于这样的事情,您几乎总能在那里找到解决方案,因为这可能被认为是一个常见问题。

    据我所知,您正在同步等待收到消息,并且您已设置无超时。结果,发生这种情况的线程被永远阻塞(正如你所经历的那样)。

    为了取消ActiveMQMessageConsumer 对象,您需要调用close() 方法。但是,在您的情况下,如果您同步调用receive 是正确的,则您将必须实现某种多线程环境,其中第二个线程不会被阻塞,并且可以调用close() on消费者对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-01
      • 1970-01-01
      • 2022-07-03
      • 1970-01-01
      • 1970-01-01
      • 2015-11-25
      相关资源
      最近更新 更多