Wednesday, December 28, 2011

JBoss AS7.1 - My Episode 1 - Learn about JMS 1.1 and EJB 3.1 Message-Driven Bean

Before we start with this episode, users need to know the difference between JBoss Community project and JBoss Enterprise Middleware Products.
JBoss AS7.1 community project driven by Red Hat
Implemented the JEE 6 specification, without ASL-based support, with the latest innovations, used by developers for testing and integrating middleware capacities and well suited for early to advanced pro typing and development
JBoss Enterprise Application Platform 6.0 product will be provided by Red Hat
Integrated, tested and certified Enterprise Platform. Subscription of such a product includes patches, updates, SLA-based support, multi-year maintenance policies, and also a Red Hat Open Source Assurance.
This a Best of both worlds - Rapid innovation, with long-term stability, supportability and maintainability
What is it?
This episode will show you and demonstrate the use of JMS 1.1 and EJB 3.1 Message-Driven Bean deployed in the JBoss Application Server 7.1
System requirements
To build successful this episode you need following softwares to be installed:
NOTE: This episode will use the already default configured connection factory named "InVmConnectionFactory" with the jndi "java:/ConnectionFactory" and a queue named "testQueue" with the jndi "queue/test". The artifacts will come from the JBoss Community Maven repository, a superset of the Maven central repository.
Code for this episode can be downloaded from here https://github.com/jbossas/quickstart/tree/master/helloworld-mdb
With the prerequisites out of the way, you're ready to build and deploy.
Application Structure
Our application consists of the following components:
  • HelloWorldMDBServletClient: a servlet 3.0 application acts as client that sends several messages to a defined queue
  • HelloWorldMDB: a message-driven bean that asynchronously receives and processes the messages that are sent to the queue by the servlet application
HelleworldMDBServletClient Servlet 3.0 class
this servlet application acts as client application and send several messages to a defined queue named "testQueue", that the message-driven bean application HelloWorldMDB listens to. Required components for sending messages are injection of the connection factory and the queue resources:
@Resource(mappedName = "java:/ConnectionFactory")
private static ConnectionFactory connectionFactory;
@Resource(mappedName = "java:/queue/test")
private static Queue queue;
the next action items will be to create connection, session and message produces in the servlet method doGet()
connection = connectionFactory.createConnection();
Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
MessageProducer messageProducer = session.createProducer(queue);
Now we are ready to send several messages
TextMessage message = session.createTextMessage();
for (int i = 0; i < MSG_COUNT; i++) {
message.setText("This is message " + (i + 1));
messageProducer.send(message);
out.write("Message ("+i+"): " + message.getText() +"
");
}
Message-Driven Bean class
The HelloWorldMDB is a simple message-driven bean class uses the annotation @MessageDriven to define some very important properties like the "destinationType" and "destination", …
@MessageDriven(name = "HelloWorldMDB", activationConfig = {
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
@ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/test"),
@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge")
}
)
public class HelloWorldMDB implements MessageListener {
The next action item will be to write the logic in the onMessage method. This method is invoked by the EJB container when the queue receives a message. The method onMessage is used for JMS API and is a method of the MessageListener Interface.
The onMessage method casts the incoming message to a TestMessage to display the text as String
public void onMessage(Message rcvMessage) {
TextMessage msg = null;
try {
if (rcvMessage instanceof TextMessage) {
msg = (TextMessage) rcvMessage;
LOGGER.info("Received Message: " + msg.getText());
} else {
LOGGER.warning("Message of wrong type: "
+ rcvMessage.getClass().getName());
}
} catch (JMSException e) {
throw new RuntimeException(e);
}
}
Deploying the application
First of all you need to enable the "admin" user from $JBOSS_HOME/standalone/configuration/mgmt-users.properties file, and then start JBoss AS 7.1.x. by running this script
$JBOSS_HOME/bin/standalone.sh
or if you are using windows
$JBOSS_HOME/bin/standalone.bat
To deploy the application, you first need to produce the archive to deploy using the following Maven goal:
mvn package
You can now deploy the artifact to JBoss AS by executing the following command:

mvn jboss-as:deploy
This will deploy target/jboss-as-helloworld-mdb.war.
The application will be running at the following URL http://localhost:8080/jboss-as-helloworld-mdb/HelloWorldMDBServletClient.
Go to the JBoss Application Server console or Server log and the result can look like this:
15:42:35,453 INFO class org.jboss.as.quickstarts.mdb.HelloWorldMDB) Received Message: This is message 1
15:42:35,455 INFO class org.jboss.as.quickstarts.mdb.HelloWorldMDB) Received Message: This is message 2
15:42:35,457 INFO class org.jboss.as.quickstarts.mdb.HelloWorldMDB) Received Message: This is message 3
15:42:35,478 INFO class org.jboss.as.quickstarts.mdb.HelloWorldMDB) Received Message: This is message 5
15:42:35,481 INFO class org.jboss.as.quickstarts.mdb.HelloWorldMDB) Received Message: This is message 4

To undeploy from JBoss AS, run this command:
mvn jboss-as:undeploy
You can also start JBoss AS 7 and deploy the project using Eclipse. See the JBoss AS 7 Getting Started Guide for Developers for more information.

2 comments:

  1. I appreciate for your post, and learn much from it. It’s really an excellent work. I really enjoy reading most of your articles and your thoughts about JBoss Developers. JBoss is the most widely used open source application server on the market.

    ReplyDelete
  2. Hello. Thank you for your example.
    I have a question: we know, that queue has just one consumer, but I don't see, that by sending a message you write something like: msg.setReceiver(HelloWorldMDB). If I write one more receiver HelloWorldMDB_2, how do our queue know which one of both listeners should get a message?
    Thank you!

    ReplyDelete