JMS Messaging with Seam

Seam makes it easy to send and receive JMS messages to and from Seam components. In this post I am going to show you how to configure JBoss Messaging and Seam to send a message to a JMS Queue.

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.

1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="UTF-8"?>
<server>
   <mbean code="org.jboss.jms.server.destination.QueueService"
     name="jboss.messaging.destination:service=Queue,name=achsSearch"
     xmbean-dd="xmdesc/Queue-xmbean.xml">
      <depends optional-attribute-name="ServerPeer">jboss.messaging:service=ServerPeer</depends>
      <depends>jboss.messaging:service=PostOffice</depends>
   </mbean>
</server>
  • 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.

screenhunter_05-2009-08-07

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.

1
2
3
<jms:managed-queue-sender name="searchQueueSender" auto-create="true" queue-jndi-name="/queue/achsSearch" />

<jms:queue-connection queue-connection-factory-jndi-name="java:/JmsXA" />

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//Package Statement

//Be sure and use the javax libraries and not Seam Queue libraries
import javax.jms.QueueSender;
import javax.jms.QueueSession;

//Other Import Statements

@Name("newSearch")
@Scope(ScopeType.CONVERSATION)
public class NewSearch {
  @In
  private QueueSender searchQueueSender;
  @In(create = true)
  private QueueSession queueSession;

  public void submitSearch() {
    try {
      //Send message to queue
      searchQueueSender.send(queueSession.createTextMessage("Seam ROCKS!"));
    } catch (Exception e) {
      log.error(e.getMessage());
    }
  }
}
  • To verify that your message arrived you can check the JBoss Web Console by clicking on your Queue and looking at the MessageCount field.


Believe or not that is it. I ran into a few gotchas but overall pretty easy and very cool. Seam makes JMS a breeze.



Comments

  1. Elvis December 18th

    Comment Arrow

    Quite cool there man. I’ve been using SEAM for a while now but still a newbie with JMS and messaging. Your snappy but spicy article gives me the anxiety to hit hard.
    Hopep it would work for me.


  2. Brian December 18th

    Comment Arrow

    Elvis, Messaging in Seam is a breeze. Good luck.


  3. sathish January 16th

    Comment Arrow

    hi i want to implement a JMS message driven bean in a stateless session bean. how would i get this? can anyone help me.

    thanks in advance.


  4. Brian January 16th

    Comment Arrow

    Message driven beans are by default stateless. They are their own type of bean and can’t be a session bean. EJB3 has 3 types of beans:

    1. Session Bean
    2. Entity Bean
    3. Message Driven Bean

  5. sathish January 18th

    Comment Arrow

    thanks,
    i would like to send a list object to a queue and get that list object from queue in a xhtml file to view those list objects. how can i do this in seam? can i expect from you?


  6. sathish January 22nd

    Comment Arrow

    hi brian ,
    i created an MDB and can able to send messages to that queue from an application scoped component. its working file. i wanted to receive acknoledgments from that queue in another queue. and i want to access those acknoledgments from newly created queue in my app scoped component. how would i get this?
    can you suggest me?

    thanks in advance


  7. Brian January 27th

    Comment Arrow

    Sathish – Check out publish and subscribe messaging. I think that would work for you are trying to do. You should be able to find some information in the JBoss messaging documentation.


  8. sathish January 27th

    Comment Arrow

    ya thanks for your replay brian.
    i have cleared my doubts that sending and receiving messages using two queues. its working fine thanks again.

    i need to configure mail-ra.rar to one of my queue for receiving mails. i have tried this as follows ,
    package org.domain.DigesterDemo.session;

    import javax.ejb.ActivationConfigProperty;
    import javax.ejb.MessageDriven;
    import javax.mail.Message;
    import javax.mail.MessagingException;

    import org.jboss.annotation.ejb.ResourceAdapter;
    import org.jboss.resource.adapter.mail.inflow.MailListener;
    import org.jboss.seam.annotations.Name;

    @MessageDriven(activationConfig = {@ActivationConfigProperty(propertyName=”mailServer”, propertyValue=”imap.gmail.com”),
    @ActivationConfigProperty(propertyName=”mailFolder”, propertyValue=”INBOX”),
    @ActivationConfigProperty(propertyName=”storeProtocol”, propertyValue=”imap”),
    @ActivationConfigProperty(propertyName=”pollingInterval”, propertyValue=”70000″),
    @ActivationConfigProperty(propertyName=”userName”, propertyValue=”infyzTest@gmail.com”),
    @ActivationConfigProperty(propertyName=”password”, propertyValue=”testinfyz”),
    @ActivationConfigProperty(propertyName = “flush”, propertyValue = “true”)})
    @ResourceAdapter(“mail-ra.rar”)
    @Name(“mailInbox”)
    public class MailInbox implements MailListener
    {
    public void onMessage(Message message)
    {
    System.out.println(“mail received…..”);
    try {
    System.out.println(“The msg has been recieveed #0″+message.getSubject());
    } catch (MessagingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

    }
    but am getting error like
    failed to execute foldercheck
    can u please describe the process to configure mail-ra.rar in right way?

    thanks in advance


  9. sam April 28th

    Comment Arrow

    hi brain i did all all steps u specified. getting the error at @name, @scop saying that can not resolved .i have added javax libraries also. one more error for
    saying that prefix jms not found. please help.


  10. sam April 28th

    Comment Arrow

    is that a component.xml file ? the other error i am getting at adding those two lines in .xml file at <jms:managed-queue-sender…
    saying that prefix jms not found.


  11. Brian April 30th

    Comment Arrow

    @Sam – You need to import

    import org.jboss.seam.annotations.Name;
    import org.jboss.seam.annotations.Scope;

    Those 2 lines are added to the components.xml file. That is not the complete components.xml file. Add the following to the top of the components.xml file and the namespace will be recognized.

    xmlns:jms=”http://jboss.com/products/seam/jms” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”

    Are you using Ecliplse IDE or any other IDE? It would really help you with these library issues.


  12. sam April 30th

    Comment Arrow

    Thanks for reply. i am using eclips getting few error while deploying it will try to solve it. otherwise will ask you.
    have one more question does queue service persist data in DB ? bcz if we restart the server the queue will get empty ,right so is there any way to persist the messages from queue to DB?


  13. sam April 30th

    Comment Arrow

    how to get the SERVERPEERID from messaging-service. XML file ? do i need parsing the XML file? or is there any API those helps in the same to retrieve the same. Actually my requirement is to get generate the unique id on different JBOS servers in cluster manner.


  14. Brian April 30th

    Comment Arrow

    By default JBOSS will persist the queue to the file system so the messages are persisted. You can change it to persist to a database. You will need to look at the JBOSS messaging documentation. Off the top of my head I believe it was pretty straightforward to configure it.

    As far as the SERVERPEERID, I have no idea. If I was you I would check the JBOSS Messaging or JBOSS AS forums .


  15. sam May 5th

    Comment Arrow

    thnks


  16. Brian May 5th

    Comment Arrow

    You are welcome Sam.


  17. Roland May 17th

    Comment Arrow

    Thanks for the great tutorial Brian. Being totally new to J2EE I tried to get your example to work.

    I just started a standard-seam-project, put your NewSearch.java file into the src/hot folder and added following to the home.xhtml :

    Just in order to see, whether the messageCount-value ascends, but it doesn’t. JBoss 5.1 does not throw an exception. (Could not get queue into JBoss 6, that is why I switched to 5.1)

    Are you able to pinpoint the error ? Did I forget to declare the NewSearch-class as a managedBean ? A little hint would be refreshing indeed :-) Thanks in advance.

    Roland


  18. Roland May 17th

    Comment Arrow

    I added :

    to the home.xhtml (pasting it did not work)


  19. Roland May 17th

    Comment Arrow

    Sorry, I added a commandButton with an action : NewSearch.submitSearch (in brackets)


  20. Brian May 17th

    Comment Arrow

    Roland – Did you get it to work? Looks like your code didn’t come through on your comment.


  21. Roland May 17th

    Comment Arrow

    Hey, yeah, seems like there is some HTML/XML – filter on preventing me to post tags. So I really did not code anything else than adding a button in home.xhtml (created with eclipse wizard) :

    h:commandButton value=”Druecken” action=”#{NewSearch.submitSearch}”/

    Right now : Clicking the button on the page does not trigger the message delivering to the queue. Hmm


  22. Brian May 17th

    Comment Arrow

    @Roland.

    Try this.

    h:commandButton value=”Druecken” action=”#{newSearch.submitSearch}”/

    You have to use the Seam component name and not the Java class name.


  23. Roland May 17th

    Comment Arrow

    Thank you very much for the hint, but now I have another issue :

    jboss-exception : “..error while reading /WEB-INF/components.xml”

    I googled it and suspect that I have to add something to the faces-config.xml regarding the searchQueue. Could it solve the problem ?


  24. Brian May 17th

    Comment Arrow

    You don’t need anything in your faces config file. Do you have the jms namespace defined at the top of your components.xml file?

    xmlns:jms=”http://jboss.com/products/seam/jms”


  25. Roland May 17th

    Comment Arrow

    Okay, my last post a bit confusing…so I switched to JBoss 4.2 now, (cause I had “Datasource not found -issues” with 5.1) did not create an own queue, but used the built in testQueue and sent the message there. But still the counter does not increment :-( no exception are displayed now …


  26. Brian May 17th

    Comment Arrow

    Hmm…JBoss 4.2 uses JBossMQ which is their older messaging system. The configuration I have above is for JBoss Messaging which is their current messaging systems and is the default for JBoss 5 and above.

    You can install JBoss Messaging on JBoss 4. Here are the instructions.

    http://featurenotbug.com/2009/07/installing-jboss-messaging-on-jboss-as-423/

    When you look at the JMX Console like shown above do you see the queue that you created?


  27. Roland May 17th

    Comment Arrow

    Hi,

    as you probably guessed I switched to JBoss 5.0 and now there aren’t any exceptions thrown after deploying the whole. Yes I can see the queue in the domain : jboss.messaging.destination. Hmm, I suspect it has something to do with a false JSF-commandbutton-action ?
    I have : … action =”#{componentname.methodname}”
    Sorry for wasting your time.

    Roland


  28. Brian May 17th

    Comment Arrow

    @Roland – I sent you an email. You aren’t wasting my time. Doing this stuff for the first time can be tricky.


  29. Roland May 17th

    Comment Arrow

    Hi Brian,

    unfortunately my mail-adress was wrong. The was missing a dot :-/ could you please resend it ? Maybe I can send you code, although I did not could much until now.

    Best Regards,

    Roland


  30. samina November 3rd

    Comment Arrow

    Hi brain,
    would like to ask about jbpm, how can i load classes those i defined in workflows (jpdl.xml file) in ” continue=asynch ” condition ?
    i am using jbpm with seam framework.
    What is the time you available here on site, so that i can get a qick response from you.
    thanks:)


  31. Brian November 6th

    Comment Arrow

    samina – I have never used jbpm so I am of no help. Check the Seam forums and you don’t find anything post your question.


  32. MAd November 22nd

    Comment Arrow

    I’ve a problem …
    The Message was not sent…
    i’ve a xtml page with commandbutton SEND
    but when i click on send the method will not invocate

    The NewSearch file is under org.domain.sender.sessionm


  33. Brian November 22nd

    Comment Arrow

    @MAd – You need to make sure your button is inside a form or it won’t fire.


  34. MAd November 22nd

    Comment Arrow

    Tnx
    My problem is seguent ::
    org.jboss.resource.adapter.jms.JmsConnectionFactoryImpl cannot be cast to javax.jms.QueueConnectionFactory


  35. Brian November 22nd

    Comment Arrow

    MAd – What version of JBOSS AS are you running?


  36. MAd November 23rd

    Comment Arrow

    JBOss 5.0.1


  37. MAd November 23rd

    Comment Arrow

    Ok …
    After many attempt …
    Tnx for your Time


  38. Thomas Victor November 5th

    Comment Arrow

    I am trying to use a queue in seam application. It is SEAM 2.2.0.GA application. The queue is starting. I can see messages getting published to the Queue. But I have message driven bean, it is not reading from the queue.


  39. Brian December 11th

    Comment Arrow

    Thomas,

    Follow these instructions to see if you can get your MDB working.

    http://featurenotbug.com/2009/08/message-driven-beans-mdb-using-seam-and-ejb-30/


  40. Thomas Victor December 11th

    Comment Arrow

    Thanks for your reply. My problem was that my app was deployed as war. So it can not support MDB. Finally I ended up with non MDB solution.


  41. Brian December 11th

    Comment Arrow

    Ah…Good catch Thomas. Thanks for the update.


Add Yours

  • Author Avatar

    YOU


Comment Arrow



About Author

Brian

Brian Abston is a Java Developer in Oklahoma who also loves technology.