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