Announcement Announcement Module
Collapse
No announcement yet.
Tomcat, Spring and JMS Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Tomcat, Spring and JMS

    Hi All,

    I've got Spring (2.0.2) based web application running under Tomcat (4.1.31). I need to asynchronously listen (receive/consume) to the JMS messages from remote server. I do not need to send any messages, only receive.

    What jars do I need to add to do that? Do I need standalone JMS Provider(and which one, if yes) or I can just create some Spring beans like MessageListenerAdapter and do my job?

    Are there any simple example (Tomcat+Spring+JMS) that I can start with?

  • #2
    I've found the solution.

    You will need the following JAR files on your classpath:
    apache-activemq-4.1.0-incubator.jar
    mx4j-2.1.1.jar
    mx4j-remote-2.1.1.jar


    And then some beans:
    Code:
    <bean id="defaultQueue" class="org.apache.activemq.command.ActiveMQQueue">
        <constructor-arg value="defaultQueue"/>
      </bean>
      
      <bean id="connectionFactory" class="org.apache.activemq.spring.ActiveMQConnectionFactory">
        <property name="brokerURL" value="vm://localhost"/>
      </bean>
      
      <bean id="listenerContainer" class="org.springframework.jms.listener.SimpleMessageListenerContainer">
        <property name="connectionFactory" ref="connectionFactory"/>
        <property name="destination" ref="defaultQueue"/>
        <property name="messageListener">
         <bean class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
              <property name="delegate">
                   <bean class="SomeServiceClassWithHandleMessageMethod"/>
              </property>
         </bean>
        </property>
      </bean>
      
      <!-- This one is to send test messages -->
      <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="connectionFactory"/>
        <property name="defaultDestination" ref="defaultQueue"/>
      </bean>
    And it works just fine.

    Comment


    • #3
      You need a message broker, such as the suggested ActiveMQ, and define a Spring SimpleMessageListenerContainer or DefaultMessageListenerContainer for it (with the latter having the advantage of more configuration options and built-in auto-recovery). Of course, an important factor is where your messages come from: The originating process will have to talk to the same broker to send its messages, which might affect your choice of broker.

      Note that we recommend to set the message listener container's "sessionTransacted" flag to "true" for transactional receives (in which case messages will be resent in case of processing failure in the listener). Furthermore, for simple purposes, you might want to simply implement the "javax.jms.MessageListener" interface instead of using a POJO with a Spring MessageListenerAdapter in front of it.

      Juergen

      Comment


      • #4
        Hello
        I would like to use SPRING JMS in axis2, so please could i have an example using SPRING JMS and all XML configuration file (applicationContext).
        THX

        Comment


        • #5
          Using MessageListener without MessageListenerAdapter

          Originally posted by Juergen Hoeller View Post
          Furthermore, for simple purposes, you might want to simply implement the "javax.jms.MessageListener" interface instead of using a POJO with a Spring MessageListenerAdapter in front of it.

          Juergen
          Could you pl show an example of a simple MessageListener
          without MessageListenerAdapter?

          Regards

          /U

          Comment


          • #6
            Code:
            <bean id="listenerContainer" class="org.springframework.jms.listener.SimpleMessageListenerContainer">
                <property name="connectionFactory" ref="connectionFactory"/>
                <property name="destination" ref="defaultQueue"/>
                <property name="messageListener">
                 <bean class="MyMessageListener"/>
                </property>
              </bean>

            Comment


            • #7
              Hello everybody
              i try to make a communication between Client and webservice using Spring JMS. I wrote this code to the server side :
              Code:
              public interface SKBRemotingJMSService {
              	public String addServiceDescription(String service, String description);
              
              	public String publishServiceDescription(String service);
              }
              Code:
              package skbSource;
              
              import java.util.HashMap;
              
              public class SKBRemotingJMSServiceImpl implements SKBRemotingJMSService{
              	HashMap<String, SKB> serviceDescription = new HashMap<String, SKB>();
              
              	public String addServiceDescription(String service, String description) {
              
              		this.serviceDescription.put(service, new SKB(service,description));
              		return "ok";
              	}
              
              	public String publishServiceDescription(String service) {
              		if (this.serviceDescription.containsKey(service))
              		return this.serviceDescription.get(service).getDescription();
              		else return "Service not Found";
              	}
              }
              Code:
              package skbSource;
              
              import org.apache.axiom.om.OMElement;
              import org.apache.axis2.engine.ServiceLifeCycle;
              import org.apache.axis2.context.ConfigurationContext;
              import org.apache.axis2.context.OperationContext;
              import org.apache.axis2.context.ServiceContext;
              import org.apache.axis2.description.AxisService;
              import org.springframework.context.support.ClassPathXmlApplicationContext;
              
              import org.apache.commons.logging.Log;
              import org.apache.commons.logging.LogFactory;
              
              public class SpringInit implements ServiceLifeCycle {
                      
                  private static Log logger = LogFactory
                      .getLog(SpringInit .class);
              
                  // The web service
                  public OMElement springInit(OMElement ignore) {
              
                      return null;
                  }
              
                  public void init(ServiceContext serviceContext) {
                      
                  }
              
                  public void setOperationContext(OperationContext arg0) {
              
                  }
              
                  public void destroy(ServiceContext arg0) {
              
                  }
              
                   /**
                   * this will be called during the deployement time of the service. irrespective
                   * of the service scope this method will be called
                   */
                  public void startUp(ConfigurationContext ignore, AxisService service) {
                      new
                      ClassPathXmlApplicationContext(new String[] {"server.xml","jms.xml"});
                      //appCtx.refresh();
                     // appCtx1.refresh();
                 // new ClassPathXmlApplicationContext(new String[] {""});
                  // new ClassPathXmlApplicationContext(new String[] {"jms.xml"}); 
              
                      if (logger.isDebugEnabled()) {
                          logger.debug("\n\nstartUp() set spring classloader via axisService.getClassLoader() ... ");
                      }
                  }
                  /**
                   * this will be called during the deployement time of the service. irrespective
                   * of the service scope this method will be called
                   */
                  public void shutDown(ConfigurationContext ignore, AxisService service) {
                      
                  }
              }
              Code:
              <?xml version="1.0" encoding="UTF-8"?>
              <beans xmlns="http://www.springframework.org/schema/beans"
                    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                    xsi:schemaLocation="http://www.springframework.org/schema/beans
                       http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
              
                 <bean id="SKBRemotingJMSService"
                      class="org.springframework.jms.remoting.JmsInvokerServiceExporter">
                    <property name="serviceInterface" value="skbSource.SKBRemotingJMSService"/>
                    <property name="service">
                       <bean class="skbSource.SKBRemotingJMSServiceImpl"/>
                    </property>
                 </bean>
              
                 <bean class="org.springframework.jms.listener.SimpleMessageListenerContainer">
                    <property name="connectionFactory" ref="connectionFactory"/>
                    <property name="destination" ref="queue"/>
                    <property name="concurrentConsumers" value="3"/>
                    <property name="messageListener" ref="SKBRemotingJMSService"/>
                 </bean>
              
              </beans>
              Code:
              <?xml version="1.0" encoding="UTF-8"?>
              <beans xmlns="http://www.springframework.org/schema/beans"
              	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              	xsi:schemaLocation="http://www.springframework.org/schema/beans
                      http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
              
              	<bean id="connectionFactory"
              		class="org.apache.activemq.ActiveMQConnectionFactory">
              		<property name="brokerURL" value="tcp://TOU-DEV16:61616" />
              	</bean>
              
              	<bean id="queue"
              		class="org.apache.activemq.command.ActiveMQQueue">
              		<constructor-arg value="mmm" />
              	</bean>
              
              </beans>
              Code:
              <?xml version="1.0" encoding="UTF-8"?>
              <serviceGroup>
              	<service name="SpringInit"
              		class="skbSource.SpringInit">
              		<description>This web service initializes SKB</description>
              		<parameter name="ServiceClass">
              			skbSource.SpringInit
              		</parameter>
              		<parameter name="ServiceTCCL">composite</parameter>
              		<parameter name="load-on-startup">true</parameter>
              		<operation name="springInit">
              			<messageReceiver
              				class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver" />
              		</operation>
              	</service>
                <service name="SKBRemotingJMSService">
                  <description>SKB JMS Remoting service </description>
                  <messageReceivers>
                    <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"
                      class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"/>
                    <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
                      class="org.apache.axis2.rpc.receivers.RPCMessageReceiver"/>
                  </messageReceivers>
                  <parameter name="ServiceClass" locked="true">skbSource.SKBRemotingJMSServiceImpl</parameter>
              </service>
              </serviceGroup>
              But I got this errors, the start up code can't find the the spring configuration file server.xml and jms.xml. the problem come from the methode :ClassPathXmlApplicationContext(new String[] {"server.xml","jms.xml"}).
              could you please help me !
              Code:
              Error:
               org.apache.axis2.deployment.DeploymentException: Processing Operations Modules IOException parsing XML document from class path resource [server.xml]; nested exception is java.io.FileNotFoundException:
               class path resource [server.xml] cannot be opened because it does not exist; nested exception is:
               org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [server.xml]; nested exception is java.io.FileNotFoundException:
               class path resource [server.xml] cannot be opened because it does not exist; nested exception is: org.apache.axis2.deployment.DeploymentException: IOException parsing XML document from class path resource [server.xml]; nested exception is java.io.FileNotFoundException:
               class path resource [server.xml] cannot be opened because it does not exist; nested exception is:
              org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [server.xml]; nested exception is java.io.FileNotFoundException:
               class path resource [server.xml] cannot be opened because it does not exist; nested exception is: org.apache.axis2.deployment.DeploymentException: Processing Operations Modules IOException parsing XML document from class path resource [server.xml]; nested exception is java.io.FileNotFoundException:
               class path resource [server.xml] cannot be opened because it does not exist; nested exception is: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [server.xml]; nested exception is java.io.FileNotFoundException:
               class path resource [server.xml] cannot be opened because it does not exist; nested exception is: org.apache.axis2.deployment.DeploymentException: IOException parsing XML document from class path resource [server.xml]; nested exception is java.io.FileNotFoundException:
              class path resource [server.xml] cannot be opened because it does not exist; nested exception is:
               org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [server.xml]; nested exception is java.io.FileNotFoundException:
               class path resource [server.xml] cannot be opened because it does not exist; nested exception is: org.apache.axis2.deployment.DeploymentException:
               Processing Operations Modules IOException parsing XML document from class path resource [server.xml]; nested exception is java.io.FileNotFoundException: class path resource [server.xml] cannot be opened because it does not exist; nested exception is: org.springframework.beans.factory.BeanDefinitionStoreException:
               IOException parsing XML document from class path resource [server.xml]; nested exception is java.io.FileNotFoundException:
               class path resource [server.xml] cannot be opened because it does not exist; nested exception is:

              Comment


              • #9
                Thanks

                Thank u for u r mail that is solved one big problem in my project


                Originally posted by Bas View Post
                I've found the solution.

                You will need the following JAR files on your classpath:
                apache-activemq-4.1.0-incubator.jar
                mx4j-2.1.1.jar
                mx4j-remote-2.1.1.jar


                And then some beans:
                Code:
                <bean id="defaultQueue" class="org.apache.activemq.command.ActiveMQQueue">
                    <constructor-arg value="defaultQueue"/>
                  </bean>
                  
                  <bean id="connectionFactory" class="org.apache.activemq.spring.ActiveMQConnectionFactory">
                    <property name="brokerURL" value="vm://localhost"/>
                  </bean>
                  
                  <bean id="listenerContainer" class="org.springframework.jms.listener.SimpleMessageListenerContainer">
                    <property name="connectionFactory" ref="connectionFactory"/>
                    <property name="destination" ref="defaultQueue"/>
                    <property name="messageListener">
                     <bean class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
                          <property name="delegate">
                               <bean class="SomeServiceClassWithHandleMessageMethod"/>
                          </property>
                     </bean>
                    </property>
                  </bean>
                  
                  <!-- This one is to send test messages -->
                  <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
                    <property name="connectionFactory" ref="connectionFactory"/>
                    <property name="defaultDestination" ref="defaultQueue"/>
                  </bean>
                And it works just fine.

                Comment

                Working...
                X