The example application will invoke an EJB which is deployed within WildFly which will:
The example application will then receive the message sent by the EJB.
The example leverages the Arquillian framework to run a WildFly instance and deploy the MDB.
download WildFly 8.0.0.Final from here and install.
set the JBOSS_HOME property to point to the WildFly install directory
type mvn verify
from the example directory to run
The example code is composed of two main classes:
EJBClientExample
SendMessageBean
Let's take a look at EJBClientExample first.
Properties env = new Properties(); env.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming"); InitialContext initialContext = new InitialContext();
SendMessageService service = (SendMessageService) initialContext.lookup("ejb:/test//SendMessageBean!org.apache.activemq.javaee.example.server.SendMessageService");
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.
env = new Properties(); env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory"); env.put(Context.PROVIDER_URL, "http-remoting://localhost:8080"); initialContext = new InitialContext(env);
ConnectionFactory cf = (ConnectionFactory)initialContext.lookup("jms/RemoteConnectionFactory");
Queue queue = (Queue)initialContext.lookup("jms/queues/testQueue");
connection = cf.createConnection("guest", "password"); 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(); } }