i am a beginner to jms and i am just trying to get a simple web app running that produces and consumes messages. so on my welcome page i have a form with a text box and submit button. on submit the text in the text box is wrapped in a message and sent to the queue on the server via a MessageProducer. from what i understand, this message should remain on the queue until i call the receive method from some MessageConsumer where the consumer is linked to that queue. unfortunately on my second jsp page where i click the "Get Messages" button , the received message turns up null. i am pretty sure they are being sent properly (at least there are no errors saying otherwise) is there a way i could check that though? any ideas on whats going wrong? thanks in advance. heres the producer/consumer code.
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;
}
}