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