Embedded Example


This examples shows how to setup and run JBoss Messaging embedded.

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

On this example, we are only using one jar (jbm-core.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. Create the Configuration, and set the properties accordingly
  2.          Configuration configuration = new ConfigurationImpl();
             configuration.setEnablePersistence(false);
             configuration.setSecurityEnabled(false);
             configuration.getAcceptorConfigurations().add(new TransportConfiguration(InVMAcceptorFactory.class.getName()));
             
  3. Create and start the server
  4.            MessagingServer server = Messaging.newMessagingServer(configuration);
               server.start();
  5. As we are not using a JNDI environment we instantiate the objects directly
  6.            ClientSessionFactory sf = new ClientSessionFactoryImpl (new TransportConfiguration(InVMConnectorFactory.class.getName()));
            
  7. Create a Core Queue
  8.            
             ClientSession coreSession = sf.createSession(false, false, false);
             final String queueName = "queue.exampleQueue";
             coreSession.createQueue(queueName, queueName, true);
             coreSession.close();
            
  9. Create the session and producer
  10.            
                session = sf.createSession();
                                       
                ClientProducer producer = session.createProducer(queueName);
                
            
  11. Create and send a Message
  12. 
                ClientMessage message = session.createClientMessage(false);
                message.putStringProperty(propName, "Hello sent at " + new Date());
                System.out.println("Sending the message.");
                producer.send(message);
           
  13. Create the message consumer and start the connection
  14. 
                ClientConsumer messageConsumer = session.createConsumer(queueName);
                session.start();
  15. Receive the message
  16. 
                ClientMessage messageReceived = messageConsumer.receive(1000);
                System.out.println("Received TextMessage:" + messageReceived.getProperty(propName));
  17. Be sure to close our resources!
  18.            
               finally
               {
                  if (connection != null)
                  {
                     connection.close();
                  }
               }
            
  19. Stop the server
  20.            
               server.stop();