This example will show how to run JBoss Messaging in JBoss AS (Application Server).
The example application will invoke an EJB which will (1) send a JMS message, (2) update a database from
the same transaction.
The example application will then receive the message sent by the EJB.
We will also check that the database has been updated as expected.
To run the example, you need to download JBoss AS 5.x and create a configuration for JBoss Messaging.
The example also requires a database. The instructions are for MySQL. If you use another database, please refer to the database documentation to configure it.
The example makes a copy of the default-with-jbm2 profile so please configure this for the database
Please refer to JBoss Messaging Quickstart guide to install it in JBoss AS 5
This example needs a database supporting XA transactions. The instructions are written for MySQL. Please refer to your database documentation to configure it appropriately.
To use MySQL with JBoss AS 5
$JBOSS_HOME/server/default-with-jbm2/lib/
During the deployment, the example will:
hsqldb-ds.xml
) from the profilemysql-ds.xml
to to the profileMySQL is configured to connect to a jbossdb
database using the jboss
/jboss
credentials
MySQL configuration file defines two DataSources:
DefaultDS
, the default DataSource (used to create a table from the example)XADS
, the XA DataSource. This datasource will be used to send a JMS message and update the table within the same transactionTo deploy and run the server, type ant deploy
from this directory.
Once the second server has started, type ant run
to run the example .
Type ant undeploy
to remove the profile from JBoss AS 5.
EJBExample
SendMessageBean
Let's take a look at EJBClientExample first.
InitialContext initialContext = new InitialContext();
SendMessageService service = (SendMessageService)initialContext.lookup("mdb-example/SendMessageBean/remote");
service.createTable();
sendAndUpdate
method. This method will send a JMS text message (with the text passed in parameter)
and insert a row in the database table with the text and the message's JMS Message ID
service.sendAndUpdate("This is a text message");
We will now consume the JMS message which was sent by the EJB at step 4.
ConnectionFactory cf = (ConnectionFactory)initialContext.lookup("/ConnectionFactory");
Queue queue = (Queue)initialContext.lookup("queue/testQueue");
connection = cf.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageConsumer consumer = session.createConsumer(queue);
connection.start();
TextMessage messageReceived = (TextMessage)consumer.receive(5000);
System.out.println("Received message: " + messageReceived.getText() +
" (" + messageReceived.getJMSMessageID() + ")");
finally
block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects
finally
{
if (initialContext != null)
{
initialContext.close();
}
if (connection != null)
{
connection.close();
}
}
Let's now take a look at the EJB example
ic = new InitialContext();
java:/JmsXA
)
ConnectionFactory cf = (ConnectionFactory)ic.lookup("java:/JmsXA");
Queue queue = (Queue)ic.lookup("queue/testQueue");
jmsConnection = cf.createConnection();
Session session = jmsConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer messageProducer = session.createProducer(queue);
TextMessage message = session.createTextMessage(text);
messageProducer.send(message);
System.out.println("Sent message: " + message.getText() + "(" + message.getJMSMessageID() + ")");
DataSource ds = (DataSource)ic.lookup("java:/XADS");
jdbcConnection = ds.getConnection();
PreparedStatement pr = jdbcConnection.prepareStatement("INSERT INTO " + TABLE
+ " (id, text) VALUES ('" + message.getJMSMessageID() + "', '" + text + "');");
pr.execute();
pr.close();
finally
block.
finally
{
if (ic != null)
{
ic.close();
}
if (jmsConnection != null)
{
jmsConnection.close();
}
if (jdbcConnection != null)
{
jdbcConnection.close();
}
}