1 2 Previous Next 22 Replies Latest reply on Jul 13, 2009 8:00 AM by leosbitto Go to original post
      • 15. Re: Embedded server - how to create JMS Topic?
        rkapur123

        Hi Leos.bitto

        I have jboss SOA 4.3 ESB which has Jboss Messaging as messaging provoder.

        Using Spring framework's MDP (message driven pojo) - I want to listen on "sampleQueue". As per springframework 2.5 guide - MDP is a simple class which implements javax.jms.MessageListener onMessage(). I have the class and specified the following in applicationContext.xml file

        
        <bean id="messageListener" class="com.acme.SpringMDP" />
        
        <bean id="listenerContainer"
         class="org.springframework.jms.listener.DefaultMessageListenerContainer">
         <property name="concurrentConsumers" value="5" />
         <property name="destination" ref="destination" />
         <property name="connectionFactory" ref="connectionFactory" />
         <property name="messageListener" ref="messageListener" />
         </bean>



        but how should I define "connectionFactory" and "destination" in spring context file.

        Earlier you wrote:

        My application uses the ApplicationContext from Spring Framework, so I already have an XML configuration file, and it is easy to add few lines there to create the necessarry JMS queues and topics, using my simple class which calls your core API. The same is true for creating the JMS (XA)ConnectionFactory - no JNDI needed.


        I would really appreciate if you can tell how you configure connection with Jboss.

        kind regards,
        rishi



        • 16. Re: Embedded server - how to create JMS Topic?
          leosbitto

           

          "rkapur123" wrote:
          Hi Leos.bitto

          I have jboss SOA 4.3 ESB which has Jboss Messaging as messaging provoder.

          Using Spring framework's MDP (message driven pojo) - I want to listen on "sampleQueue". As per springframework 2.5 guide - MDP is a simple class which implements javax.jms.MessageListener onMessage().


          I do not use any support for JMS from Spring, because my trust in their JMS code vanished when I found out how terribly inefficient is their JmsTemplate. So I have studied the JMS 1.1 specification and wrote the necessarry code myself - it was not difficult, and I got a code which performs much better.

          "rkapur123" wrote:

          I have the class and specified the following in applicationContext.xml file

           <bean id="messageListener" class="com.acme.SpringMDP" />
          
           <bean id="listenerContainer"
           class="org.springframework.jms.listener.DefaultMessageListenerContainer">
           <property name="concurrentConsumers" value="5" />
           <property name="destination" ref="destination" />
           <property name="connectionFactory" ref="connectionFactory" />
           <property name="messageListener" ref="messageListener" />
           </bean>
          


          but how should I define "connectionFactory" and "destination" in spring context file.


          There are more possibilities, but the most versatile one seems to be to use JNDI - that actually makes your code compatible with most JMS providers, not only JBoss Messaging. Check the chapter 5.3. JNDI configuration of the JBoss Messaging documentation and use something like this (not tested):

           <bean name="jbmJndiEnv">
           <props>
           <prop key="java.naming.factory.initial">org.jnp.interfaces.NamingContextFactory</prop>
           <prop key="java.naming.provider.url">jnp://myhost:1099</prop>
           <prop key="java.naming.factory.url.pkgs">org.jboss.naming:org.jnp.interfaces</prop>
           </props>
           </property>
          
           <bean id="connectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
           <property name="jndiEnvironment" ref="jbmJndiEnv" />
           <property name="lookupOnStartup" value="false"/>
           <property name="proxyInterface" value="javax.jms.ConnectionFactory"/>
           <property name="jndiName" value="ConnectionFactory" />
           </bean>
          
           <bean id="destination" class="org.springframework.jndi.JndiObjectFactoryBean">
           <property name="jndiEnvironment" ref="jbmJndiEnv" />
           <property name="lookupOnStartup" value="false"/>
           <property name="proxyInterface" value="javax.jms.Destination" />
           <property name="jndiName" value="/queue/ExampleQueue" />
           </bean>
          


          • 17. Re: Embedded server - how to create JMS Topic?
            leosbitto

            Because I have started this thread about embedding the JBoss Messaging server, here is my solution for everybody who would need it:

            import java.util.HashMap;
            import java.util.HashSet;
            import java.util.Map;
            import java.util.Set;
            import org.jboss.messaging.core.config.Configuration;
            import org.jboss.messaging.core.config.TransportConfiguration;
            import org.jboss.messaging.core.config.impl.ConfigurationImpl;
            import org.jboss.messaging.core.remoting.impl.invm.InVMAcceptorFactory;
            import org.jboss.messaging.core.remoting.impl.invm.TransportConstants;
            import org.jboss.messaging.core.server.Messaging;
            import org.jboss.messaging.core.server.MessagingServer;
            import org.jboss.messaging.jms.JBossQueue;
            import org.jboss.messaging.jms.JBossTopic;
            import org.jboss.messaging.utils.SimpleString;
            
            public class EmbeddedServer {
            
             private final MessagingServer server;
            
             public EmbeddedServer() {
             final Set<TransportConfiguration> transports = new HashSet<TransportConfiguration>();
             transports.add(new TransportConfiguration(InVMAcceptorFactory.class.getName()));
             server = createMessagingServer(transports);
             }
            
             public EmbeddedServer(int serverID) {
             final Map<String,Object> params = new HashMap<String,Object>();
             params.put(TransportConstants.SERVER_ID_PROP_NAME, serverID);
             final Set<TransportConfiguration> transports = new HashSet<TransportConfiguration>();
             transports.add(new TransportConfiguration(InVMAcceptorFactory.class.getName(), params));
             server = createMessagingServer(transports);
             }
            
             public static MessagingServer createMessagingServer(Set<TransportConfiguration> transports) {
             final Configuration configuration = new ConfigurationImpl();
             configuration.setPersistenceEnabled(false);
             configuration.setSecurityEnabled(false);
             configuration.setAcceptorConfigurations(transports);
             return Messaging.newMessagingServer(configuration);
             }
            
             public void start() throws Exception {
             server.start();
             }
            
             public void stop() throws Exception {
             server.stop();
             }
            
             public JBossQueue createQueue(String name, boolean temporary) throws Exception {
             final SimpleString q = JBossQueue.createAddressFromName(name);
             server.createQueue(q, q, null, false, temporary);
             return new JBossQueue(name);
             }
            
             public JBossQueue createQueue(String name) throws Exception {
             return createQueue(name, false);
             }
            
             public JBossQueue createTemporaryQueue(String name) throws Exception {
             return createQueue(name, true);
             }
            
             public JBossTopic createTopic(String name, boolean temporary) throws Exception {
             final SimpleString t = JBossTopic.createAddressFromName(name);
             server.createQueue(t, t, new SimpleString("__JBMX=-1"), false, temporary);
             return new JBossTopic(name);
             }
            
             public JBossTopic createTopic(String name) throws Exception {
             return createTopic(name, false);
             }
            
             public JBossTopic createTemporaryTopic(String name) throws Exception {
             return createTopic(name, true);
             }
            }
            


            You need javax.jms.(XA)ConnectionFactory, too:

            import java.util.HashMap;
            import java.util.Map;
            import org.jboss.messaging.core.config.TransportConfiguration;
            import org.jboss.messaging.core.remoting.impl.invm.InVMConnectorFactory;
            import org.jboss.messaging.core.remoting.impl.invm.TransportConstants;
            import org.jboss.messaging.jms.client.JBossConnectionFactory;
            
            public class InVMConnector {
            
             public static final String DEFAULT_NAME = InVMConnectorFactory.class.getName();
            
             public static JBossConnectionFactory createJBossConnectionFactory() {
             return createJBossConnectionFactory(DEFAULT_NAME);
             }
            
             public static JBossConnectionFactory createJBossConnectionFactory(String className) {
             final TransportConfiguration transportConfiguration =
             new TransportConfiguration(className);
             return new JBossConnectionFactory(transportConfiguration);
             }
            
             public static JBossConnectionFactory createJBossConnectionFactory(int serverID) {
             return createJBossConnectionFactory(serverID, DEFAULT_NAME);
             }
            
             public static JBossConnectionFactory createJBossConnectionFactory(int serverID, String className) {
             final Map<String,Object> connectionParams = new HashMap<String,Object>();
             connectionParams.put(TransportConstants.SERVER_ID_PROP_NAME, serverID);
             final TransportConfiguration transportConfiguration =
             new TransportConfiguration(className, connectionParams);
             return new JBossConnectionFactory(transportConfiguration);
             }
            
            }
            


            These two classes are designed to be used with the ApplicationContext from Spring Framework, here is a sample configuration:

             <bean id="JBMServer" class="EmbeddedServer" init-method="start" destroy-method="stop" />
            
             <bean id="JBMInVMConnectionFactory" class="InVMConnector" factory-method="createJBossConnectionFactory" depends-on="JBMServer" />
            
             <bean id="JBMtestQueue" factory-bean="JBMServer" factory-method="createQueue">
             <constructor-arg value="testQueue" />
             </bean>
            
             <bean id="JBMtestTopic" factory-bean="JBMServer" factory-method="createTopic">
             <constructor-arg value="testTopic" />
             </bean>
            


            This solution is obviously useful only when the message persistence is not needed. If the persistence is needed, running an embedded server seems to be inappropriate - run a standalone server in that case instead.

            • 18. Re: Embedded server - how to create JMS Topic?
              ataylor

              or If you want to run a full standalone server in spring use something like the following and make sure the JBM config files are available on the classpath

              <?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.xsd">
              
               <bean name="messagingServer" class="org.jboss.messaging.core.server.impl.MessagingServerImpl" init-method="start" destroy-method="stop">
               <constructor-arg ref="Configuration"/>
               <constructor-arg ref="JBMSecurityManager"/>
               </bean>
              
               <bean name="Configuration" class="org.jboss.messaging.core.config.impl.FileConfiguration" init-method="start" destroy-method="stop"/>
              
               <bean name="JBMSecurityManager" class="org.jboss.messaging.core.security.impl.JBMSecurityManagerImpl"/>
              
              
              </beans>




              • 19. Re: Embedded server - how to create JMS Topic?
                leosbitto

                 

                "ataylor" wrote:
                or If you want to run a full standalone server in spring use something like the following and make sure the JBM config files are available on the classpath


                Cool, it is nice to see that you guys from JBoss are willing to suggest a straight replacement of JBoss Microcontainer by Spring Framework. Your example is missing the JMS objects needed at the client side, though - at least javax.jms.(XA)ConnectionFactory and some javax.jms.Destination (Queue or Topic) would be needed, unless you want to use your core API directly.

                • 20. Re: Embedded server - how to create JMS Topic?
                  ataylor

                   

                  Cool, it is nice to see that you guys from JBoss are willing to suggest a straight replacement of JBoss Microcontainer by Spring Framework


                  I wouldnt say that, this is just something i was messing round with.

                  Your example is missing the JMS objects needed at the client side, though - at least javax.jms.(XA)ConnectionFactory and some javax.jms.Destination (Queue or Topic) would be needed, unless you want to use your core API directly.


                  These are defined in the jbm-configuration, jbm-jms.xml and jbm-users.xml files.

                  • 21. Re: Embedded server - how to create JMS Topic?
                    timfox

                     

                    "Leos.Bitto" wrote:

                    Cool, it is nice to see that you guys from JBoss are willing to suggest a straight replacement of JBoss Microcontainer by Spring Framework.


                    Well.. we're not recommending a replacement of JBoss MC by Spring.

                    But we also realise that a lot of people use Spring, or A.N. Other dependency injection framework and we want JBM to be flexible enough to work with all those :)

                    • 22. Re: Embedded server - how to create JMS Topic?
                      leosbitto

                       

                      "ataylor" wrote:
                      Cool, it is nice to see that you guys from JBoss are willing to suggest a straight replacement of JBoss Microcontainer by Spring Framework


                      I wouldnt say that, this is just something i was messing round with.


                      Well, I have actually suspected that the omission of JBoss Microcontainer in your example would be most probably considered a mistake by most RedHat-employed people. :-)

                      "ataylor" wrote:

                      Your example is missing the JMS objects needed at the client side, though - at least javax.jms.(XA)ConnectionFactory and some javax.jms.Destination (Queue or Topic) would be needed, unless you want to use your core API directly.


                      These are defined in the jbm-configuration, jbm-jms.xml and jbm-users.xml files.


                      Sure, but that is where the server picks them. I wrote that your example (unlike mine) does not show how the client is supposed to reach these objects.

                      1 2 Previous Next