Coverage Report - org.mule.providers.jms.XaTransactedJmsMessageReceiver
 
Classes in this File Line Coverage Branch Coverage Complexity
XaTransactedJmsMessageReceiver
79%
85/108
59%
40/68
3.909
XaTransactedJmsMessageReceiver$JmsThreadContext
100%
1/1
N/A
3.909
XaTransactedJmsMessageReceiver$ThreadContextLocal
100%
3/3
N/A
3.909
 
 1  
 /*
 2  
  * $Id: XaTransactedJmsMessageReceiver.java 10497 2008-01-24 10:10:32Z 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.providers.jms;
 12  
 
 13  
 import org.mule.impl.MuleMessage;
 14  
 import org.mule.providers.ConnectException;
 15  
 import org.mule.providers.SingleAttemptConnectionStrategy;
 16  
 import org.mule.providers.TransactedPollingMessageReceiver;
 17  
 import org.mule.providers.jms.filters.JmsSelectorFilter;
 18  
 import org.mule.transaction.TransactionCoordination;
 19  
 import org.mule.transaction.XaTransaction;
 20  
 import org.mule.umo.UMOComponent;
 21  
 import org.mule.umo.UMOTransaction;
 22  
 import org.mule.umo.endpoint.UMOEndpoint;
 23  
 import org.mule.umo.lifecycle.InitialisationException;
 24  
 import org.mule.umo.provider.UMOConnector;
 25  
 import org.mule.umo.provider.UMOMessageAdapter;
 26  
 import org.mule.util.ClassUtils;
 27  
 import org.mule.util.MapUtils;
 28  
 
 29  
 import java.util.List;
 30  
 
 31  
 import javax.jms.Destination;
 32  
 import javax.jms.JMSException;
 33  
 import javax.jms.Message;
 34  
 import javax.jms.MessageConsumer;
 35  
 import javax.jms.Session;
 36  
 
 37  
 import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
 38  
 
 39  
 public class XaTransactedJmsMessageReceiver extends TransactedPollingMessageReceiver
 40  
 {
 41  
     public static final long DEFAULT_JMS_POLL_FREQUENCY = 100;
 42  6
     public static final TimeUnit DEFAULT_JMS_POLL_TIMEUNIT = TimeUnit.MILLISECONDS;
 43  
 
 44  
     protected final JmsConnector connector;
 45  
     protected boolean reuseConsumer;
 46  
     protected boolean reuseSession;
 47  6
     protected final ThreadContextLocal context = new ThreadContextLocal();
 48  
     protected final long timeout;
 49  
     protected final RedeliveryHandler redeliveryHandler;
 50  
 
 51  
     /**
 52  
      * Holder receiving the session and consumer for this thread.
 53  
      */
 54  12
     protected static class JmsThreadContext
 55  
     {
 56  
         public Session session;
 57  
         public MessageConsumer consumer;
 58  
     }
 59  
 
 60  
     /**
 61  
      * Strongly typed ThreadLocal for ThreadContext.
 62  
      */
 63  6
     protected static class ThreadContextLocal extends ThreadLocal
 64  
     {
 65  
         public JmsThreadContext getContext()
 66  
         {
 67  84
             return (JmsThreadContext) get();
 68  
         }
 69  
 
 70  
         protected Object initialValue()
 71  
         {
 72  12
             return new JmsThreadContext();
 73  
         }
 74  
     }
 75  
 
 76  
     public XaTransactedJmsMessageReceiver(UMOConnector umoConnector, UMOComponent component, UMOEndpoint endpoint)
 77  
             throws InitialisationException
 78  
     {
 79  6
         super(umoConnector, component, endpoint);
 80  
         // TODO AP: find appropriate value for polling frequency with the scheduler;
 81  
         // see setFrequency/setTimeUnit & VMMessageReceiver for more
 82  6
         this.setFrequency(DEFAULT_JMS_POLL_FREQUENCY);
 83  6
         this.setTimeUnit(DEFAULT_JMS_POLL_TIMEUNIT);
 84  6
         this.connector = (JmsConnector) umoConnector;
 85  6
         this.timeout = endpoint.getTransactionConfig().getTimeout();
 86  
 
 87  
         // If reconnection is set, default reuse strategy to false
 88  
         // as some jms brokers will not detect lost connections if the
 89  
         // same consumer / session is used
 90  6
         if (this.connectionStrategy instanceof SingleAttemptConnectionStrategy)
 91  
         {
 92  6
             this.reuseConsumer = true;
 93  6
             this.reuseSession = true;
 94  
         }
 95  
 
 96  
         // User may override reuse strategy if necessary
 97  6
         this.reuseConsumer = MapUtils.getBooleanValue(endpoint.getProperties(), "reuseConsumer",
 98  
                                                       this.reuseConsumer);
 99  6
         this.reuseSession = MapUtils.getBooleanValue(endpoint.getProperties(), "reuseSession",
 100  
                                                      this.reuseSession);
 101  
 
 102  
         // Do extra validation, XA Topic & reuse are incompatible. See MULE-2622
 103  6
         boolean topic = connector.getTopicResolver().isTopic(getEndpoint());
 104  6
         if (topic && (reuseConsumer || reuseSession))
 105  
         {
 106  2
             logger.warn("Destination " + getEndpoint().getEndpointURI() + " is a topic and XA transaction was " +
 107  
                         "configured. Forcing 'reuseSession' and 'reuseConsumer' to false. Set these " +
 108  
                         "on endpoint to avoid the message.");
 109  2
             reuseConsumer = false;
 110  2
             reuseSession = false;
 111  
         }
 112  
 
 113  
         // Check if the destination is a queue and
 114  
         // if we are in transactional mode.
 115  
         // If true, set receiveMessagesInTransaction to true.
 116  
         // It will start multiple threads, depending on the threading profile.
 117  
 
 118  
         // If we're using topics we don't want to use multiple receivers as we'll get
 119  
         // the same message multiple times
 120  6
         this.setUseMultipleTransactedReceivers(!topic);
 121  
 
 122  
         try
 123  
         {
 124  6
             redeliveryHandler = this.connector.createRedeliveryHandler();
 125  6
             redeliveryHandler.setConnector(this.connector);
 126  
         }
 127  0
         catch (Exception e)
 128  
         {
 129  0
             throw new InitialisationException(e, this);
 130  6
         }
 131  
 
 132  6
     }
 133  
 
 134  
     protected void doDispose()
 135  
     {
 136  
         // template method
 137  6
     }
 138  
 
 139  
     protected void doConnect() throws Exception
 140  
     {
 141  8
         if (connector.isConnected() && connector.isEagerConsumer())
 142  
         {
 143  0
             createConsumer();
 144  
             // creating this consumer now would prevent from the actual worker
 145  
             // consumer
 146  
             // to receive the message!
 147  
             //Antoine Borg 08 Dec 2006 - Uncommented for MULE-1150
 148  
             // if we comment this line, if one tries to restart the service through
 149  
             // JMX,
 150  
             // this will fail...
 151  
             //This Line seems to be the root to a number of problems and differences between
 152  
             //Jms providers. A which point the consumer is created changes how the conneciton can be managed.
 153  
             //For example, WebsphereMQ needs the consumer created here, otherwise ReconnectionStrategies don't work properly
 154  
             //(See MULE-1150) However, is the consumer is created here for Active MQ, The worker thread cannot actually
 155  
             //receive the message.  We need to test with a few more Jms providers and transactions to see which behaviour
 156  
             // is correct.  My gut feeling is that the consumer should be created here and there is a bug in ActiveMQ
 157  
         }
 158  8
     }
 159  
 
 160  
     protected void doDisconnect() throws Exception
 161  
     {
 162  8
         if (connector.isConnected())
 163  
         {
 164  8
             closeConsumer(true);
 165  
         }
 166  8
     }
 167  
 
 168  
     /**
 169  
      * The poll method is overriden from the {@link TransactedPollingMessageReceiver}
 170  
      */
 171  
     public void poll() throws Exception
 172  
     {
 173  
         try
 174  
         {
 175  20
             JmsThreadContext ctx = context.getContext();
 176  
             // Create consumer if necessary
 177  20
             if (ctx.consumer == null)
 178  
             {
 179  16
                 createConsumer();
 180  
             }
 181  
             // Do polling
 182  19
             super.poll();
 183  
         }
 184  0
         catch (Exception e)
 185  
         {
 186  
             // Force consumer to close
 187  0
             closeConsumer(true);
 188  0
             throw e;
 189  
         }
 190  
         finally
 191  
         {
 192  
             // Close consumer if necessary
 193  20
             closeConsumer(false);
 194  18
         }
 195  18
     }
 196  
 
 197  
     /*
 198  
      * (non-Javadoc)
 199  
      * 
 200  
      * @see org.mule.providers.TransactionEnabledPollingMessageReceiver#getMessages()
 201  
      */
 202  
     protected List getMessages() throws Exception
 203  
     {
 204  
         // As the session is created outside the transaction, it is not
 205  
         // bound to it yet
 206  20
         JmsThreadContext ctx = context.getContext();
 207  
 
 208  20
         UMOTransaction tx = TransactionCoordination.getInstance().getTransaction();
 209  18
         if (tx != null)
 210  
         {
 211  20
             tx.bindResource(connector.getConnection(), ctx.session);
 212  
         }
 213  
 
 214  
         // Retrieve message
 215  20
         Message message = null;
 216  
         try
 217  
         {
 218  19
             message = ctx.consumer.receive(timeout);
 219  
         }
 220  0
         catch (JMSException e)
 221  
         {
 222  
             // If we're being disconnected, ignore the exception
 223  0
             if (!this.isConnected())
 224  
             {
 225  
                 // ignore
 226  
             }
 227  
             else
 228  
             {
 229  0
                 throw e;
 230  
             }
 231  20
         }
 232  20
         if (message == null)
 233  
         {
 234  8
             if (tx != null)
 235  
             {
 236  8
                 tx.setRollbackOnly();
 237  
             }
 238  8
             return null;
 239  
         }
 240  12
         message = connector.preProcessMessage(message, ctx.session);
 241  
 
 242  
         // Process message
 243  12
         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  12
         if (message.getJMSRedelivered())
 261  
         {
 262  2
             if (logger.isDebugEnabled())
 263  
             {
 264  0
                 logger.debug("Message with correlationId: " + message.getJMSCorrelationID()
 265  
                              + " is redelivered. handing off to Exception Handler");
 266  
             }
 267  2
             redeliveryHandler.handleRedelivery(message);
 268  
         }
 269  
 
 270  11
         if (tx instanceof JmsClientAcknowledgeTransaction)
 271  
         {
 272  0
             tx.bindResource(message, null);
 273  
         }
 274  
 
 275  12
         UMOMessageAdapter adapter = connector.getMessageAdapter(message);
 276  11
         routeMessage(new MuleMessage(adapter));
 277  12
         return null;
 278  
     }
 279  
 
 280  
     /*
 281  
      * (non-Javadoc)
 282  
      * 
 283  
      * @see org.mule.providers.TransactionEnabledPollingMessageReceiver#processMessage(java.lang.Object)
 284  
      */
 285  
     protected void processMessage(Object msg) throws Exception
 286  
     {
 287  
         // This method is never called as the
 288  
         // message is processed when received
 289  0
     }
 290  
 
 291  
     protected void closeConsumer(boolean force)
 292  
     {
 293  28
         JmsThreadContext ctx = context.getContext();
 294  28
         if (ctx == null)
 295  
         {
 296  0
             return;
 297  
         }
 298  
         // Close consumer
 299  28
         if (force || !reuseSession || !reuseConsumer)
 300  
         {
 301  22
             connector.closeQuietly(ctx.consumer);
 302  22
             ctx.consumer = null;
 303  
         }
 304  
         // Do not close session if a transaction is in progress
 305  
         // the session will be closed by the transaction
 306  27
         if (force || !reuseSession)
 307  
         {
 308  22
             connector.closeQuietly(ctx.session);
 309  20
             ctx.session = null;
 310  
         }
 311  26
     }
 312  
 
 313  
     /**
 314  
      * Create a consumer for the jms destination
 315  
      *
 316  
      * @throws Exception
 317  
      */
 318  
     protected void createConsumer() throws Exception
 319  
     {
 320  
         try
 321  
         {
 322  16
             JmsSupport jmsSupport = this.connector.getJmsSupport();
 323  16
             JmsThreadContext ctx = context.getContext();
 324  
             // Create session if none exists
 325  16
             if (ctx.session == null)
 326  
             {
 327  16
                 ctx.session = this.connector.getSession(endpoint);
 328  
                 //set reuse flag
 329  15
                 ((XaTransaction.MuleXaObject) ctx.session).setReuseObject(reuseSession);
 330  
 
 331  
             }
 332  
 
 333  
             // Create destination
 334  16
             final boolean topic = connector.getTopicResolver().isTopic(endpoint);
 335  15
             Destination dest = jmsSupport.createDestination(ctx.session, endpoint.getEndpointURI()
 336  
                     .getAddress(), topic);
 337  
 
 338  
             // Extract jms selector
 339  16
             String selector = null;
 340  15
             if (endpoint.getFilter() != null && endpoint.getFilter() instanceof JmsSelectorFilter)
 341  
             {
 342  0
                 selector = ((JmsSelectorFilter) endpoint.getFilter()).getExpression();
 343  
             }
 344  16
             else if (endpoint.getProperties() != null)
 345  
             {
 346  
                 // still allow the selector to be set as a property on the endpoint
 347  
                 // to be backward compatible
 348  15
                 selector = (String) endpoint.getProperties().get(JmsConstants.JMS_SELECTOR_PROPERTY);
 349  
             }
 350  16
             String tempDurable = (String) endpoint.getProperties().get("durable");
 351  16
             boolean durable = connector.isDurable();
 352  15
             if (tempDurable != null)
 353  
             {
 354  0
                 durable = Boolean.valueOf(tempDurable).booleanValue();
 355  
             }
 356  
 
 357  
             // Get the durable subscriber name if there is one
 358  14
             String durableName = (String) endpoint.getProperties().get("durableName");
 359  16
             if (durableName == null && durable && topic)
 360  
             {
 361  13
                 durableName = "mule." + connector.getName() + "." + endpoint.getEndpointURI().getAddress();
 362  14
                 logger.debug("Jms Connector for this receiver is durable but no durable name has been specified. Defaulting to: "
 363  
                              + durableName);
 364  
             }
 365  
 
 366  
             // Create consumer
 367  16
             ctx.consumer = jmsSupport.createConsumer(ctx.session, dest, selector, connector.isNoLocal(),
 368  
                                                      durableName, topic);
 369  
         }
 370  0
         catch (JMSException e)
 371  
         {
 372  0
             throw new ConnectException(e, this);
 373  16
         }
 374  16
     }
 375  
 }