View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transport.vm;
8   
9   import org.mule.api.MuleEvent;
10  import org.mule.api.MuleMessage;
11  import org.mule.api.endpoint.EndpointURI;
12  import org.mule.api.endpoint.OutboundEndpoint;
13  import org.mule.api.transaction.TransactionCallback;
14  import org.mule.api.transport.DispatchException;
15  import org.mule.api.transport.NoReceiverForEndpointException;
16  import org.mule.config.i18n.CoreMessages;
17  import org.mule.transaction.TransactionTemplate;
18  import org.mule.transport.AbstractMessageDispatcher;
19  import org.mule.transport.vm.i18n.VMMessages;
20  import org.mule.util.queue.Queue;
21  import org.mule.util.queue.QueueSession;
22  
23  /**
24   * <code>VMMessageDispatcher</code> is used for providing in memory interaction
25   * between components.
26   */
27  public class VMMessageDispatcher extends AbstractMessageDispatcher
28  {
29      private final VMConnector connector;
30  
31      public VMMessageDispatcher(OutboundEndpoint endpoint)
32      {
33          super(endpoint);
34          this.connector = (VMConnector) endpoint.getConnector();
35      }
36  
37      @Override
38      protected void doDispatch(final MuleEvent event) throws Exception
39      {
40          EndpointURI endpointUri = endpoint.getEndpointURI();
41  
42          if (endpointUri == null)
43          {
44              throw new DispatchException(CoreMessages.objectIsNull("Endpoint"), event,
45                  (OutboundEndpoint) endpoint);
46          }
47          QueueSession session = connector.getQueueSession();
48          Queue queue = session.getQueue(endpointUri.getAddress());
49          if (!queue.offer(event, connector.getQueueTimeout()))
50          {
51              // queue is full
52              throw new DispatchException(VMMessages.queueIsFull(queue.getName(), queue.size()),
53                      event, (OutboundEndpoint) endpoint);
54          }
55          if (logger.isDebugEnabled())
56          {
57              logger.debug("dispatched MuleEvent on endpointUri: " + endpointUri);
58          }
59      }
60  
61      @Override
62      protected MuleMessage doSend(final MuleEvent event) throws Exception
63      {
64          MuleMessage retMessage;
65          EndpointURI endpointUri = event.getEndpoint().getEndpointURI();
66          final VMMessageReceiver receiver = connector.getReceiver(endpointUri);
67          // Apply any outbound transformers on this event before we dispatch
68  
69          if (receiver == null)
70          {
71              throw new NoReceiverForEndpointException(VMMessages.noReceiverForEndpoint(connector.getName(),
72                                                                                        event.getEndpoint().getEndpointURI()));
73          }
74  
75          TransactionTemplate<MuleMessage> tt = new TransactionTemplate<MuleMessage>(
76                                                              receiver.getEndpoint().getTransactionConfig(),
77                                                              event.getMuleContext());
78  
79          TransactionCallback<MuleMessage> cb = new TransactionCallback<MuleMessage>()
80          {
81              public MuleMessage doInTransaction() throws Exception
82              {
83                  return receiver.onCall(event.getMessage());
84              }
85          };
86          retMessage = tt.execute(cb);
87          
88          if (logger.isDebugEnabled())
89          {
90              logger.debug("sent event on endpointUri: " + event.getEndpoint().getEndpointURI());
91          }
92          return retMessage;
93      }
94  
95      @Override
96      protected void doDispose()
97      {
98          // template method
99      }
100 
101     @Override
102     protected void doConnect() throws Exception
103     {
104         if (!endpoint.getExchangePattern().hasResponse())
105         {
106             // use the default queue profile to configure this queue.
107             connector.getQueueProfile().configureQueue(
108                     endpoint.getEndpointURI().getAddress(), connector.getQueueManager());
109         }
110     }
111 
112     @Override
113     protected void doDisconnect() throws Exception
114     {
115         // template method
116     }
117 
118 }