Tuesday, June 8, 2010

Send Text Message To JMS Queue - Spring

Since I am working on Spring these days, I would post more on Spring as I have realised that Spring is the most comprehansive and more developer friendly framework among all the available frameworks in Java programming language.

Below is a class (the full code infact) that is useful to publish a text mesage into a JMS queue. Note that it use the Spring JmsTemplate. The configuration of which is given after the code snippet.


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

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

/**
* Class to send JMS through spring jms template
*
* @author Darel
*
*/
public class JMSSender {
private JmsTemplate jmsTemplate;

public JmsTemplate getJmsTemplate() {
return jmsTemplate;
}

public void setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}

/**
* send JMS to JMS server
*
* @param queueName
* JMS queue name
* @param msg
* Queue object to be sent
*/

public void sendMesage(String queueName, final String msg) {
jmsTemplate.send(queueName, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {

return session.createTextMessage(msg);

}
});
}

}


Since this now a JMS client, we need the configuration in the application context file of Spring. The configuration is as below.


<!-- ************* JMS Client configuration ************************* -->
<bean id="jmsSender" class="com.mycompany.proj.service.util.JMSSender">
<property name="jmsTemplate">
<ref bean="queueTemplate" />
</property>
</bean>
<bean id="queueTemplate"
class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory">
<ref bean="queueConnectionFactory" />
</property>
<property name="destinationResolver">
<ref bean="jmsDestinationResolver" />
</property>


</bean>
<bean id="queueConnectionFactory"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate">
<ref bean="permissionJndiTemplate" />
</property>
<property name="jndiName">
<value>${jms.connectionFactory}</value>
</property>
</bean>
<bean id="jmsDestinationResolver"
class="org.springframework.jms.support.destination.JndiDestinationResolver">
<property name="jndiTemplate">
<ref bean="permissionJndiTemplate" />
</property>
<property name="cache">
<value>true</value>
</property>
</bean>
<bean id="permissionJndiTemplate"
class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">
weblogic.jndi.WLInitialContextFactory
</prop>
<prop key="java.naming.provider.url">
${jms.provider.url}
</prop>
<prop key="java.naming.security.authentication">
${jms.authentication.type}
</prop>
<prop key="java.naming.security.principal">
${jms.security.principal}
</prop>
<prop key="java.naming.security.credentials">
${jms.security.credentials}
</prop>
</props>
</property>
</bean>


The values have $(...) which means that the value is got from the properties file. You can also hardcode the values if you do not have a properties file.

The above should be enough to publish a text message to the JMS queue. If you would need to publish a object. You can use the createObjectMessage method instead of createTextMessage.

You may also publidh a SOAP message into the JMS queue using this client. You will have to write the soap message object to a out stream and convert that to a String. The example is shown below.

//Here I am assuming that you have a method to create a SOAP message
SOAPMessage soapMessage = getSoapMessage();

ByteArrayOutputStream out = new ByteArrayOutputStream();

soapMessage.writeTo(out);

jmsSender.sendMesage(queueName, out.toString());

2 comments:


  1. This blog explains the details about changing the ways of doing that business. That is understand well and doing some different process. Provides he best output of others. Thanks for this blog.

    Text Message Marketing

    ReplyDelete
  2. I am expecting more interesting topics from you. And this was nice content and definitely it will be useful for many people.
    Digital Marketing SMS
    Digital Marketing Text
    Digital Mobile Marketing
    Text Whitelabel Solutions
    sms Whitelabel Solutions

    ReplyDelete