View Javadoc

1   /*
2    * $Id: SimpleAsyncRequestReplyRequester.java 22666 2011-08-15 06:35:36Z mike.schilling $
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.routing.requestreply;
12  
13  import org.mule.api.MuleContext;
14  import org.mule.api.MuleEvent;
15  import org.mule.api.MuleException;
16  import org.mule.api.config.MuleProperties;
17  import org.mule.api.construct.FlowConstructAware;
18  import org.mule.api.context.MuleContextAware;
19  import org.mule.api.endpoint.InboundEndpoint;
20  import org.mule.api.lifecycle.Disposable;
21  import org.mule.api.lifecycle.Initialisable;
22  import org.mule.api.lifecycle.Startable;
23  import org.mule.api.lifecycle.Stoppable;
24  import org.mule.api.processor.MessageProcessor;
25  import org.mule.api.source.MessageSource;
26  
27  public class SimpleAsyncRequestReplyRequester extends AbstractAsyncRequestReplyRequester
28      implements Startable, Stoppable
29  {
30  
31      protected MessageProcessor requestMessageProcessor;
32  
33      @Override
34      protected void sendAsyncRequest(MuleEvent event) throws MuleException
35      {
36          setAsyncReplyProperties(event);
37          requestMessageProcessor.process(event);
38      }
39  
40      protected void setAsyncReplyProperties(MuleEvent event) throws MuleException
41      {
42          event.getMessage().setReplyTo(getReplyTo());
43          event.getMessage().setOutboundProperty(MuleProperties.MULE_REPLY_TO_REQUESTOR_PROPERTY,
44              event.getFlowConstruct().getName());
45          String correlation = event.getFlowConstruct().getMessageInfoMapping().getCorrelationId(
46              event.getMessage());
47          event.getMessage().setCorrelationId(correlation);
48      }
49  
50      private String getReplyTo()
51      {
52          return ((InboundEndpoint) replyMessageSource).getEndpointURI().toString();
53      }
54  
55      @Override
56      protected void verifyReplyMessageSource(MessageSource messageSource)
57      {
58          if (!(messageSource instanceof InboundEndpoint))
59          {
60              throw new IllegalArgumentException(
61                  "Only an InboundEndpoint reply MessageSource is supported with SimpleAsyncRequestReplyRequester");
62          }
63      }
64  
65      public void setMessageProcessor(MessageProcessor processor)
66      {
67          requestMessageProcessor = processor;
68      }
69  
70      @Deprecated
71      public void setMessageSource(MessageSource source)
72      {
73          setReplySource(source);
74      }
75  
76      public void start() throws MuleException
77      {
78          if (replyMessageSource != null)
79          {
80              if (replyMessageSource instanceof FlowConstructAware)
81              {
82                  ((FlowConstructAware) replyMessageSource).setFlowConstruct(this.flowConstruct);
83              }
84              if (replyMessageSource instanceof Initialisable)
85              {
86                  ((Initialisable) replyMessageSource).initialise();
87              }
88              if (replyMessageSource instanceof Startable)
89              {
90                  ((Startable) replyMessageSource).start();
91              }
92          }
93          if (requestMessageProcessor != null)
94          {
95              if (requestMessageProcessor instanceof FlowConstructAware)
96              {
97                  ((FlowConstructAware) requestMessageProcessor).setFlowConstruct(this.flowConstruct);
98              }
99              if (requestMessageProcessor instanceof Initialisable)
100             {
101                 ((Initialisable) requestMessageProcessor).initialise();
102             }
103             if (requestMessageProcessor instanceof Startable)
104             {
105                 ((Startable) requestMessageProcessor).start();
106             }
107         }
108         super.start();
109     }
110 
111     public void stop() throws MuleException
112     {
113         if (replyMessageSource != null && replyMessageSource instanceof Stoppable)
114         {
115             ((Stoppable) replyMessageSource).stop();
116 
117             if (requestMessageProcessor != null && requestMessageProcessor instanceof Stoppable)
118             {
119                 ((Stoppable) requestMessageProcessor).stop();
120             }
121         }
122         if (requestMessageProcessor != null)
123         {
124             if (requestMessageProcessor instanceof Stoppable)
125             {
126                 ((Stoppable) requestMessageProcessor).stop();
127             }
128             if (requestMessageProcessor instanceof Disposable)
129             {
130                 ((Disposable) requestMessageProcessor).dispose();
131             }
132         }
133         super.stop();
134     }
135 
136     @Override
137     public void setMuleContext(MuleContext context)
138     {
139         super.setMuleContext(context);
140         if (requestMessageProcessor instanceof MuleContextAware)
141         {
142             ((MuleContextAware)requestMessageProcessor).setMuleContext(context);
143         }
144     }
145 
146 }