20 September, 2010

How create message queue using Spring 2.5.x and Oracle Weblogic Server 11g

Last week I spent some time on configuring JMS Message Queue using Spring 2.5.4 and Oracle Weblogic Server 11g R2.

Following are the step by step instructions to create queue.

1) Log into http://localhost:7001/console, supply weblogic/weblogic (default)
2) Create JMS Server using Oracle Weblogic Server. I used JDBC persistent store - MySQL based, to store the messages under db.
3) Name your jms queue and factory and let these refer to your newly created jms server which points to JDBC persistent store.

4) Now configure your spring jms file; which looks something like this:
































5) Create message listener class; which is influenced by traditional mdbs (J2EE 1.3 era..)

package amp;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

public class MyQueueListener implements MessageListener
{

public void onMessage(Message message)
{
if (message instanceof TextMessage)
{
try
{
String txtMessage = ((TextMessage) message).getText();
System.out.println("" + txtMessage + "");
}
catch (JMSException ex)
{
throw new RuntimeException(ex);
}
}
else
{
throw new IllegalArgumentException(
"Message must be of type TextMessage");
}
}
}

7) Create client class whcih produces huge message payloads; just to test

package amp;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.springframework.context.ApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

public class QueueClient
{
private ApplicationContext ctx = AppContext.getApplicationContext();
private JmsTemplate jt = (JmsTemplate) ctx.getBean("jmsTemplate");
private Queue q = (Queue) ctx.getBean("jdbcQ");

public void sendMessage(final String m) throws Exception
{
jt.send(q, new MessageCreator()// inner class
{
public Message createMessage(Session session)
throws JMSException
{
TextMessage tm = session.createTextMessage();
tm.setText(m);
return tm;
}
});
//System.out.println("message sent to destination called "+q.getQueueName());
}
}

8) Finally call this class under web environment; to see the message output. I used OEPE pack to conduct these tests.

Hope this helps to configure message queues.

No comments:

One new learning / day - however small it is

Read a blog / or article Watch TED talk  Read a small self-help book (many free eBooks available with less than 100 pages/can be completed i...