我正在使用 activemq ,下面提到的代码可以正常工作,但我想像在Spring Framework中那样自动运行下面的代码,就像我们使用@JmsListener一样,这样会自动调用使用者类,因此是否有任何方法或注释在jersey中,这将允许我的消费者类在后台运行并获取activeMQ队列中的数据。我是jersey的新手。
制作人课程
public class Producer {
private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;
private static String subject = "QUEUETEST";
public static void main(String[] args) throws JMSException {
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
Connection connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue(subject);
MessageProducer producer = session.createProducer(destination);
TextMessage message = session.createTextMessage("Hello welcome come to QUEUETEST ActiveMQ!");
producer.send(message);
System.out.println("Sentage '" + message.getText() + "'");
connection.close();
}
}
消费类
public class Consumer {
private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;
// default broker URL is : tcp://localhost:61616"
private static String subject = "QUEUETEST";
public static void main(String[] args) throws JMSException {
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
Connection connection = connectionFactory.createConnection();
connection.start();
Session session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue(subject);
MessageConsumer consumer = session.createConsumer(destination);
Message message = consumer.receive();
if (message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
System.out.println("Receivedage '" + textMessage.getText()
+ "'");
}
connection.close();
}
}