Embedded JMS Server Example

This examples shows how to setup and run an embedded JMS server using HornetQ.

HornetQ was designed using POJOs (Plain Old Java Objects) which means embedding HornetQ in your own application is as simple as instantiating a few objects.

This example does not use any configuration files. The server is configured using POJOs and can be easily ported to any dependency injection framework.
We will setup and run a full-fledged JMS server which binds its JMS resources to JNDI and can be accessed by remote clients.

Example step-by-step

To run the example, simply type mvn verify from this directory

  1. Create HornetQ core configuration, and set the properties accordingly
  2.             Configuration configuration = new ConfigurationImpl();
                configuration.setPersistenceEnabled(false);
                configuration.setSecurityEnabled(false);
                configuration.getAcceptorConfigurations().add(new TransportConfiguration(NettyAcceptorFactory.class.getName()));
                Configuration configuration = new ConfigurationImpl();
  3. Create the HornetQ core server
  4.             HornetQServer hornetqServer = HornetQ.newHornetQServer(configuration);
             
  5. Create and start the JNDI server (using JBoss JNDI implementation)
  6.             System.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
                NamingBeanImpl naming = new NamingBeanImpl();
                naming.start();
                Main jndiServer = new Main();
                jndiServer.setNamingInfo(naming);
                jndiServer.setPort(1099);
                jndiServer.setBindAddress("localhost");
                jndiServer.setRmiPort(1098);
                jndiServer.setRmiBindAddress("localhost");         
                jndiServer.start();
             
  7. Create the JMS configuration
  8.             JMSConfiguration jmsConfig = new JMSConfigurationImpl();
             
  9. Configure context used to bind the JMS resources to JNDI
  10.             Hashtable<String, String> env = new Hashtable<String, String>();
                env.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
                env.put("java.naming.provider.url", "jnp://localhost:1099");
                env.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
                Context context = new InitialContext(env);
                jmsConfig.setContext(context);
             
  11. Configure the JMS ConnectionFactory
  12.             TransportConfiguration connectorConfig = new TransportConfiguration(NettyConnectorFactory.class.getName());
                ConnectionFactoryConfiguration cfConfig = new ConnectionFactoryConfigurationImpl("cf", connectorConfig, "/cf");
                jmsConfig.getConnectionFactoryConfigurations().add(cfConfig);
             
  13. Configure the JMS Queue
  14.             QueueConfiguration queueConfig = new QueueConfigurationImpl("queue1", null, false, "/queue/queue1");
                jmsConfig.getQueueConfigurations().add(queueConfig);
             
  15. Start the JMS Server using the HornetQ core server and the JMS configuration
  16.             JMSServerManager jmsServer = new JMSServerManagerImpl(hornetqServer, jmsConfig);
                jmsServer.start();
             

    At this point the JMS server is started and any JMS clients can look up JMS resources from JNDI to send/receive messages from the server. To keep the example simple, we will send and receive a JMS message from the same JVM used to run the JMS server.

  17. Lookup JMS resources defined in the configuration
  18.             ConnectionFactory cf = (ConnectionFactory)context.lookup("/cf");
                Queue queue = (Queue)context.lookup("/queue/queue1");
             
  19. Send and receive a message using JMS API
  20. See the Queue Example for detailed steps to send and receive a JMS message

    Finally, we stop the JMS server and its associated resources.

  21. Stop the JMS server
  22.             jmsServer.stop();
             
  23. Stop the JNDI server
  24.             naming.stop();
                jndiServer.stop();