Embedded Example

This example shows how to setup and run HornetQ embedded with remote clients connecting.

HornetQ was designed to use POJOs (Plain Old Java Objects), what makes embedding HornetQ as simple as instantiating a few objects.

We have limited the server classpath on this example:

  1. hornetq-core.jar
  2. netty.jar

Similarly we have also limited the classpath on client:

  1. hornetq-core-client.jar
  2. netty.jar

HornetQ Embedded could be used from very simple use cases with only InVM support to very complex cases with clustering, persistence and fail over.

Example step-by-step

To run the example, simply type ./build.sh (or build.bat on windows) from this directory

In this we don't use any configuration files. (Everything is embedded). We simply instantiate ConfigurationImpl, HornetQServer, start it and operate on JMS regularly


  1. The example is starting the server remotely.
  2.            process = startRemoteEmbedded();
            
  3. On EmbeddedServer: Create the Configuration, and set the properties accordingly
  4.            Configuration configuration = new ConfigurationImpl();
               configuration.setEnablePersistence(false);
               configuration.setSecurityEnabled(false);
            
  5. On EmbeddedServer: Create and start the server
  6.            HornetQServer server = HornetQ.newHornetQServer(configuration);
               server.start();
            
  7. As we are not using a JNDI environment we instantiate the objects directly
  8.            ServerLocator serverLocator = HornetQClient.createServerLocatorWithoutHA(new TransportConfiguration(NettyConnectorFactory.class.getName()));
               ClientSessionFactory sf = serverLocator.createSessionFactory();
            
  9. Create a Core Queue
  10.            ClientSession coreSession = sf.createSession(false, false, false);
               final String queueName = "queue.exampleQueue";
               coreSession.createQueue(queueName, queueName, true);
               coreSession.close();
            
  11. Create the session and producer
  12.            session = sf.createSession();
                                       
               ClientProducer producer = session.createProducer(queueName);
            
  13. Create and send a Message
  14.            ClientMessage message = session.createMessage(false);
               message.putStringProperty(propName, "Hello sent at " + new Date());
               System.out.println("Sending the message.");
               producer.send(message);
            
  15. Create the message consumer and start the connection
  16.            ClientConsumer messageConsumer = session.createConsumer(queueName);
               session.start();
            
  17. Receive the message
  18.            ClientMessage messageReceived = messageConsumer.receive(1000);
               System.out.println("Received TextMessage:" + messageReceived.getProperty(propName));
            
  19. Be sure to close our resources!
  20.            if (sf != null)
               {
                  sf.close();
               }
            
  21. Stop the server
  22.            process.destroy();