Chapter 41. Embedding JBoss Messaging

JBoss Messaging is designed as set of simple Plain Old Java Objects (POJOs). This means JBoss Messaging can be instantiated and run in any dependency injection framework such as JBoss Microcontainer, Spring or Google Guice. It also means that if you have an application that could use messaging functionality internally, then it can directly instantiate JBoss Messaging clients and servers in its own application code to perform that functionality. We call this embedding JBoss Messaging.

Examples of applications that might want to do this include any application that needs very high performance, transactional, persistent messaging but doesn't want the hassle of writing it all from scratch.

Embedding JBM can be done in very few easy steps. Instantiate the configuration object, instantiate the server, start it, and you have a JBoss Messaging running in your virtual machine. It's as simple and easy as that.

41.1. POJO instantiation

You can follow this step-by-step guide:

Create the configuration object - this contains configuration information for a JBoss Messaging server. If you want to configure it from a file on the classpath, use FileConfigurationImpl

import org.jboss.messaging.core.config.Configuration;
import org.jboss.messaging.core.config.impl.FileConfiguration;

...


Configuration config = new FileConfiguration();
config.setConfigurationUrl(urlToYourconfigfile);
config.start();

If you don't need to support a configuration file, just use ConfigurationImpl and change the config parameters accordingly, such as adding acceptors.

The acceptors are configured through ConfigurationImpl. Just add the NettyAcceptorFactory on the transports the same way you would through the main configuration file.

import org.jboss.messaging.core.config.Configuration;
import org.jboss.messaging.core.config.impl.ConfigurationImpl;

...

Configuration config = new ConfigurationImpl();
HashSet<TransportConfiguration> transports = new HashSet<TransportConfiguration>();
      
transports.add(new TransportConfiguration(NettyAcceptorFactory.class.getName()));
transports.add(new TransportConfiguration(InVMAcceptorFactory.class.getName()));

config.setAcceptorConfigurations(transports);

You need to instantiate and start JBoss Messaging server. The class org.jboss.messaging.core.serverMessaging has a few static methods for creating servers with common configurations.

import org.jboss.messaging.core.server.Messaging;
import org.jboss.messaging.core.server.MessagingServer;

...

MessagingServer server = Messaging.newMessagingServer(config);

server.start();

You also have the option of instantiating MessagingServerImpl directly:

MessagingServer server = 
        new MessagingServerImpl(config);
server.start();

41.2. Dependency Frameworks

You may also choose to use a dependency injection framework such as JBoss Micro Container™ or Spring Framework™.

JBoss Messaging standalone uses JBoss Micro Container as the injection framework. JBMBootstrapServer and jbm-jboss-beans.xml which are part of the JBoss Messaging distribution provide a very complete implementation of what's needed to bootstrap the server using JBoss Micro Container.

When using JBoss Micro Container, you need to provide a XML declaring the MessagingServer and Configuration object, you can also inject a security manager and a MBean server if you want, but those are optional.

A very basic XML Bean declaration for the JBoss Micro Container would be:

<?xml version="1.0" encoding="UTF-8"?>

<deployment xmlns="urn:jboss:bean-deployer:2.0">
   
   <!-- The core configuration -->
   <bean name="Configuration" 
         class="org.jboss.messaging.core.config.impl.FileConfiguration">
   </bean>

   	<!-- The core server -->
   <bean name="MessagingServer" 
         class="org.jboss.messaging.core.server.impl.MessagingServerImpl">      
      <constructor>
         <parameter>
            <inject bean="Configuration"/>
         </parameter>            
      </constructor>         
   </bean>
   </deployment>

JBMBootstrapServer provides an easy encapsulation of JBoss Micro Container.

JBMBootstrapServer bootStrap = 
        new JBMBootstrapServer(new String[] {"jbm-jboss-beans.xml"});
        bootStrap.run();

41.3. Connecting to the Embedded JBoss Messaging

To connect clients to JBoss Messaging you just create the factories as normal:

41.3.1. Core API

If using the core API, just create the ClientSessionFactory and use the regular core API.

ClientSessionFactory nettyFactory =  new ClientSessionFactoryImpl(
                                        new TransportConfiguration(
                                           InVMConnectorFactory.class.getName()));

ClientSession session = factory.createSession();

session.createQueue("example", "example", true);

ClientProducer producer = session.createProducer("example");

ClientMessage message = session.createClientMessage(true);

message.getBody().writeString("Hello");

producer.send(message);

session.start();

ClientConsumer consumer = session.createConsumer("example");

ClientMessage msgReceived = consumer.receive();

System.out.println("message = " + msgReceived.getBody().readString());

session.close();

41.3.2. JMS API

Connection on an Embedded JBoss Messaging through JMS is also simple. Just instantiate JBossConnectionFactory directly. The following example illustrates that.

JBossConnectionFactory cf = 
    new JBossConnectionFactory(
       new TransportConfiguration(InVMConnectorFactory.class.getName()));

Connection conn = cf.createConnection();

conn.start();

Session sess = conn.createSession(true, Session.SESSION_TRANSACTED);

MessageProducer prod = sess.createProducer(queue);

TextMessage msg = sess.createTextMessage("Hello!");

prod.send(msg);

sess.commit();

MessageConsumer consumer = sess.createConsumer(queue);

TextMessage txtmsg = (TextMessage)consumer.receive();

System.out.println("Msg = " + txtmsg.getText());

sess.commit();

conn.close();

41.4. JMS Embedding Example

Please see Section 9.2.1, “Embedded” for an example which shows how to setup and run JBoss Messaging embedded with JMS.