Embedded Example


This examples shows how to setup and run JBoss Messaging embedded with remote clients connecting.

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

We have limited the server classpath on this example:

  1. jbm-core.jar
  2. jbm-transports.jar
  3. netty.jar

Similarly we have also limited the classpath on client:

  1. jbm-core-client.jar
  2. jbm-transports.jar
  3. netty.jar

JBoss Messaging 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 ant from this directory

In this we don't use any configuration files. (Everything is embedded). We simply instantiate ConfigurationImpl, MessagingServer, 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.            MessagingServer server = Messaging.newMessagingServer(configuration);
               server.start();
  7. As we are not using a JNDI environment we instantiate the objects directly
  8.            ClientSessionFactory sf = new ClientSessionFactoryImpl (new TransportConfiguration(NettyConnectorFactory.class.getName()));
            
  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.createClientMessage(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.            
               finally
               {
                  if (connection != null)
                  {
                     connection.close();
                  }
               }
            
  21. Stop the server
  22.            
               process.destroy();