- Setup the JMS environment as described in the "Configuring Weblogic JMS" post
- Create the Messaging client: This is a simple Java class which uses the spring JmsTemplate to send a message to the queue. The JmsTemplate can be used for message production and synchronous message reception. For asynchronous reception, Spring provides a number of message listener containers that are used to create Message-Driven POJOs (MDPs).
public class QueueSender {
private JmsTemplate jmsTemplate;
public void setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}
public void sendMesage() {
jmsTemplate.send("jms/testQueue", new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage("Hello");
}
});
}
}QueueSender.java - Configure the Bean in the applicationContext.xml file: The following is alisting of the applicationContext.xml file.
<?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="jndiTemplate" 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">t3://localhost:20001</prop>
</props>
</property>
</bean>
<bean id="queueConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate">
<ref bean="jndiTemplate" />
</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="jndiTemplate" />
</property>
<property name="cache">
<value>true</value>
</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="jmsSender" class="jms.QueueSender">
<property name="jmsTemplate">
<ref bean="queueTemplate" />
</property>
</bean>
</beans>WEB-INF/applicationContext.xml
The JndiDestinationResolver class can be used to obtain the Queue destinations using the JNDI Name. The send method in JmsTemplate (see QueueSender), uses the JNDI name, which is used by the JndiDestinationResolver to obtain the appropriate destination. - Create a servlet to invoke the Message Sender: The following servlet is used to invoke the QueueSender:
public class QueueSenderServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
QueueSender sender = (QueueSender)ctx.getBean("jmsSender");
sender.sendMesage();
}
}QueueSenderServlet.java - Update the web.xml file to add the servlet and spring application context:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>SpringJMSClientWeb</display-name>
<servlet>
<description></description>
<display-name>QueueSenderServlet</display-name>
<servlet-name>QueueSenderServlet</servlet-name>
<servlet-class>jms.QueueSenderServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>QueueSenderServlet</servlet-name>
<url-pattern>/QueueSenderServlet</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>web.xml
The listener defined web.xml (ContextLoaderListener) by default loads the applicationContext.xml file in WEB-INF directory of the web application.
Tuesday, December 12, 2006
Implementing JMS with Spring: Messaging Client
Last week, I described how to implement JMS, using a stand-alone client and a Message Driven Bean. In this post and the next, I will describe how to implement JMS using Spring and Message Driven POJOs. This post will describe how to create a Messaging client using Spring. The next post will describe how to implement a Message driven POJO. For this I used a simple servlet that, when invoked will send a text message "hello", to a destination queue. The Message driven pojo, listening on the queue will then receive and print the message. Follow these steps to run the example
Subscribe to:
Post Comments (Atom)
Popular Posts
-
The previous post described how to implement a JMS messaging client using Spring JMS . This post will describe how to implement the Message ...
-
JUnit 4 introduces a completely different API to the older versions. JUnit 4 uses Java 5 annotations to describe tests instead of using in...
-
In the past, I had a few posts on how to implement pagination using displaytag( 1 , 2 ). That solution is feasible only with small result se...
-
This post will describe how to create and deploy a Java Web Application war to Heroku using Heroku CLI. You will need a basic understanding ...
-
New posts with iText 5.5.12 Following are two new posts for PDF Merge with iText 5.5.12 Merge PDF files using iText 5 Merge and Paginate PDF...
-
Recently I was attempting to deploy to weblogic from a Jenkins installed on a Red Hat Enterprise Linux Server release 7.3 , to a remote Webl...
-
WebLogic Server does not support or certify any particular LDAP servers. Any LDAP v2 or v3 compliant LDAP server should work with WebLogic ...
-
The example here demonstrates the use of an anonymous PL/SQL block to return data to a calling Java program. It also shows how to use nested...
-
The String class doesn't have the ability to compare text from a natural language perspective. Its equals and compareTo methods compa...
-
In this post we will see how to do an offline install Jenkins and required plugins on a Red Hat Enterprise Linux Server release 7.3. This is...
Thanks for the post it was a good example.
ReplyDeleteI have a question about throwing RuntimeException on the onMessage method.
If a Runtime exception occurs, shouldn't the message still be in the queue, and possibly resent until the onMessage can complete without exception?
When I throw an exception I lose the message, which isn't a good thing!
Ok, seems that I have to explicitly set the session transacted flag to true in the DefaultMessageListenContainer.
ReplyDeleteWith that flag set the message is resent until the onMessage method completes without exception.
Thanks again for the post.
Hi,
ReplyDeleteI tried the example and I tried to deploy it to tomcat. But during deployment I am getting this error although I have my weblogic 10 instance running on my PC.
I included all possible jar files to the classpath.
SEVERE: Context initialization failed
org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is java.lang.NoClassDefFoundError: weblogic/utils/NestedException
Caused by: java.lang.NoClassDefFoundError: weblogic/utils/NestedException
Hi,
ReplyDeleteI tried the example and I tried to deploy it to tomcat. But during deployment I am getting this error although I have my weblogic 10 instance running on my PC.
I included all possible jar files to the classpath.
SEVERE: Context initialization failed
org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is java.lang.NoClassDefFoundError: weblogic/utils/NestedException
Caused by: java.lang.NoClassDefFoundError: weblogic/utils/NestedException
Thanks for the example,
Mohan
Good job! Could you post the solution on how you would consume the message using a MDP?
ReplyDeleteHi!
ReplyDeleteWhat version of Spring was used in sample?
I'm having a NameNotFoundException in the jndiName from bean queueConnectionFactory.
Good Job!! will you please post the solution for Synchronous messaging with temporary queue and spring jms template.
ReplyDeleteThanks , it helped me .
ReplyDeleteI could use this with spring2.0. jboss messaging . I tried this from a POJO.
ReplyDeleteInfact in abhi's sample destination is resolved in run time . i had configured my destination directly in the config file, it worked .
Your article is good. Here is another good article on JMS and Spring Integration
ReplyDeleteInteresting Article
ReplyDeleteSpring online training Spring online training Spring Hibernate online training Spring Hibernate online training
spring training in chennai spring hibernate training in chennai
Very good article. Liked it. it is very helpful for me
ReplyDeleteThis post is really nice and informative. The explanation given is really comprehensive and informative.
ReplyDeleteoracle training
I must appreciate you for providing such a valuable content for us. This is one amazing piece of article. Helped a lot in increasing my knowledge.
ReplyDeleteoracle training in bangalore
sql server dba training in bangalore
web designing training in bangalore
digital marketing training in bangalore
java training in bangalore
Really nice way to present your blog and information is also too good. Thanks for sharing it. If you are searching for more courses than visit here:-
ReplyDeleteOracle Training | Online Course | Certification in chennai | Oracle Training | Online Course | Certification in bangalore | Oracle Training | Online Course | Certification in hyderabad | Oracle Training | Online Course | Certification in pune | Oracle Training | Online Course | Certification in coimbatore
Thanks for sharing such a helpful, and understandable blog. I really enjoyed reading it.
ReplyDeleteRobots for kids
Robotic Online Classes
Robotics School Projects
Programming Courses Malaysia
Coding courses
Coding Academy
coding robots for kids
Coding classes for kids
Coding For Kids
Hi, this is really amazing article so thanks for posting and sharing this article we are supporting training for all click here for further details please
ReplyDeletefull stack developer course near me , online internships for ece students , online internship for electrical engineering students , online internship for ece students , online internship for cse students , online internship for b.com students , internship for eee students , internship for cse 3rd year students , ethical hacking internship
A very interesting blog....
ReplyDeleteups computer full form
kb full form
love full form
rpm full form
ide full form in computer
dtp full form
d v d full form
bca full form and subjects
swat full form
micr full form in computer
Infycle Technology, No.1 Software Training Institute in Chennai, afford best Data Science training in Chennai and also provide technical courses like Oracle, Java, Big data, AWS, Python, DevOps, Digital Marketing, Selenium Testing, etc., and we also provide the best training from the best technical trainers and after completion of training students will be able to crack the jobs on top MNC’s. for more information call 7504633633.
ReplyDeleteThere are lots of information about latest technology and current project modules click here for latest project title with abstract and code execution MCA Project Topics , MCA Final Year Project , MCA Final Year Project Topics , MCA Mini Project Topics , cse mini projects , M.Sc Computer Science Project Topics , Mini Project Topics for MSc Computer Science , MSc Computer Science Project Topics in Php , MSc Computer Science Project Topics in Python , MSc Computer Science Project Topics in Java
ReplyDeleteDefinitely consider that that you stated. Your favorite justification seemed to be at the net the easiest factor to keep in mind of. 토토사이트
ReplyDeleteVery efficiently written information. It will be valuable to everyone who utilizes it, as well as yours truly. Keep doing what you are doing – for sure i will check out more posts.
ReplyDelete경마
온라인경마
Your article is very interesting. I think this article has a lot of information needed, looking forward to your new posts.
ReplyDelete스포츠토토
Whatsapp Number Call us Now! 01537587949
ReplyDeleteIt Training In Dhaka
USA pone web iphone repair USA
USA SEX WEB careful
bd sex video B tex
bd sex video sex video
bd sex video freelancing course
Impressive!Thanks for the post
ReplyDeleteBest Travel Agency in Madurai | Travels in Madurai
Madurai Travels | Best Travels in Madurai
Tours and Travels in Madurai | Best Tour Operators in Madurai
슬롯커뮤니티
ReplyDeleteGet your Career in IT on track with our Career Accelerator Program by Premium Learnings
ReplyDeleteCheck our Youtube channel for our training videos - https://www.youtube.com/c/PremiumLearningssystem
For more info do visit us: https://www.premiumlearnings.com/
Royalcasino935
ReplyDeleteThanks for such a great blog here. I was searching for something like this for quite a long time and at last, I’ve found it on your blog. It was definitely interesting for me to read about their market situation nowadays.
ReplyDeletebest c online course
online oracle
c course
cheap website design in chennai
big data training online