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.MuleEvent;
15 import org.mule.api.MuleException;
16 import org.mule.api.config.MuleProperties;
17 import org.mule.api.construct.FlowConstruct;
18 import org.mule.api.construct.FlowConstructAware;
19 import org.mule.api.endpoint.InboundEndpoint;
20 import org.mule.api.endpoint.OutboundEndpoint;
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, FlowConstructAware
29 {
30
31 private MessageProcessor requestMessageProcessor;
32
33 @Override
34 protected void sendAsyncRequest(MuleEvent event) throws MuleException
35 {
36 setAsyncReplyProperties(event);
37 if (requestMessageProcessor instanceof OutboundEndpoint)
38 {
39 event = new DefaultMuleEvent(event.getMessage(), (OutboundEndpoint) requestMessageProcessor,
40 event.getSession());
41 }
42 requestMessageProcessor.process(event);
43 }
44
45 protected void setAsyncReplyProperties(MuleEvent event) throws MuleException
46 {
47 event.getMessage().setReplyTo(getReplyTo());
48 event.getMessage().setOutboundProperty(MuleProperties.MULE_REPLY_TO_REQUESTOR_PROPERTY,
49 event.getFlowConstruct().getName());
50 String correlation = event.getFlowConstruct().getMessageInfoMapping().getCorrelationId(
51 event.getMessage());
52 event.getMessage().setCorrelationId(correlation);
53 }
54
55 private String getReplyTo()
56 {
57 return ((InboundEndpoint) replyMessageSource).getEndpointURI().getAddress();
58 }
59
60 @Override
61 protected void verifyReplyMessageSource(MessageSource messageSource)
62 {
63 if (!(messageSource instanceof InboundEndpoint))
64 {
65 throw new IllegalArgumentException(
66 "Only an InboundEndpoint reply MessageSource is supported with SimpleAsyncRequestReplyRequester");
67 }
68 }
69
70 public void setMessageProcessor(MessageProcessor processor)
71 {
72 requestMessageProcessor = processor;
73 }
74
75 @Deprecated
76 public void setMessageSource(MessageSource source)
77 {
78 setReplySource(source);
79 }
80
81 public void start() throws MuleException
82 {
83 if (replyMessageSource != null)
84 {
85 if (replyMessageSource instanceof FlowConstructAware)
86 {
87 ((FlowConstructAware) replyMessageSource).setFlowConstruct(this.flowConstruct);
88 }
89 if (replyMessageSource instanceof Initialisable)
90 {
91 ((Initialisable) replyMessageSource).initialise();
92 }
93 if (replyMessageSource instanceof Startable)
94 {
95 ((Startable) replyMessageSource).start();
96 }
97 }
98 }
99
100 public void stop() throws MuleException
101 {
102 if (replyMessageSource != null && replyMessageSource instanceof Stoppable)
103 {
104 ((Stoppable) replyMessageSource).stop();
105 }
106 }
107
108 public void setFlowConstruct(FlowConstruct flowConstruct)
109 {
110 this.flowConstruct = flowConstruct;
111 }
112
113 }