[英]unable to receive messages from queue in jms
我是jms的初學者,我只是想讓一個運行簡單的Web應用程序運行,該應用程序可以生成和使用消息。 所以在我的歡迎頁面上,我有一個帶有文本框和提交按鈕的表單。 在提交時,文本框中的文本將包裝在一條消息中,並通過MessageProducer發送到服務器上的隊列。 據我了解,此消息應保留在隊列中,直到我從某個消費者連接到該隊列的MessageConsumer調用receive方法。 不幸的是,在第二個jsp頁面上,我單擊“獲取消息”按鈕,收到的消息變為null。 我很確定他們被正確發送了(至少沒有錯誤的說法)有沒有辦法我可以檢查一下? 關於出什么問題有什么想法嗎? 提前致謝。 生產者/消費者代碼。
public class Producer {
private static final String CONNECTION_FACTORY = "connectionFactory";
private static final String CONNECTION_QUEUE = "jms/myqueue";
private static final String CONNECTION_TOPIC = "jms/mytopic";
public Producer(String message) {
ConnectionFactory connectionFactory = null;
Connection connection = null;
//Get the JNDI Context
try {
Context jndiContext = new InitialContext();
//Create the Connection Factory and Connection
connectionFactory = (ConnectionFactory) jndiContext.lookup(Producer.CONNECTION_FACTORY);
connection = connectionFactory.createConnection();
//Create the session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
publishToQueue(jndiContext, session, message);
session.commit();
} catch (NamingException e) {
} catch (JMSException e) {
}finally{
//close connection
}
}
private void publishToQueue(Context jndiContext, Session session, String message) throws NamingException, JMSException{
//Create Queue
Queue queue = (Queue) jndiContext.lookup(Producer.CONNECTION_QUEUE);
//Create Message Producer
MessageProducer producer = session.createProducer(queue);
//Send TextMessage
producer.setDeliveryMode(DeliveryMode.PERSISTENT);
TextMessage textMessage = session.createTextMessage();
textMessage.setText(message);
producer.send(textMessage);
producer.close();
}
}
public class Consumer {
private static final String CONNECTION_FACTORY = "connectionFactory";
private static final String CONNECTION_QUEUE = "jms/myqueue";
private static final String CONNECTION_TOPIC = "jms/mytopic";
private Message message;
public Consumer() {
ConnectionFactory connectionFactory = null;
Connection connection = null;
//Get the JNDI Context
try {
Context jndiContext = new InitialContext();
//Create the Connection Factory
connectionFactory = (ConnectionFactory) jndiContext.lookup(Consumer.CONNECTION_FACTORY);
connection = connectionFactory.createConnection();
//Create the session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//Call methods to retrieve message
message = getFromQueue(jndiContext, session);
session.commit();
if(message!=null){
System.out.println("Message Received");
}
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(connection != null){
connection.close();
}
} catch (JMSException e) {
e.printStackTrace();
}
}
}
private Message getFromQueue(Context jndiContext, Session session) throws NamingException, JMSException{
//Create new Queue
Queue queue = (Queue)jndiContext.lookup(Consumer.CONNECTION_QUEUE);
//Create Message Consumer
MessageConsumer consumer = session.createConsumer(queue);
return consumer.receive(10000);
}
public Message getMessage(){
return message;
}
}
抱歉,在另一個問題中找到了答案。MessageConsumer 不使用消息 ..我所需要做的就是在getFromQueue方法之前調用connection.start()
public Consumer() {
ConnectionFactory connectionFactory = null;
Connection connection = null;
//Get the JNDI Context
try {
Context jndiContext = new InitialContext();
//Create the Connection Factory
connectionFactory = (ConnectionFactory) jndiContext.lookup(Consumer.CONNECTION_FACTORY);
connection = connectionFactory.createConnection();
//Create the session
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//Call methods to retrieve message
--> connection.start(); <--
message = getFromQueue(jndiContext, session);
session.commit();
if(message!=null){
System.out.println("Message Received");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.