12 Replies Latest reply on Nov 8, 2013 1:35 AM by ananymous7239

    How to integrate HornetQ in Spring?

    jbx

      I am trying to get HornetQ integrated within a Spring application. I basically want to initialise an embedded HornetQ instance, together with the respective JMS service for message consumers hosted within other JVMs to connect to it and pull off messages.

       

      Chapter 43 states that HornetQ can be easily integrated within Spring but it doesnt state how. (The example in 43.2 is only valid for the JBoss Micro Container and doesnt include the JMS Server. I tried looking elsewhere on the web and there doesnt seem to be much information anywhere.

       

      The only link I found is this: http://www.javacodegeeks.com/2010/06/spring-3-hornetq-21-integration.html

      which is already bloated and polluted with extra info such as Tomcat and conflicts with its JNDI. I tried to filter out the tomcat specific part since I first want to get it to work in a vanilla standalone application to understand what I am doing, but I just got a bunch of JNDI errors, typically: javax.naming.NoInitialContextException.

       

      Can someone provide a typical minimal spring configuration for a standalone application with HornetQ embedded? Just one simple queue configuration and the simplest configuration (without the Spring JMSTemplate) that would allow consumers to connect to it from other hosts.

       

      An example of a class within the spring application connecting to it with any of the injection magic would also be helpful.

       

      Thanks (and happy new year!)

        • 1. Re: How to integrate HornetQ in Spring?
          clebert.suconic

          Bill Burke did some work on trunk... and I believe he wrote some doc about it.

           

           

          This is something that we will improve early next year.

          • 2. Re: How to integrate HornetQ in Spring?
            jbx

            Any chance of getting a link to the doc?

            • 3. Re: How to integrate HornetQ in Spring?
              ataylor

              It should be pretty simple, look at the hornetq-beans.xml file which is used by the jboss microcontainer to instantiate the HornetQ pojo's. It should be simple to convert this to a spring beans file. When you do you can contribute it back to the project

              • 4. Re: How to integrate HornetQ in Spring?
                jbx

                I checked the hornetq-beans.xml and I am already initialising all the beans mentioned there. I have also put jndi.properties in the classpath containing the following (I am also sure its picking it up, because when I remove it I get a NoInitialContextException):

                 

                java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
                java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
                java.naming.provider.url=jnp://localhost:1099

                java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory

                java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces

                java.naming.provider.url=jnp://localhost:1099

                 

                I also do have the JNDI Naming Server specified in the same way:

                 

                   <bean name="namingServer" class="org.jnp.server.Main" init-method="start" destroy-method="stop">

                     <property name="namingInfo" ref="namingServerImpl" />

                     <property name="port" value="1099" />

                     <property name="bindAddress" value="localhost" />

                     <property name="rmiPort" value="1098" />

                     <property name="rmiBindAddress" value="localhost" />

                    </bean>

                 

                I just get the following exception, which seems to indicate that I am missing something related to JNDI because it is not connecting to the respective JNDI server on port 1099, but I can't find anywhere a simple working example of what I need to have.

                 

                Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'namingServerImpl' defined in file [C:\Projects\SpringMQTest\src\spring-config.xml]: Invocation of init method failed; nested exception is javax.naming.CommunicationException: Could not obtain connection to any of these urls: jnp://localhost:1099 and discovery failed with error: javax.naming.CommunicationException: Receive timed out [Root exception is java.net.SocketTimeoutException: Receive timed out] [Root exception is javax.naming.CommunicationException: Failed to connect to server localhost/127.0.0.1:1099 [Root exception is javax.naming.ServiceUnavailableException: Failed to connect to server localhost/127.0.0.1:1099 [Root exception is java.net.ConnectException: Connection refused: connect]]]

                at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1401)

                at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:512)

                at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)

                ...

                • 5. Re: How to integrate HornetQ in Spring?
                  ataylor

                  thats because you dont need the:

                   

                  java.naming.provider.url=jnp://localhost:1099

                   

                  this is just for clients, what happening is the naming server itself is trying to bind remotely. you should only use this for the client. If you are using both a client and server in the same vm then you need to set these params a different way

                  • 6. Re: How to integrate HornetQ in Spring?
                    jbx

                    If I remove that line I get a strange NoClassDefFoundError on org.jboss.util.threadpool.ThreadPool .

                     

                    I included the Jar files mentioned in that article i.e.:

                     

                    hornetq-bootstrap.jar

                    hornetq-core.jar

                    hornetq-jms.jar

                    hornetq-logging.jar

                    jbosslogging-spi-2.1.1.GA.jar

                    jnpserver.jar

                    netty.jar

                    jms.jar

                     

                    Remember this is a standalone application not inside any J2EE container.

                     

                    Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'namingServer' defined in file [C:\Projects\SpringMQTest\src\spring-config.xml]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/jboss/util/threadpool/ThreadPool

                    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)

                    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)

                    Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'namingServer' defined

                     

                    in file [C:\Projects\SpringMQTest\src\spring-config.xml]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/jboss/util/threadpool/ThreadPool

                    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
                    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)

                    • 7. Re: How to integrate HornetQ in Spring?
                      ataylor

                      that class is in jboss-common-core.jar

                      • 8. Re: How to integrate HornetQ in Spring?
                        howtodoinjava

                        Though it seems to be an old thread.. still people like me come here frequently. I also tried my hand on spring 3 + hornetq integration and was quite successfull. Possibly it could help someone.

                         

                        http://howtodoinjava.com/2013/03/24/spring-3-hornetq-standalone-integration-example/

                        • 9. Re: How to integrate HornetQ in Spring?
                          sreejithsrkmr

                          I am a newbie to HornetQ as well as Spring. So need some help.

                           

                          I have a standalone HornetQ instance which has the ConnectionFactory and Queue defined and running. I am able to connect to the queue and send/receive messages using standalone java code.

                          I am now trying to wire the JNDI lookup, ConnectionFactory and Queue in a sample Spring webapplication. The basic idea is to put a message from the web application. Any pointers on how to setup the JNDI,

                          ConnectionFactory and Queue in the Spring applicationContext.xml ? The documentation floating around seem to suggest instances where the HornetQ is embedded within the same VM as where the web application runs, while I want to host the HornetQ on a different box.

                          • 10. Re: How to integrate HornetQ in Spring?
                            sreejithsrkmr

                            Got it working. Spring configuration is as follows.

                             

                            <bean id="hornetConnectionFactory" class="org.hornetq.jms.client.HornetQJMSConnectionFactory">

                            <constructor-arg name="ha" value="false" />

                            <constructor-arg>

                            <bean class="org.hornetq.api.core.TransportConfiguration">

                            <constructor-arg

                            value="org.hornetq.core.remoting.impl.netty.NettyConnectorFactory" />

                            <constructor-arg>

                            <map key-type="java.lang.String" value-type="java.lang.Object">

                            <!-- HornetQ standalone instance details -->

                            <entry key="host" value="192.168.22.573"></entry>

                            <entry key="port" value="5445"></entry>

                            </map>

                            </constructor-arg>

                            </bean>

                            </constructor-arg>

                            </bean>

                             

                            <!-- ConnectionFactory Definition -->

                            <bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">

                            <constructor-arg ref="hornetConnectionFactory" />

                            </bean>

                             

                            <!-- Definition of the JMS queue -->

                            <bean id="defaultDestination" class="org.hornetq.jms.client.HornetQQueue">

                            <constructor-arg index="0" value="DemoQueue"/>

                            </bean>

                             

                            <bean id="producerTemplate" class="org.springframework.jms.core.JmsTemplate">

                            <property name="connectionFactory" ref="connectionFactory" />

                            <property name="defaultDestination" ref="defaultDestination" />

                            </bean>

                             

                            <bean id="messageSender" class="com.samples.producer.JMSProducer">

                            <property name="jmsTemplate" ref="producerTemplate" />

                            </bean>

                            • 11. Re: How to integrate HornetQ in Spring?
                              mofarn
                              • 12. Re: How to integrate HornetQ in Spring?
                                ananymous7239

                                Hi lokesh iam new to hornetQ and i tried your example spring integration with HornetQ ,but i am getting the following exception,please help me

                                 

                                log4j:WARN No appenders could be found for logger (org.springframework.context.support.FileSystemXmlApplicationContext).

                                log4j:WARN Please initialize the log4j system properly.

                                Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'namingServerImpl' defined in file [F:\HornetQ Workspace\Spring3HornetQStandaloneIntegration\resource\config\server\spring.xml]: Invocation of init method failed; nested exception is javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial

                                    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1420)

                                    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)

                                    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)

                                    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)

                                    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)

                                    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)

                                    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)

                                    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)

                                    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)

                                    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)

                                    at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:140)

                                    at org.springframework.context.support.FileSystemXmlApplicationContext.<init>(FileSystemXmlApplicationContext.java:84)

                                    at com.howtodoinjava.hornetq.demo.SpringContainerLoader.loadEnvironment(SpringContainerLoader.java:9)

                                    at com.howtodoinjava.hornetq.demo.HornetQMessagingTest.main(HornetQMessagingTest.java:10)

                                Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial

                                    at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)

                                    at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)

                                    at javax.naming.InitialContext.getEnvironment(Unknown Source)

                                    at org.jnp.server.NamingBeanImpl.start(NamingBeanImpl.java:160)

                                    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

                                    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

                                    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

                                    at java.lang.reflect.Method.invoke(Unknown Source)

                                    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1544)

                                    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1485)

                                    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)

                                    ... 13 more

                                 

                                @Lokesh Gupta