Wednesday, March 28, 2012

Spring JMS tutorial

This tutorial is on Weblogic server.
before running this tutorial it is expected that Connection factory and queue JNDI is created on weblogic server.



  • This is Message listener bean

package com.test;

import javax.jms.Message;
import javax.jms.MessageListener;

public class SpringMDP implements MessageListener{
public void onMessage(Message message) {
if (MapMessage.class.isInstance(message)) {
     MapMessage mapMessage = (MapMessage)(message);
   }
}
}


  • This is the configuration for Message listner bean


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <bean id="messageListener" class="com.test.SpringMDP" />

<bean id="connectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
    <value>connectionFactoryJNDI</value>
</property>
  <property name="jndiTemplate">
    <ref bean="jndiTemplate" />
</property> 
</bean>

<bean id="queue" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
  <value>queueJNDI</value>
</property>
<property name="jndiTemplate">
  <ref bean="jndiTemplate" />
</property>
</bean>

  <bean id="listenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer102">
    <property name="concurrentConsumers" value="5" />
   <property name="connectionFactory" ref="connectionFactory" />
    <property name="destination" ref="queue" />
   <property name="messageListener" ref="messageListener" />
</bean>
</beans>

  • This is a client Which publish the message

package com.test;

import java.util.Properties;
import javax.jms.DeliveryMode;
import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;
import org.apache.log4j.Logger;

public class JMSClient {
    protected final static Logger log = Logger.getLogger(JMSClient.class);
    public  void publishmessage() {
    QueueConnection connection =null; 
    QueueSession qsession = null;
    try {
    Context jndiContext = getInitialContext();
    QueueConnectionFactory connFactory = (QueueConnectionFactory)jndiContext.lookup("connectionFactoryJNDI");
    Queue targetQueue = (Queue)jndiContext.lookup("queueJNDI");
    connection = connFactory.createQueueConnection();
    qsession = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
    QueueSender sender = qsession.createSender(targetQueue);
    sender.setDeliveryMode(DeliveryMode.PERSISTENT);
    MapMessage message = qsession.createMapMessage();
    sender.send(message);
    }catch(Exception exp){
    log.error(exp.getMessage(), exp);

    }finally {
    if (qsession != null) {
    try{
    qsession.close();
    }catch(JMSException je){
    log.error(je.getMessage(), je);
    }
    }
    if (connection != null) {
    try {
    connection.close();
    }catch(JMSException je){
    log.error(je.getMessage(), je);
    }
    }
    }
    }
    public static InitialContext getInitialContext() throws javax.naming.NamingException {
        String url;
        url = "t3://localhost:7001";
        Properties h = new Properties();
        h.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
        h.put(Context.PROVIDER_URL, url);
        return( new InitialContext());
    }
}


No comments:

Post a Comment