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