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 only one jars (jbm-core.jar, jbm-jms.jar and jboss-javaee.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.            Queue queue = new JBossQueue("exampleQueue");
               JBossConnectionFactory cf = new JBossConnectionFactory (new TransportConfiguration(InVMConnectorFactory.class.getName()));
            
  7. Create a JMS Destination by using the Core API
  8.            
             ClientSession coreSession = cf.getCoreFactory().createSession(false, false, false);
             coreSession.createQueue("jms.queue.exampleQueue", "jms.queue.exampleQueue", true);
             coreSession.close();
            
  9. Create the JMS objects
  10.            
                connection = cf.createConnection();
                Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
                MessageProducer producer = session.createProducer(queue);
            
  11. Create and send a TextMessage
  12. 
                TextMessage message = session.createTextMessage("Hello sent at " + new Date());
                System.out.println("Sending the message.");
                producer.send(message);
  13. Create the message consumer and start the connection
  14. 
                MessageConsumer messageConsumer = session.createConsumer(queue);
                connection.start();
  15. Receive the message
  16. 
                TextMessage messageReceived = (TextMessage)messageConsumer.receive(1000);
                System.out.println("Received TextMessage:" + messageReceived.getText());
  17. Be sure to close our resources!
  18.            
               finally
               {
                  if (connection != null)
                  {
                     connection.close();
                  }
               }
            
  19. Stop the server
  20.            
               server.stop();