我是JMS的新手,並通過Active MQ Hello world的示例。說我有一個場景,每當我做入門 在DB員工的桌子底下,我必須把消息queue.here是從Hello World示例現在在JMS隊列上發佈消息?
public static class HelloWorldProducer {
public void createMessageOnQueue() {
try {
// Create a ConnectionFactory
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost");
// Create a Connection
Connection connection = connectionFactory.createConnection();
connection.start();
// Create a Session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// Create the destination (Topic or Queue)
Destination destination = session.createQueue("TEST.FOO");
// Create a MessageProducer from the Session to the Topic or Queue
MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
// Create a messages
String text = "Hello world! From: " + Thread.currentThread().getName() + " : " + this.hashCode();
TextMessage message = session.createTextMessage(text);
// Tell the producer to send the message
System.out.println("Sent message: "+ message.hashCode() + " : " + Thread.currentThread().getName());
producer.send(message);
// Clean up
session.close();
connection.close();
}
catch (Exception e) {
System.out.println("Caught: " + e);
e.printStackTrace();
}
}
}
我的問題生產者代碼片段是,如果我關閉連接和會話,它會關閉隊列嗎?如果是的話,如果消息還沒有被消費,會發生什麼?
第二個問題是,如果我需要第二次在同一隊列(即「TEST.FOO」)上發佈消息,是否需要第二次調用createMessageOnQueue
方法。如果是,是否不會創建新的隊列session.createQueue("TEST.FOO")?