Coverage Report - org.mule.providers.jms.JmsMessageReceiver
 
Classes in this File Line Coverage Branch Coverage Complexity
JmsMessageReceiver
77%
49/64
50%
11/22
2.769
JmsMessageReceiver$JmsWorker
54%
14/26
36%
5/14
2.769
 
 1  
 /*
 2  
  * $Id: JmsMessageReceiver.java 7963 2007-08-21 08:53:15Z 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.umo.UMOComponent;
 14  
 import org.mule.umo.UMOException;
 15  
 import org.mule.umo.UMOTransaction;
 16  
 import org.mule.umo.TransactionException;
 17  
 import org.mule.umo.endpoint.UMOEndpoint;
 18  
 import org.mule.umo.lifecycle.InitialisationException;
 19  
 import org.mule.umo.lifecycle.LifecycleException;
 20  
 import org.mule.umo.provider.UMOConnector;
 21  
 import org.mule.util.ClassUtils;
 22  
 import org.mule.providers.AbstractReceiverWorker;
 23  
 import org.mule.providers.AbstractMessageReceiver;
 24  
 import org.mule.providers.ConnectException;
 25  
 import org.mule.providers.jms.filters.JmsSelectorFilter;
 26  
 
 27  
 import java.util.ArrayList;
 28  
 import java.util.List;
 29  
 
 30  
 import javax.jms.Message;
 31  
 import javax.jms.JMSException;
 32  
 import javax.jms.Destination;
 33  
 import javax.jms.Topic;
 34  
 import javax.jms.MessageConsumer;
 35  
 import javax.jms.Session;
 36  
 import javax.jms.MessageListener;
 37  
 
 38  
 /**
 39  
  * Registers a single JmsMessage listener but uses a thread pool to process incoming
 40  
  * messages
 41  
  */
 42  60
 public class JmsMessageReceiver extends AbstractMessageReceiver implements MessageListener
 43  
 {
 44  
 
 45  
     protected JmsConnector connector;
 46  
     protected RedeliveryHandler redeliveryHandler;
 47  
     protected MessageConsumer consumer;
 48  
     protected Session session;
 49  68
     protected boolean startOnConnect = false;
 50  
 
 51  
     public JmsMessageReceiver(UMOConnector connector, UMOComponent component, UMOEndpoint endpoint)
 52  
             throws InitialisationException
 53  
     {
 54  68
         super(connector, component, endpoint);
 55  68
         this.connector = (JmsConnector) connector;
 56  
 
 57  
         try
 58  
         {
 59  68
             redeliveryHandler = this.connector.createRedeliveryHandler();
 60  68
             redeliveryHandler.setConnector(this.connector);
 61  
         }
 62  0
         catch (Exception e)
 63  
         {
 64  0
             throw new InitialisationException(e, this);
 65  68
         }
 66  68
     }
 67  
 
 68  
     protected void doConnect() throws Exception
 69  
     {
 70  63
         createConsumer();
 71  63
         if (startOnConnect)
 72  
         {
 73  0
             doStart();
 74  
         }
 75  63
     }
 76  
 
 77  
     protected void doDisconnect() throws Exception
 78  
     {
 79  63
         closeConsumer();
 80  63
     }
 81  
 
 82  
     public void onMessage(Message message)
 83  
     {
 84  
         try
 85  
         {
 86  60
             getWorkManager().scheduleWork(new JmsWorker(message, this));
 87  
         }
 88  0
         catch (Exception e)
 89  
         {
 90  0
             handleException(e);
 91  60
         }
 92  60
     }
 93  
 
 94  
     protected  class JmsWorker extends AbstractReceiverWorker
 95  
     {
 96  
         public JmsWorker(Message message, AbstractMessageReceiver receiver)
 97  59
         {
 98  60
             super(new ArrayList(1), receiver);
 99  60
             messages.add(message);
 100  60
         }
 101  
 
 102  
         public JmsWorker(List messages, AbstractMessageReceiver receiver)
 103  0
         {
 104  0
             super(messages, receiver);
 105  0
         }
 106  
 
 107  
         //@Override
 108  
         protected Object preProcessMessage(Object message) throws Exception
 109  
         {
 110  60
             Message m = (Message) message;
 111  
 
 112  60
             if (logger.isDebugEnabled())
 113  
             {
 114  0
                 logger.debug("Message received it is of type: " +
 115  
                         ClassUtils.getSimpleName(message.getClass()));
 116  0
                 if (m.getJMSDestination() != null)
 117  
                 {
 118  0
                     logger.debug("Message received on " + m.getJMSDestination() + " ("
 119  
                             + m.getJMSDestination().getClass().getName() + ")");
 120  
                 }
 121  
                 else
 122  
                 {
 123  0
                     logger.debug("Message received on unknown destination");
 124  
                 }
 125  0
                 logger.debug("Message CorrelationId is: " + m.getJMSCorrelationID());
 126  0
                 logger.debug("Jms Message Id is: " + m.getJMSMessageID());
 127  
             }
 128  
 
 129  60
             if (m.getJMSRedelivered() && redeliveryHandler != null)
 130  
             {
 131  0
                 if (logger.isDebugEnabled())
 132  
                 {
 133  0
                     logger.debug("Message with correlationId: " + m.getJMSCorrelationID()
 134  
                             + " has redelivered flag set, handing off to Exception Handler");
 135  
                 }
 136  0
                 redeliveryHandler.handleRedelivery(m);
 137  
             }
 138  59
             return m;
 139  
 
 140  
         }
 141  
 
 142  
         protected void bindTransaction(UMOTransaction tx) throws TransactionException
 143  
         {
 144  26
             if(tx instanceof JmsTransaction)
 145  
             {
 146  18
                 tx.bindResource(connector.getConnection(), session);
 147  
             }
 148  8
             else if(tx instanceof JmsClientAcknowledgeTransaction)
 149  
             {
 150  
                 //We should still bind the session to the transaction, but we also need the message itself
 151  
                 //since that is the object that gets Acknowledged
 152  8
                 tx.bindResource(connector.getConnection(), session);
 153  8
                 ((JmsClientAcknowledgeTransaction)tx).setMessage((Message)messages.get(0));
 154  
             }
 155  26
         }
 156  
     }
 157  
 
 158  
     protected void doStart() throws UMOException
 159  
     {
 160  
         try
 161  
         {
 162  
             // We ned to register the listener when start is called in order to only
 163  
             // start receiving messages after
 164  
             // start/
 165  
             // If the consumer is null it means that the connection strategy is being
 166  
             // run in a separate thread
 167  
             // And hasn't managed to connect yet.
 168  63
             if (consumer == null)
 169  
             {
 170  0
                 startOnConnect = true;
 171  
             }
 172  
             else
 173  
             {
 174  63
                 startOnConnect = false;
 175  63
                 consumer.setMessageListener(this);
 176  
             }
 177  
         }
 178  0
         catch (JMSException e)
 179  
         {
 180  0
             throw new LifecycleException(e, this);
 181  63
         }
 182  63
     }
 183  
 
 184  
     protected void doStop() throws UMOException
 185  
     {
 186  
         try
 187  
         {
 188  63
             if (consumer != null)
 189  
             {
 190  0
                 consumer.setMessageListener(null);
 191  
             }
 192  
         }
 193  0
         catch (JMSException e)
 194  
         {
 195  0
             throw new LifecycleException(e, this);
 196  63
         }
 197  63
     }
 198  
 
 199  
     protected void doDispose()
 200  
     {
 201  
         // template method
 202  66
     }
 203  
 
 204  
     protected void closeConsumer()
 205  
     {
 206  63
         connector.closeQuietly(consumer);
 207  63
         consumer = null;
 208  63
         connector.closeQuietly(session);
 209  63
         session = null;
 210  63
     }
 211  
 
 212  
     /**
 213  
      * Create a consumer for the jms destination
 214  
      *
 215  
      * @throws Exception
 216  
      */
 217  
     protected void createConsumer() throws Exception
 218  
     {
 219  
         try
 220  
         {
 221  63
             JmsSupport jmsSupport = this.connector.getJmsSupport();
 222  
             // Create session if none exists
 223  63
             if (session == null)
 224  
             {
 225  63
                 session = this.connector.getSession(endpoint);
 226  
             }
 227  
 
 228  63
             boolean topic = connector.getTopicResolver().isTopic(endpoint, true);
 229  
 
 230  
             // Create destination
 231  63
             Destination dest = jmsSupport.createDestination(session, endpoint.getEndpointURI().getAddress(),
 232  
                     topic);
 233  
 
 234  
             // Extract jms selector
 235  63
             String selector = null;
 236  63
             if (endpoint.getFilter() != null && endpoint.getFilter() instanceof JmsSelectorFilter)
 237  
             {
 238  0
                 selector = ((JmsSelectorFilter) endpoint.getFilter()).getExpression();
 239  
             }
 240  63
             else if (endpoint.getProperties() != null)
 241  
             {
 242  
                 // still allow the selector to be set as a property on the endpoint
 243  
                 // to be backward compatable
 244  63
                 selector = (String) endpoint.getProperties().get(JmsConstants.JMS_SELECTOR_PROPERTY);
 245  
             }
 246  63
             String tempDurable = (String) endpoint.getProperties().get(JmsConstants.DURABLE_PROPERTY);
 247  63
             boolean durable = connector.isDurable();
 248  63
             if (tempDurable != null)
 249  
             {
 250  0
                 durable = Boolean.valueOf(tempDurable).booleanValue();
 251  
             }
 252  
 
 253  
             // Get the durable subscriber name if there is one
 254  63
             String durableName = (String) endpoint.getProperties().get(JmsConstants.DURABLE_NAME_PROPERTY);
 255  63
             if (durableName == null && durable && dest instanceof Topic)
 256  
             {
 257  5
                 durableName = "mule." + connector.getName() + "." + endpoint.getEndpointURI().getAddress();
 258  5
                 logger.debug("Jms Connector for this receiver is durable but no durable name has been specified. Defaulting to: "
 259  
                         + durableName);
 260  
             }
 261  
 
 262  
             // Create consumer
 263  63
             consumer = jmsSupport.createConsumer(session, dest, selector, connector.isNoLocal(), durableName,
 264  
                     topic);
 265  
         }
 266  0
         catch (JMSException e)
 267  
         {
 268  0
             throw new ConnectException(e, this);
 269  63
         }
 270  63
     }
 271  
 
 272  
 }