Enterprise Integration Zone is brought to you in partnership with:

Billy is a Swedish consultant in the java space focusing on solving integration challenges. He started his career focusing on heavyweight IBM products but has since then switched over to open source alternatives. When he is not churning out code or instructing for his employer Redpill-Linpro he likes to spend his time home in the beautiful archipelago outside Stockholm with his fiancee Maria and their cat Zorro. Billy is a DZone MVB and is not an employee of DZone and has posted 9 posts at DZone. You can read more from them at their website. View Full User Profile

Camel and HornetQ as JMS provider

06.03.2012
| 7303 views |
  • submit to reddit
In my current project we're utilizing JBoss HornetQ as the messaging bus, and I wanted to try out how Camel could connect to it. My intention was to have the HornetQ instance running externally from the Camel application and connect to it. There are a bunch of examples on how to embedd HornetQ 2.2.5 (the release I'm dealing with) inside Spring, but I could'nt find a single one for how to connect to an external instance.

For my own reference I post the working solution here which uses HornetQ's JNDI to lookup the ConnectionFactory as well as a Spring JNDI template.
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:camel="http://camel.apache.org/schema/spring"
       xsi:schemaLocation="
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
          http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">

    <bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
        <property name="environment">
            <props>
                <prop key="java.naming.factory.initial">org.jnp.interfaces.NamingContextFactory</prop>
                <prop key="java.naming.provider.url">jnp://localhost:1099</prop>
                <prop key="java.naming.factory.url.pkgs">org.jnp.interfaces:org.jboss.naming</prop>
            </props>
        </property>
    </bean>

    <bean id="jmsQueueConnectionFactory"
      class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiTemplate">
            <ref bean="jndiTemplate"/>
        </property>
        <property name="jndiName">
            <value>ConnectionFactory</value>
        </property>
    </bean>
    
    <bean name="jms" class="org.apache.camel.component.jms.JmsComponent">
        <property name="connectionFactory" ref="jmsQueueConnectionFactory"/>
    </bean>

    <camel:camelContext id="context1">
        <camel:route id="FirstRoute">
            <camel:from uri="jms:queue:inputqueue"/>
            <camel:log logName="jmsLog" message="Got message from JMS queue:"/>
            <camel:to uri="jms:topic:helloworld"/>
        </camel:route>
    </camel:camelContext>
</beans>
And the Maven POM:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>camelHornet</groupId>
    <artifactId>camelHornet</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <camel.version>2.8.0</camel.version>
        <hornet.version>2.2.5.Final</hornet.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-core</artifactId>
            <version>${camel.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-spring</artifactId>
            <version>${camel.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-jms</artifactId>
            <version>${camel.version}</version>
        </dependency>
          <dependency>
            <groupId>org.hornetq</groupId>
            <artifactId>hornetq-core-client</artifactId>
            <version>${hornet.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hornetq</groupId>
            <artifactId>hornetq-jms-client</artifactId>
            <version>${hornet.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hornetq</groupId>
            <artifactId>hornetq-jms</artifactId>
            <version>${hornet.version}</version>
        </dependency>

        <dependency>
                <groupId>javax.jms</groupId>
                <artifactId>jms</artifactId>
                <version>1.1</version>
                <type>jar</type>
                <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.hornetq</groupId>
            <artifactId>hornetq-logging</artifactId>
            <version>${hornet.version}</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.netty</groupId>
            <artifactId>netty</artifactId>
            <version>3.2.3.Final</version>
        </dependency>
        <dependency>
            <groupId>jboss</groupId>
            <artifactId>jnp-client</artifactId>
            <version>4.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.logging</groupId>
            <artifactId>jboss-logging</artifactId>
            <version>3.0.0.CR1</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
              <groupId>org.apache.camel</groupId>
              <artifactId>camel-maven-plugin</artifactId>
              <version>${camel.version}</version>
              <configuration>
                <applicationContextUri>META-INF/context.xml</applicationContextUri>
              </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Published at DZone with permission of Billy Sjöberg, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Daniel Slazer replied on Tue, 2012/06/12 - 12:05pm

Pretty nooby question I guess. Problem is, I have this code:
private class ButtonHandler implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
if (event.getSource () == search)
{
query = searchfield.getText();
}
}
}
I would like to write Query to a file. When I add the "throws IOException" line to the end of the method, it gives me an error. How do I make this work?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.