This example demonstrates how to configure several properties on the JBoss Messaging Resource Adapter. We setup two JBoss Servers. The enterprise application is being deployed in one application server while the MDBs and JMS Connections are pointing to a remote server
This example is composed by two MDBs (MDBQueueA and MDBQueueB) and a StatlessSessionBean (StatelessSender). The main class (MDBRemoteClientExample) will call a method on StatelessSender and send a Message to Queue B.
StatelessSender will send a message to Queue A and it is getting the connection out of the JavaConnectionArchitecture (JCA) ConnectionFactory, and sending a message to QueueA which will be received on MDBQueueA.
MDBQueueB is connected to a different JBoss Messaging resource-adapter, and it will receive the message sent by the main Class.
All the MDBs and JMS Connections are referring to the remote server
A Resource Adapter is a way to connect any system provider to an application server, and is integral part of the Java Connectors Architecture specification.
JBossMessaging provides its own adapter and this example will provide you a quick tutorial on how to configure some of the default properties, and how to change the default values on MDB Inbound Properties, or on ConnectionFactory Outbound Properties.
This ResourceAdapter is what provides integration for Message Drive Beans (MDBs) or DataSource integration on the application server.
You can configure the adapter through ActivactionConfigProperties on the MDB. Example:
@MessageDriven(name = "MessageMDBExample",
activationConfig =
{
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
@ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/testQueue"),
@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
@ActivationConfigProperty(propertyName = "ConnectorClassName", propertyValue = "org.jboss.messaging.integration.transports.netty.NettyConnectorFactory"),
@ActivationConfigProperty(propertyName = "ConnectionParameters", propertyValue = "jbm.remoting.netty.port=5545")
})
public class MDBRemoteExample implements MessageListener
MDB Properties can also be defined on XML Files. The JBoss EJB3 Documentation will have more information.
Optionally you could also define the resource adapter you are using. On JBoss EJB3 there is a JBoss specific tag where you can define the Resource-adapter file name:
...
import org.jboss.ejb3.annotation.ResourceAdapter;
@MessageDriven(name = "MessageMDBExample",
activationConfig =
{
...
})
@ResourceAdapter("example-jbm-ra.rar")
public class MDBRemoteExample implements MessageListener
...
In this example however we will configure the default adapter.
Other application servers will provide different ways of binding the resource-adapter.
You can configure ConnectionFactories that you use outside of the MDB context, such as in your SessionBeans. On JBoss Application Server, that could be defined on a datasource deployment file (-ds.xml), using Configuration-properties on the connection factory.
For example, jms-remote-ds.xml
<connection-factories>
<tx-connection-factory>
<jndi-name>RemoteJmsXA</jndi-name>
<xa-transaction/>
<rar-name>jms-ra.rar</rar-name>
<connection-definition>org.jboss.messaging.ra.JBMConnectionFactory</connection-definition>
<config-property name="SessionDefaultType" type="java.lang.String">javax.jms.Topic</config-property>
<config-property name="ConnectorClassName" type="java.lang.String">org.jboss.messaging.integration.transports.netty.NettyConnectorFactory</config-property>
<config-property name="ConnectionParameters" type="java.lang.String">jbm.remoting.netty.port=5545</config-property>
<max-pool-size>20</max-pool-size>
</tx-connection-factory>
</connection-factories>
It is possible to also change global configuration at the JBossMessaging resource adapter. The default installation script will install jboss-messaging resource adapter at $JBOSS_HOME/server/YOUR-SERVER/deploy/jms-ra.rar.
To change these properties, open the ra.xml under jms-ra.rar/META-INF
Example for ra.xml:
<?xml version="1.0" encoding="UTF-8"?>
<connector xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/connector_1_5.xsd"
version="1.5">
...
<resourceadapter>
<resourceadapter-class>org.jboss.messaging.ra.JBMResourceAdapter</resourceadapter-class>
<config-property>
<description>The transport type</description>
<config-property-name>ConnectorClassName</config-property-name>
<config-property-type>java.lang.String</config-property-type>
<config-property-value>org.jboss.messaging.core.remoting.impl.invm.InVMConnectorFactory</config-property-value>
</config-property>
<config-property>
<description>The transport configuration. These values must be in the form of key=val;key=val;</description>
<config-property-name>ConnectionParameters</config-property-name>
<config-property-type>java.lang.String</config-property-type>
<config-property-value>jbm.remoting.invm.serverid=0</config-property-value>
</config-property>
<config-property>
<description>Use XA methods to obtain connections?</description>
<config-property-name>UseXA</config-property-name>
<config-property-type>java.lang.Boolean</config-property-type>
<config-property-value>true</config-property-value>
</config-property>
<config-property>
<description>The user name used to login to the JMS server</description>
<config-property-name>UserName</config-property-name>
<config-property-type>java.lang.String</config-property-type>
<config-property-value></config-property-value>
</config-property>
...
Refer to the Resource Adapter Chapter on the JBoss Messaging Documentation for more information about configuring the ra.xml properties.
You may choose to deploy multiple JBossMessaging Resource adapters on the same application server, for example if you are connecting to different JBM servers and if you wish to have a higher control of properties on each server you are connecting to. You will be able to determine which rar you are using individually at each MDB and ConnectionFactory as specified before here. Just copy the directory jms-ra.rar in your application as any other name with the extension .rar and use that file name at your deployments.
Please refer to JBoss Messaging Quickstart guide to install it in JBoss AS 5
To deploy and start the first server, simply type ant deploy
from the example directory
After the first server has started start the second server, simply type ant deploy2
from the example directory
To run the example, simply type ant run
from the example directory
To undeploy the example, simply type ant undeploy undeploy2
from the example directory
** make sure that JBOSS_HOME is set to the JBoss installation directory
jndi.properties
file in the directory config
initialContext = new InitialContext();
StatelessSenderService sender = (StatelessSenderService)initialContext.lookup("mdb-example/StatelessSender/remote");
sender.sendHello("Hello there MDB!");
JBossQueue destQueueA = new JBossQueue("A");
JBossQueue destQueueB = new JBossQueue("B");
Connection conn = connectionFactory.createConnection("guest", "guest");
Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer prodA = sess.createProducer(destQueueA);
prod.send(sess.createTextMessage(message));
MessageProducer prodB = sess.createProducer(destQueueB);
prodB.send(sess.createTextMessage(message));
conn.close();
@MessageDriven(name = "MDB_QueueA",
activationConfig =
{
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
@ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/A"),
@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"))
})
public class MDBQueueA implements MessageListener
{
public void onMessage(Message message).....
@MessageDriven(name = "MDB_QueueB",
activationConfig =
{
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
@ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/B"),
@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge")
})
@ResourceAdapter("example-jbm-ra.rar")
public class MDBQueueB implements MessageListener
{
public void onMessage(Message message).....