DZone spoke with Gavin King, JBoss Fellow at Red Hat, earlier this week to discuss the newly ratified JSR 299 (Contexts and Dependency Injection) specification as well as Java EE 6 (JSR 316), both of which passed their final approval ballots on Nov 30th, 2009. In this exclusive interview, Gavin describes the evolution of the CDI specification (formerly known as Web Beans) and describes some of its primary goals. He also talks about the several CDI implementations currently on the market, including JBoss’ own Weld implementation, and also highlights some of the benefits of running Weld can inside a servlet container, or in a Java SE environment. Finally, Gavin previews what we can expect to see in Seam 3, and points out the vast improvements that have been made in JSF 2 — he also encourages JSF skeptics to “take a second look.”
In my previous post, JMS Messaging with Seam, I showed you how to setup a queue and send a message to that queue using Seam. Now it is time to read that message from the queue and do something with it. As you guessed, it is very simple to do when using Seam and EJB 3.0.
The code below shows how to read a text message from the queue.
If you followed the previous post you should see the “Seam ROCKS!” message.
That’s great but in the real world you will probably be working with objects and not simple text messages. Don’t worry. With Seam and EJB 3.0 this is also a breeze.
Lets assume in the previous post I sent a Person object to the queue instead of a text message. The code below shows you how to read the Person object from the queue.
try{
ObjectMessage objectMessage =(ObjectMessage)message;
Person person =(Person) objectMessage.getObject(); // TODO Do something with message System.out.println("Person = "+ person.getSurName()+", "+ person.getGivenName()); }catch(JMSException e){ // TODO Auto-generated catch block
e.printStackTrace(); } }
The first you need to do is configure JBoss to create the queue on startup. NOTE: Seam will not create the queue. If you forget to do this Seam will complain about not being able to bind to the queue.
Create an xml file in your JBoss AS deploy directory. I named mine ACHS-jms-destinations-service.xml.
Now start JBoss AS and check to see if your queue has been created. To check open up the JBoss Web Console(http://localhost:8080/web-console). Click on System -> JMX MBeans and scroll down to jboss.messaging.destination. You should see your newly created queue. In my example below it is named achsSearch.
Now that we have our queue created we need to configure Seam to use the queue.
The following 2 lines need to be placed in the compenents.xml file.
The first line tells Seams where to find the queue and creates a Seam compenent called searchQueueSender. You can name this whatever you like.
The second line tells Seam to use the connection factor that supports JTA. This enables it to participate in transactions. Pretty sweet. The other connection factories (e.g. ConnectionFactory) only support local transactions.
Believe it or not that is all of the configuration that is needed. Now to coding.
Below is an action class that simply submits a text message to the queue. The big gotcha I ran into here is that Seam also has a QueueSession library and Eclipse imported that one when I tried to fix the missing imports. I had to add the javax.jms libraries to my classpath before I could import the 2 correct javax.jms libraries needed.