Java 我无法停止或终止使用者接收(0),如果等待时间为零毫秒,请帮助我如何关闭activemq使用者
免责声明:我不熟悉activemq,我只是调查并通读了一遍。对于这样的问题,您几乎总能在那里找到解决方案,因为这可能被认为是一个常见问题 据我所知,您正在同步地等待消息被接收,并且您已经设置了无超时。因此,发生这种情况的线程将永远被阻塞(正如您所经历的那样) 要取消Java 我无法停止或终止使用者接收(0),如果等待时间为零毫秒,请帮助我如何关闭activemq使用者,java,activemq,Java,Activemq,免责声明:我不熟悉activemq,我只是调查并通读了一遍。对于这样的问题,您几乎总能在那里找到解决方案,因为这可能被认为是一个常见问题 据我所知,您正在同步地等待消息被接收,并且您已经设置了无超时。因此,发生这种情况的线程将永远被阻塞(正如您所经历的那样) 要取消ActiveMQMessageConsumer对象,需要调用该方法。但是,在您的情况下,如果我没有记错您正在同步调用receive,那么您将必须实现某种多线程环境,在这种环境中,第二个线程没有被阻塞,并且可以对使用者对象调用close
ActiveMQMessageConsumer
对象,需要调用该方法。但是,在您的情况下,如果我没有记错您正在同步调用receive
,那么您将必须实现某种多线程环境,在这种环境中,第二个线程没有被阻塞,并且可以对使用者对象调用close()
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);
}