Coverage Report - org.mule.transport.jms.TransactedSingleResourceJmsMessageReceiver
 
Classes in this File Line Coverage Branch Coverage Complexity
TransactedSingleResourceJmsMessageReceiver
0%
0/62
0%
0/20
3
TransactedSingleResourceJmsMessageReceiver$MessageReceiverWorker
0%
0/15
0%
0/2
3
TransactedSingleResourceJmsMessageReceiver$MessageReceiverWorker$1
0%
0/20
0%
0/12
3
 
 1  
 /*
 2  
  * $Id: TransactedSingleResourceJmsMessageReceiver.java 11592 2008-04-18 10:09:22Z dirk.olmes $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 9  
  */
 10  
 
 11  
 package org.mule.transport.jms;
 12  
 
 13  
 import org.mule.DefaultMuleMessage;
 14  
 import org.mule.api.MuleException;
 15  
 import org.mule.api.endpoint.InboundEndpoint;
 16  
 import org.mule.api.lifecycle.CreateException;
 17  
 import org.mule.api.lifecycle.InitialisationException;
 18  
 import org.mule.api.lifecycle.StartException;
 19  
 import org.mule.api.lifecycle.StopException;
 20  
 import org.mule.api.service.Service;
 21  
 import org.mule.api.transaction.Transaction;
 22  
 import org.mule.api.transaction.TransactionCallback;
 23  
 import org.mule.api.transport.Connector;
 24  
 import org.mule.api.transport.MessageAdapter;
 25  
 import org.mule.transaction.TransactionCoordination;
 26  
 import org.mule.transaction.TransactionTemplate;
 27  
 import org.mule.transport.AbstractMessageReceiver;
 28  
 import org.mule.transport.ConnectException;
 29  
 import org.mule.transport.jms.filters.JmsSelectorFilter;
 30  
 import org.mule.util.ClassUtils;
 31  
 
 32  
 import javax.jms.Destination;
 33  
 import javax.jms.JMSException;
 34  
 import javax.jms.Message;
 35  
 import javax.jms.MessageConsumer;
 36  
 import javax.jms.MessageListener;
 37  
 import javax.jms.Session;
 38  
 import javax.jms.Topic;
 39  
 import javax.resource.spi.work.Work;
 40  
 
 41  0
 public class TransactedSingleResourceJmsMessageReceiver extends AbstractMessageReceiver
 42  
         implements MessageListener
 43  
 {
 44  
     protected JmsConnector connector;
 45  
     protected RedeliveryHandler redeliveryHandler;
 46  
     protected MessageConsumer consumer;
 47  
     protected Session session;
 48  0
     protected boolean startOnConnect = false;
 49  
 
 50  
     /** determines whether messages will be received in a transaction template */
 51  0
     protected boolean receiveMessagesInTransaction = true;
 52  
 
 53  
     /** determines whether Multiple receivers are created to improve throughput */
 54  0
     protected boolean useMultipleReceivers = true;
 55  
 
 56  
     /**
 57  
      * @param connector
 58  
      * @param service
 59  
      * @param endpoint
 60  
      * @throws InitialisationException
 61  
      */
 62  
     public TransactedSingleResourceJmsMessageReceiver(Connector connector,
 63  
                                                       Service service,
 64  
                                                       InboundEndpoint endpoint) throws CreateException
 65  
     {
 66  
 
 67  0
         super(connector, service, endpoint);
 68  
 
 69  0
         this.connector = (JmsConnector) connector;
 70  
 
 71  
         // TODO check which properties being set in the TransecteJmsMessage receiver
 72  
         // are needed...
 73  
 
 74  
         try
 75  
         {
 76  0
             redeliveryHandler = this.connector.getRedeliveryHandlerFactory().create();
 77  0
             redeliveryHandler.setConnector(this.connector);
 78  
         }
 79  0
         catch (Exception e)
 80  
         {
 81  0
             throw new CreateException(e, this);
 82  0
         }
 83  0
     }
 84  
 
 85  
     protected void doDispose()
 86  
     {
 87  
         // template method
 88  0
     }
 89  
 
 90  
     protected void doConnect() throws Exception
 91  
     {
 92  
         try
 93  
         {
 94  0
             JmsSupport jmsSupport = this.connector.getJmsSupport();
 95  
             // Create session if none exists
 96  0
             if (session == null)
 97  
             {
 98  0
                 session = this.connector.getSession(endpoint);
 99  
             }
 100  
 
 101  
             // Create destination
 102  0
             boolean topic = connector.getTopicResolver().isTopic(endpoint, true);
 103  
 
 104  0
             Destination dest = jmsSupport.createDestination(session, endpoint.getEndpointURI().getAddress(),
 105  
                     topic);
 106  
 
 107  
             // Extract jms selector
 108  0
             String selector = null;
 109  0
             if (endpoint.getFilter() != null && endpoint.getFilter() instanceof JmsSelectorFilter)
 110  
             {
 111  0
                 selector = ((JmsSelectorFilter) endpoint.getFilter()).getExpression();
 112  
             }
 113  0
             else if (endpoint.getProperties() != null)
 114  
             {
 115  
                 // still allow the selector to be set as a property on the endpoint
 116  
                 // to be backward compatable
 117  0
                 selector = (String) endpoint.getProperties().get(JmsConstants.JMS_SELECTOR_PROPERTY);
 118  
             }
 119  0
             String tempDurable = (String) endpoint.getProperties().get(JmsConstants.DURABLE_PROPERTY);
 120  0
             boolean durable = connector.isDurable();
 121  0
             if (tempDurable != null)
 122  
             {
 123  0
                 durable = Boolean.valueOf(tempDurable).booleanValue();
 124  
             }
 125  
 
 126  
             // Get the durable subscriber name if there is one
 127  0
             String durableName = (String) endpoint.getProperties().get(JmsConstants.DURABLE_NAME_PROPERTY);
 128  0
             if (durableName == null && durable && dest instanceof Topic)
 129  
             {
 130  0
                 durableName = "mule." + connector.getName() + "." + endpoint.getEndpointURI().getAddress();
 131  0
                 logger.debug("Jms Connector for this receiver is durable but no durable name has been specified. Defaulting to: "
 132  
                         + durableName);
 133  
             }
 134  
 
 135  
             // Create consumer
 136  0
             consumer = jmsSupport.createConsumer(session, dest, selector, connector.isNoLocal(), durableName,
 137  
                     topic);
 138  
         }
 139  0
         catch (JMSException e)
 140  
         {
 141  0
             throw new ConnectException(e, this);
 142  0
         }
 143  0
     }
 144  
 
 145  
     protected void doStart() throws MuleException
 146  
     {
 147  
         try
 148  
         {
 149  
             // We ned to register the listener when start is called in order to only
 150  
             // start receiving messages after start.
 151  
             // If the consumer is null it means that the connection strategy is being
 152  
             // run in a separate thread and hasn't managed to connect yet.
 153  0
             if (consumer == null)
 154  
             {
 155  0
                 startOnConnect = true;
 156  
             }
 157  
             else
 158  
             {
 159  0
                 startOnConnect = false;
 160  0
                 this.consumer.setMessageListener(this);
 161  
             }
 162  
         }
 163  0
         catch (JMSException e)
 164  
         {
 165  0
             throw new StartException(e, this);
 166  0
         }
 167  0
     }
 168  
 
 169  
     protected void doStop() throws MuleException
 170  
     {
 171  
         try
 172  
         {
 173  0
             if (consumer != null)
 174  
             {
 175  0
                 consumer.setMessageListener(null);
 176  
             }
 177  
         }
 178  0
         catch (JMSException e)
 179  
         {
 180  0
             throw new StopException(e, this);
 181  0
         }
 182  0
     }
 183  
 
 184  
     public void doDisconnect() throws Exception
 185  
     {
 186  0
         closeConsumer();
 187  0
     }
 188  
 
 189  
     protected void closeConsumer()
 190  
     {
 191  0
         connector.closeQuietly(consumer);
 192  0
         consumer = null;
 193  0
         connector.closeQuietly(session);
 194  0
         session = null;
 195  0
     }
 196  
 
 197  
     public void onMessage(Message message)
 198  
     {
 199  
         try
 200  
         {
 201  0
             getWorkManager().scheduleWork(new MessageReceiverWorker(message));
 202  
         }
 203  0
         catch (Exception e)
 204  
         {
 205  0
             handleException(e);
 206  0
         }
 207  0
     }
 208  
 
 209  
     protected class MessageReceiverWorker implements Work
 210  
     {
 211  
         Message message;
 212  
 
 213  
         public MessageReceiverWorker(Message message)
 214  0
         {
 215  0
             this.message = message;
 216  0
         }
 217  
 
 218  
         public void run()
 219  
         {
 220  
             try
 221  
             {
 222  0
                 TransactionTemplate tt = new TransactionTemplate(endpoint.getTransactionConfig(),
 223  
                         connector.getExceptionListener(), connector.getMuleContext());
 224  
 
 225  0
                 if (receiveMessagesInTransaction)
 226  
                 {
 227  0
                     TransactionCallback cb = new MessageTransactionCallback(message)
 228  
                     {
 229  
 
 230  0
                         public Object doInTransaction() throws Exception
 231  
                         {
 232  
                             // Get Transaction & Bind MuleSession
 233  0
                             Transaction tx = TransactionCoordination.getInstance().getTransaction();
 234  0
                             if (tx != null)
 235  
                             {
 236  0
                                 tx.bindResource(connector.getConnection(), session);
 237  
                             }
 238  0
                             if (tx instanceof JmsClientAcknowledgeTransaction)
 239  
                             {
 240  0
                                 tx.bindResource(message, message);
 241  
                             }
 242  
 
 243  0
                             if (logger.isDebugEnabled())
 244  
                             {
 245  0
                                 logger.debug("Message received it is of type: " +
 246  
                                         ClassUtils.getSimpleName(message.getClass()));
 247  0
                                 if (message.getJMSDestination() != null)
 248  
                                 {
 249  0
                                     logger.debug("Message received on " + message.getJMSDestination() + " ("
 250  
                                             + message.getJMSDestination().getClass().getName() + ")");
 251  
                                 }
 252  
                                 else
 253  
                                 {
 254  0
                                     logger.debug("Message received on unknown destination");
 255  
                                 }
 256  0
                                 logger.debug("Message CorrelationId is: " + message.getJMSCorrelationID());
 257  0
                                 logger.debug("Jms Message Id is: " + message.getJMSMessageID());
 258  
                             }
 259  
 
 260  0
                             if (message.getJMSRedelivered())
 261  
                             {
 262  0
                                 if (logger.isDebugEnabled())
 263  
                                 {
 264  0
                                     logger.debug("Message with correlationId: "
 265  
                                             + message.getJMSCorrelationID()
 266  
                                             + " is redelivered. handing off to Exception Handler");
 267  
                                 }
 268  0
                                 redeliveryHandler.handleRedelivery(message);
 269  
                             }
 270  
 
 271  0
                             MessageAdapter adapter = connector.getMessageAdapter(message);
 272  0
                             routeMessage(new DefaultMuleMessage(adapter));
 273  0
                             return null;
 274  
                         }
 275  
                     };
 276  0
                     tt.execute(cb);
 277  0
                 }
 278  
                 else
 279  
                 {
 280  0
                     MessageAdapter adapter = connector.getMessageAdapter(message);
 281  0
                     routeMessage(new DefaultMuleMessage(adapter));
 282  
                 }
 283  
 
 284  
             }
 285  0
             catch (Exception e)
 286  
             {
 287  0
                 getConnector().handleException(e);
 288  0
             }
 289  
 
 290  0
         }
 291  
 
 292  
         public void release()
 293  
         {
 294  
             // Nothing to release.
 295  0
         }
 296  
 
 297  
     }
 298  
 
 299  
 }