5 Replies Latest reply on Dec 20, 2011 4:02 AM by gaohoward

    Transactional Recieve

    gilstr

      Hello

       

      I've two questions regarding using a transactional session in the consumer side:

       

      1. In a case the HornetQ failed to receive the consumer's ack (due to network failure for instance) will the consumer's will get the message, i.e will the consumer.receive() invocation return the message?
      2. Is it concidered as a good practice to process the received message in the consumer's side and only the commit?

       

      Thanks,

       

      Gil

        • 1. Re: Transactional Recieve
          gaohoward

          Question 1: with a transactional session, if a session commit fails, the messages in the transaction will be rolled back and redelivered.

          consumer.receive() will return the message no matter whether the commit is going to fail or not.

           

          Q2: Not sure I understand this. Normally if you want the receiving and processing to be one atomic operation, you need to wrap them into one transaction.

           

          Howard

          • 2. Re: Transactional Recieve
            gilstr

            Hello Howard

             

            Thank you for your answer.

             

            Regarding Q1, if the HornetQ is going to rollback the transaction (for instance, because it didn't get an Ack from the consumer) but the consumer did receive the message (and proceed it afterwards), what's the meaning of rolling back that transaction?

             

            Regarding Q2, suppose that due to the business requirements, every incoming message should be handled by the consumer in its own transaction, it can be represented by the following pseudo code:

             

            consumer.receive()

             

            try {

                 doSomething();

                 msg.acknowledge();

              }

             

              catch (Exception e) {   

            session.rollback();

              }

             

            suppose that I'd like to process (execute doSomething()) multiple method concurently (but in seperate transaction) - should I be using multiple session objects or there is a way to commit (or rollback) a transaction in a message level?

             

            I appreciate your help.


            • 3. Re: Transactional Recieve
              gaohoward

              Hi, I'd like to clarify that with transactions, messages ack is done in the transaction commit. You shouldn't call message.ack().

               

              Re Q1: If the ack failed, transaction would be rolled back. This means if your consumer has properly coded, the processing of the message should be rolled back also (for example an updating of a datasource) so that as if the message has not been received and processed. Next time the message is redelivered and it will be processed again.

               

              Re Q2: I think here you need to use JTA to wrap the code. basic steps are:

               

              1. Using a transaction manager to create a transaction

              2. Enlist the JMS XAResource and other resources into the transaction

              3. receive the message and do the processing

              4. prepare and commit.

               

              You don't call the roll back your self, the tx manager will automatically roll back the transaction if anything is wrong.

               

              Have a look at the "xa-with-jta" example to get more information.

               

              Howard

              • 4. Re: Transactional Recieve
                gilstr

                Hi

                 

                Regarding Q1 - how can the consumer know if a transaction failed?

                 

                Thanks again for your thorough answer.

                 

                Gil

                • 5. Re: Transactional Recieve
                  gaohoward

                  how can the consumer know if a transaction failed?

                   

                  you will get an 'TransactionRolledBackException' exception ( for session.commit() ) or XAException (JTA).

                   

                  You can have a look at JMS 1.1 api doc and the javax.transaction API doc to get more information.

                   

                  Howard

                  1 of 1 people found this helpful