1 2 3 4 Previous Next 48 Replies Latest reply on Apr 10, 2011 1:28 PM by blabno Go to original post
      • 45. Re: JMS MessageListener in a web application
        dan.j.allen

        Yeeeeaaaaaah! Finally! Happy messaging ;)

        • 46. Re: JMS MessageListener in a web application
          sathishavunoori
          hi Dan,
          according to your code for jms working fine . i am very thankful to you for that.


          i have configured seam interceptors in ejb-jar.xml and set @AutoCreate to my application scoped seam component but it was not working when i injecting that component into my MDB with @In(create=true). is there any alternative?
          if i ask seam for a component between lifecycle methods its working fine.

          thanks for your advise .
          • 47. Re: JMS MessageListener in a web application
            blabno

            Hi guys, I'm trying to add JMS to my application, but don't want to use MDB (no ejb).
            I've come up to the point when messages are being sent and read, but I have no clue how to rollback the transaction and put message back into the queue.
            I've configured my datasource to be XA, but that doesn't help. I'm trying to thrown exceptions in message listener or rolling back the transaction manually but without success. Do you have any ideas what am I doing wrong?


            Here is message sender:


            @Name("mailman")
            public class Mailman {
            // ------------------------------ FIELDS ------------------------------
            
                @In(create = true)
                private QueueSender mailQueueSender;
            
                @In(create = true)
                private QueueSession queueSession;
            
            // -------------------------- OTHER METHODS --------------------------
            
                public void send() throws JMSException {
                    final HashMap map = new HashMap();
                    map.put("time",System.currentTimeMillis());
                    mailQueueSender.send(queueSession.createObjectMessage(map));
                }
            }



            Here is the message reciver:


            @Startup
            @Scope(ScopeType.APPLICATION)
            @Name("logger")
            public class LoggerBean implements MessageListener {
            
                @Logger
                private Log log;
            
                private QueueSession queueSession;
            
                @Create
                public void init() {
                    try {
                        Queue queue = getQueue();
                        queueSession = QueueConnection.instance().createQueueSession(true, Session.SESSION_TRANSACTED);
                        queueSession.createReceiver(queue).setMessageListener((MessageListener) Component.getInstance(LoggerBean.class));
                    } catch (JMSException e) {
                        e.printStackTrace();
                    } catch (NamingException e) {
                        e.printStackTrace();
                    }
                }
            
                private Queue getQueue() throws NamingException {
                    InitialContext ctx = new InitialContext();
                    Queue queue = (Queue) ctx.lookup("queue/realestateMailQueue");
                    return queue;
                }
            
                @Transactional
                public void onMessage(Message msg) {
                    Lifecycle.beginCall();
                    final UserTransaction userTransaction = Transaction.instance();
                    try {
                        if(!userTransaction.isActive()) {
                            userTransaction.begin();
                        }
                        try {
                            log.info(((ObjectMessage) msg).getObject());
                        } catch (JMSException e) {
                            e.printStackTrace();
                        }
                        if(userTransaction.isActive()) {
                            userTransaction.setRollbackOnly();
                        }
                    } catch (NotSupportedException e) {
                        e.printStackTrace();
                    } catch (SystemException e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            if(userTransaction.isMarkedRollback()) {
                                userTransaction.rollback();
                            }
                        } catch (SystemException e) {
                            e.printStackTrace();
                        }
                        Lifecycle.endCall();
                    }
            
                }
            
                @Destroy
                public void destroy() throws JMSException {
                    queueSession.close();
                }
            
            }




            Could it be a problem that i'm opening the queueSession and not closing it after reading message?

            • 48. Re: JMS MessageListener in a web application
              blabno

              I've experimented a lot and I've discovered that transaction must be started before queueSession is created.
              This means that if I want to take advantage of having each message processed in separate transaction then I have to open and close the session each time i want to read the message. Also reading should be done using queueSession.createReceiver(getQueue()).receiveNoWait() instead of queueSession.createReceiver(queue).setMessageListener.

              1 2 3 4 Previous Next