PDA

View Full Version : JMS Template Example


redpill
Sep 8th, 2004, 04:01 PM
Has anyone seen an example of using the JmsTemplate?
Something real concrete for us simpletons?

timmorrow
Sep 8th, 2004, 08:45 PM
Have you read 14.4. Using the JmsTemplate (http://www.springframework.org/docs/reference/ch14.html#d0e8408)? It contains some examples, at least using JmsTemplate102.

Here is my example (when using a web container):

JmsTemplate jmsTemplate = new JmsTemplate();
jmsTemplate.setConnectionFactory(connectionFactory );
jmsTemplate.convertAndSend(destination, "Hello, world!");


where connectionFactory and destination are member variables initialzed from a context and configured as follows:

<bean id="connectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName"><value>jms/connectionFactory</value></property>
<property name="resourceRef"><value>true</value></property>
</bean>

<bean id="destination" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName"><value>jms/queue</value></property>
<property name="resourceRef"><value>true</value></property>
</bean>


assuming jms/connectionFactory and jms/queue are configured in web.xml.

If you're not using a web container, you can get the connection factory and destination from your JMS provider's JNDI tree.

Tim

redpill
Sep 9th, 2004, 12:45 PM
cool thanks!!

it's hard to believe it can be just that easy..

joegaber
Sep 12th, 2004, 05:31 PM
assuming jms/connectionFactory and jms/queue are configured in web.xml.

If you're not using a web container, you can get the connection factory and destination from your JMS provider's JNDI tree.

Tim[/quote]


Can you show me the way to configure the web.xml based on the above example.

wgunadi
Sep 13th, 2004, 05:33 PM
assuming jms/connectionFactory and jms/queue are configured in web.xml.

If you're not using a web container, you can get the connection factory and destination from your JMS provider's JNDI tree.

Tim


Can you show me the way to configure the web.xml based on the above example.

I second this request. One thing that is missing at least for some of us is a cohesive step-by-step example on how to create a web-application (hosted by Tomcat or Jetty) that hosts one or more Queues that remote clients can pump messages into.

I'm sure this kind of application is not unique to a few of us.

Mark Pollack
Sep 13th, 2004, 07:04 PM
Hi,

I'll add this to the docs. If anyone has already done this and wants to email me their code, it would save me a bit of time...

Cheers,
Mark

Mark Pollack
Sep 13th, 2004, 07:04 PM
Hi,

I'll add this to the docs. If anyone has already done this and wants to email me their code, it would save me a bit of time...

Cheers,
Mark

timmorrow
Sep 13th, 2004, 07:52 PM
I'm afraid my examples here probably aren't going to complete enough for documentation, but I'll post anyway.

As far as I'm aware, Tomcat doesn't have any support for JMS, meaning sending messages from a servlet in Tomcat is just like writing a standalone client:
* You have to use the client jar file provided by your JMS provider (e.g. openJMS, ActiveMQ, TIBCO etc.)
* You use your JMS providers admin tool to create a connection factory and queue (or perhaps they provide a default one)
* You use JNDI to lookup the connection factory and queue

So here is an example for JBoss 3.2.5 and Weblogic 8.1 for sending from a servlet.

web.xml
======
Add these entries:

<resource-env-ref>
<resource-env-ref-name>jms/queue</resource-env-ref-name>
<resource-env-ref-type>javax.jms.Queue</resource-env-ref-type>
</resource-env-ref>
<resource-ref>
<description>JMS Connection Factory</description>
<res-ref-name>jms/connectionFactory</res-ref-name>
<res-type>javax.jms.QueueConnectionFactory</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>


jboss-web.xml - For JBoss
==========
This file is located in your WEB-INF directory too. Add these entries:

<jboss-web>
<resource-env-ref>
<resource-env-ref-name>jms/queue</resource-env-ref-name>
<jndi-name>queue/testQueue</jndi-name>
</resource-env-ref>
<resource-ref>
<res-ref-name>jms/connectionFactory</res-ref-name>
<jndi-name>java:/ConnectionFactory</jndi-name>
</resource-ref>
</jboss-web>

Its been a while, and I don't use JBoss built-in JMS provider, but I think that "queue/testQueue" and "java:/ConnectionFactory" are the correct names of objects bound in JBoss's JNDI tree. Check its docs for info and substitute them with appropriate examples.

weblogic.xml - For Weblogic
=========
Add these entries:

<weblogic-web-app>
<reference-descriptor>
<resource-description>
<res-ref-name>jms/connectionFactory</res-ref-name>
<jndi-name>wlConnectionFactory</jndi-name>
</resource-description>
<resource-description>
<resource-env-description>
<res-env-ref-name>jms/queue</res-env-ref-name>
<jndi-name>wlQueue</jndi-name>
</resource-env-description>
</reference-descriptor>
</weblogic-web-app>

Again, check weblogic docs for precise JNDI names of connection factory and queue objects. You can, of course, create your own using Weblogic's excellent admin console.

Now that those items are defined in web.xml, they can be looked up from the "java:comp/env" context. So your bean context is configured as in the earlier example:

<bean id="connectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName"><value>jms/connectionFactory</value></property>
<property name="resourceRef"><value>true</value></property>
</bean>

<bean id="destination" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName"><value>jms/queue</value></property>
<property name="resourceRef"><value>true</value></property>
</bean>


I personally don't declare my queues in web.xml and the container's web xml file because they are determined at runtime. In that case, you can use Spring's JndiDestinationResolver and a JndiTemplate bean to specify how to lookup the queue from the container's JNDI tree directly (that is, specifying the appropriate initial context factory and URL).

Tim

timmorrow
Sep 13th, 2004, 07:55 PM
One important point: With JBoss 3.2.5 and Weblogic 8.1 you'll need to use JmsTemplate102 for sending.

You can use JMS 1.1. APIs with Weblogic if you put a JMS 1.1 JAR file in your server's classpath.

pinglu
Sep 15th, 2004, 04:06 PM
Why did the jndiname in web.xml and applicartioncontext.xml point to differently one is wlQueue another is jms/queue?

<resource-description>
<resource-env-description>
<res-env-ref-name>jms/queue</res-env-ref-name>
<jndi-name>wlQueue</jndi-name>
</resource-env-description>
</reference-descriptor>

<bean id="connectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName"><value>jms/connectionFactory</value></property>
<property name="resourceRef"><value>true</value></property>
</bean>

<bean id="destination" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName"><value>jms/queue</value></property>
<property name="resourceRef"><value>true</value></property>
</bean>

timmorrow
Sep 17th, 2004, 01:04 AM
The web.xml file defines the JNDI name available to your application (e.g. "jms/connectionFactory"), and thus to Spring's bean context.

The container-specific xml file (e.g. jboss-web.xml, weblogic.xml) maps that "logical" name to an actual JNDI name in the container's JNDI tree. As you can see this varies between containers.

This insulates your application from knowing exactly where the JNDI object is coming from. Declaring a resource reference in web.xml also allows the container to provide its own implementation of the object, to do useful stuff for you like connection pooling.

Tim