HornetQ Stand Alone Server Example using Maven

HornetQ is an open source project to build a multi-protocol, embeddable, very high performance, clustered, asynchronous messaging system. HornetQ supports the JMS 1.1 API and also defines its own messaging API for maximum performance and flexibility. HornetQ class-beating high performance journal provides persistent messaging performance at rates normally seen for non-persistent messaging. HornetQ offers server replication and automatic client fail-over to eliminate lost or duplicated messages in case of server failure.

In this post, we will learn about most basic configuration for running the hornetq server as stand alone server i.e. outside any container like jboss.

Step 1) Create a maven project using below command and convert it to eclipse java project

mvn archetype:generate -DgroupId=com.howtodoinjava -DartifactId=HornetQHelloWorld
-DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

 cd HornetQHelloWorld

 mvn eclipse:eclipse

step 2) Update pom.xml file and update the project dependencies

pom.xml

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>com.howtodoinjava</groupid>
  <artifactid>HornetQHelloWorld</artifactid>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>HornetQHelloWorld</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupid>junit</groupid>
      <artifactid>junit</artifactid>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  <dependency>
     <groupid>org.hornetq</groupid>
     <artifactid>hornetq-core</artifactid>
     <version>2.0.0.GA</version>
     <scope>compile</scope>
  </dependency>
  <dependency>
     <groupid>org.hornetq</groupid>
     <artifactid>hornetq-jms</artifactid>
     <version>2.0.0.GA</version>
     <scope>compile</scope>
  </dependency>
  <dependency>
     <groupid>org.hornetq</groupid>
     <artifactid>hornetq-logging</artifactid>
     <version>2.0.0.GA</version>
     <scope>compile</scope>
  </dependency>
  <dependency>
     <groupid>org.hornetq</groupid>
     <artifactid>hornetq-transports</artifactid>
     <version>2.0.0.GA</version>
     <scope>compile</scope>
  </dependency>
  <dependency>
     <groupid>org.jboss.netty</groupid>
     <artifactid>netty</artifactid>
     <version>3.1.0.GA</version>
  </dependency>
  <dependency>
     <groupid>org.jboss.javaee</groupid>
     <artifactid>jboss-jms-api</artifactid>
     <version>1.1.0.GA</version>
     <scope>compile</scope>
  </dependency>
  </dependencies>

Step 3) Place the basic hornetq configuration file in classpath.

hornetq-configuration.xml

< ?xml version="1.0"?>
xsi:schemaLocation="urn:hornetq /schema/hornetq-configuration.xsd"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:hornetq">
	<connectors>
		<connector name="netty-connector">
			<factory -class>org.hornetq.integration.transports.netty.NettyConnectorFactory
			</factory>
		</connector>
	</connectors>
	<acceptors>
		<acceptor name="netty-acceptor">
			<factory -class>org.hornetq.integration.transports.netty.NettyAcceptorFactory
			</factory>
		</acceptor>
	</acceptors>
	<security -enabled>false</security>

Step 4) Configure the connector factory and place the configuration file in classpath.

hornetq-jms.xml

< ?xml version="1.0"?>
xsi:schemaLocation="urn:hornetq /schema/hornetq-jms.xsd"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:hornetq">
	<!--the connection factory used by the example -->
	<connection -factory name="ConnectionFactory">
		<connectors>
			<connector -ref connector-name="netty-connector"></connector>
		</connectors>
		<entries>
			<entry name="ConnectionFactory"></entry>
		</entries>
	</connection>
	<queue name="exampleQueue">
		<entry name="exampleQueue"></entry>
	</queue>

Step 5) Start the server

EmbeddedServerDemo.java

package com.howtodoinjava;

import org.hornetq.core.config.impl.FileConfiguration;
import org.hornetq.core.server.HornetQServer;
import org.hornetq.core.server.HornetQServers;
import org.hornetq.jms.server.JMSServerManager;
import org.hornetq.jms.server.impl.JMSServerManagerImpl;

public class EmbeddedServerDemo
{
	public static void main(String[] args) throws Exception {
		try
		{
			//Load the file configuration first of all
			FileConfiguration configuration = new FileConfiguration();
			configuration.setConfigurationUrl("hornetq-configuration.xml");
			configuration.start();

			//Create a new instance of hornetq server
			HornetQServer server = HornetQServers.newHornetQServer(configuration);

			//Wrap inside a JMS server
			JMSServerManager jmsServerManager = new JMSServerManagerImpl(
					server, "hornetq-jms.xml");

			// if you want to use JNDI, simple inject a context here or don't
			// call this method and make sure the JNDI parameters are set.
			jmsServerManager.setContext(null);

			//Start the server
			jmsServerManager.start();

			//WOO HOO
			System.out.println("HornetQ server started successfully !!");
		}
		catch (Throwable e)
		{
			System.out.println("Well, you seems to doing something wrong. Please check if config files are in your classes folder.");
			e.printStackTrace();
		}
	}
}

Output in console:

22 Mar, 2013 2:09:33 PM org.hornetq.core.logging.impl.JULLogDelegate info
INFO: live server is starting..
22 Mar, 2013 2:09:33 PM org.hornetq.core.logging.impl.JULLogDelegate warn
WARNING: AIO wasn't located on this platform, it will fall back to using pure Java NIO. If your platform is Linux, install LibAIO to enable the AIO journal
22 Mar, 2013 2:09:33 PM org.hornetq.core.logging.impl.JULLogDelegate info
INFO: Using NIO Journal
22 Mar, 2013 2:09:33 PM org.hornetq.core.logging.impl.JULLogDelegate warn
WARNING: Security risk! It has been detected that the cluster admin user and password have not been changed from the installation default. Please see the HornetQ user guide, cluster chapter, for instructions on how to do this.
22 Mar, 2013 2:09:33 PM org.hornetq.core.logging.impl.JULLogDelegate info
INFO: Started Netty Acceptor version 3.1.5.GA-r1772
22 Mar, 2013 2:09:33 PM org.hornetq.core.logging.impl.JULLogDelegate info
INFO: HornetQ Server version 2.0.0.GA (Hornet Queen, 113) started

HornetQ server started successfully !!

Download source code

Happy Learning !!

Was this post helpful?

Join 8000+ Awesome Developers, Like YOU!

20 thoughts on “HornetQ Stand Alone Server Example using Maven”

  1. i did the same suggested by you to Cristy Serna, given full path like below :

    configuration.setConfigurationUrl(“/HornetQHelloWorld/src/main/java/hornetq-configuration.xml”);
    JMSServerManager jmsServerManager = new JMSServerManagerImpl(server, “/HornetQHelloWorld/src/main/java/hornetq-jms.xml”);

    Thanks in advance !!

    Reply
  2. Hi i am new to HornetQ,

    i have tried the same mentioned above but getting message like below :

    Well, you seems to doing something wrong. Please check if config files are in your classes folder.
    java.lang.NullPointerException
    at org.hornetq.core.config.impl.FileConfiguration.start(FileConfiguration.java:86)
    at com.howtodoinjava.EmbeddedServerDemo.main(EmbeddedServerDemo.java:17)

    You have mentioned “”Step 5) Start the server””, which server do we need to start, is it HornetQ server ?. Sorry if i am asking a very small question.

    Please help me.

    Reply
      • Find below my config files :
        hornetq-jms.xml

        hornetq-configuration.xml :

        org.hornetq.integration.transports.netty.NettyConnectorFactory

        org.hornetq.integration.transports.netty.NettyAcceptorFactory

        false

        java class :

        package com.howtodoinjava;

        import org.hornetq.core.config.impl.FileConfiguration;
        import org.hornetq.core.server.HornetQServer;
        import org.hornetq.core.server.HornetQServers;
        import org.hornetq.jms.server.JMSServerManager;
        import org.hornetq.jms.server.impl.JMSServerManagerImpl;

        public class EmbeddedServerDemo
        {
        public static void main(String[] args) throws Exception {
        try
        {
        //Load the file configuration first of all
        FileConfiguration configuration = new FileConfiguration();
        configuration.setConfigurationUrl(“hornetq-configuration.xml”);
        configuration.start();

        //Create a new instance of hornetq server
        HornetQServer server = HornetQServers.newHornetQServer(configuration);

        //Wrap inside a JMS server
        JMSServerManager jmsServerManager = new JMSServerManagerImpl(server, “hornetq-jms.xml”);

        // if you want to use JNDI, simple inject a context here or don’t
        // call this method and make sure the JNDI parameters are set.
        jmsServerManager.setContext(null);

        //Start the server
        jmsServerManager.start();

        //WOO HOO
        System.out.println(“HornetQ server started successfully !!”);
        }
        catch (Throwable e)
        {
        System.out.println(“Well, you seems to doing something wrong. Please check if config files are in your classes folder.”);
        e.printStackTrace();
        }
        }
        }

        Anything else i need to change like queue details in config files and need to add any jars ?

        Reply
        • hornetq-jms.xml

          org.hornetq.integration.transports.netty.NettyConnectorFactory

          org.hornetq.integration.transports.netty.NettyAcceptorFactory

          false

          Reply
          • Find below the error message :

            Well, you seems to doing something wrong. Please check if config files are in your classes folder.
            java.net.MalformedURLException: no protocol: hornetq-configuration.xml
            at java.net.URL.(URL.java:567)
            at java.net.URL.(URL.java:464)
            at java.net.URL.(URL.java:413)
            at org.hornetq.core.config.impl.FileConfiguration.start(FileConfiguration.java:67)
            at com.howtodoinjava.EmbeddedServerDemo.main(EmbeddedServerDemo.java:17)

          • getting below error after doing some changes :

            Well, you seems to doing something wrong. Please check if config files are in your classes folder.
            java.lang.NullPointerException
            at org.hornetq.core.config.impl.FileConfiguration.start(FileConfiguration.java:86)
            at com.howtodoinjava.EmbeddedServerDemo.main(EmbeddedServerDemo.java:17)

  3. I´m in trouble again, i hope you can help me, when i try to run the class EmbeddedServerDemo i get this error:
    Well, you seems to doing something wrong. Please check if config files are in your classes folder.
    java.lang.NullPointerException
    at org.hornetq.core.config.impl.FileConfiguration.start(FileConfiguration.java:86)
    at com.howtodoinjava.EmbeddedServerDemo.main(EmbeddedServerDemo.java:17)

    I can’t understand what is wrong.

    Reply
    • You are running the example from command prompt so the files hornetq-configuration.xml and hornetq-jms.xml are not found in classpath. Try running the example from eclipse or edit the exact file paths in java file.

      Reply
  4. I got the code, but how can i run it? if i go to the path and run the next code:
    java -cp target/HornetQHelloWorld-1.0-SNAPSHOT.jar com.howtodoinjava.App
    i get the next error “Error: no se ha encontrado o cargado la clase principal com.howtodoinjava.App”
    Could you help me please, thank you

    Reply

Leave a Comment

About HowToDoInJava

This blog provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions, and frequently asked interview questions.