The example application will invoke an EJB which within the JBoss AS (Application Server) which will:
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 HornetQ.
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-hornetq profile so please configure this for the database
Please refer to HornetQ 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-hornetq/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
credentials1.
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, simply type ./build.sh deploy
(or build.bat deploy
on windows) from this directory.
Once the second server has started, simply type ./build.sh run
(or build.bat run
on windows) to run the example .
Simply type ./build.sh undeploy
(or build.bat undeploy
on windows) 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 IDservice.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 objectsfinally { 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(); } }
mysql
create database jbossdb;
GRANT ALL PRIVILEGES ON *.* TO 'jboss'@'%' IDENTIFIED BY 'jboss' WITH GRANT OPTION;
mysql -u jboss -p jbossdb
(and type jboss
when prompted for the password)