Version 3

    The MDB below is listending to a queue queue/A and send replies back to the same queue, after receiving the reply it starts rolling back transaction untill this message goes to the dead letter queue (to cover most scenarios). Since this is EJB-3 MDB there is no need in xml descriptors.

     

    To trigger the code you can use a sample client

     

    package sample;
    
    import javax.annotation.Resource;
    import javax.ejb.ActivationConfigProperty;
    import javax.ejb.MessageDriven;
    import javax.ejb.MessageDrivenContext;
    import javax.jms.Connection;
    import javax.jms.JMSException;
    import javax.jms.Message;
    import javax.jms.MessageListener;
    import javax.jms.MessageProducer;
    import javax.jms.QueueConnectionFactory;
    import javax.jms.Session;
    import javax.jms.TextMessage;
    
    import org.apache.log4j.Logger;
    
    
    /**
     * Sample MDB that listens and sends messages to the same queue 
     * 
     */
    @MessageDriven(name = "MyMDB", activationConfig = {
              @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
              @ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/A"),
              @ActivationConfigProperty(
                        propertyName="providerAdapterJNDI",
                        propertyValue="java:/TIBCOJMSProvider")
    })
    public class SampleEJB3MDBean implements MessageListener {
         /** Logger */
         private static final Logger logger = Logger.getLogger(SampleEJB3MDBean.class);
         
         @Resource
         private MessageDrivenContext context;
         
         @Resource(mappedName="java:/JmsXA", type=javax.jms.ConnectionFactory.class)
         private QueueConnectionFactory queueConnectionFactory;
         
         /**
          * @see javax.jms.MessageListener#onMessage(javax.jms.Message)
          */
         public void onMessage(Message msg) {
              logger.info("Received message");
              
              TextMessage txtMsg = null;
              
              try {
                   if ( ! txtMsg.getText().equals("passed!!!") ) {
                        resendMessage( txtMsg );
                   } else {
                        logger.info("The message has the right content - not resending it manually but letting DLQ to deal with it");
                        context.setRollbackOnly();
                   }
              } catch (JMSException e) {
                   logger.fatal("Not accepting anymore!", e);
                   context.setRollbackOnly();
              }
         }
    
         private void resendMessage(TextMessage txtMsg) {
              Connection connection = null;
              logger.info("Resending message");
              
              try {
                   
                   connection = queueConnectionFactory.createConnection();
                   Session session = connection.createSession(true, javax.jms.Session.AUTO_ACKNOWLEDGE);
                   
                   // reusing the original queue
                   MessageProducer producer = session.createProducer( txtMsg.getJMSDestination() );
                   producer.setDeliveryMode( DeliveryMode.PERSISTENT );               
    
                   TextMessage textMessage = session.createTextMessage("passed!!!");
                   textMessage.setJMSDestination( txtMsg.getJMSDestination() );
                   textMessage.setJMSReplyTo( txtMsg.getJMSDestination() );
                   
                   producer.send(textMessage);
                   session.close();
                   
              } catch (JMSException e) {
                   // not rolling back
                   e.printStackTrace();
              } finally {
                   try {
                        connection.close();
                   } catch (JMSException e) {
                        // not rolling back
                        e.printStackTrace();
                   }
              }
         }
    
    }