1
2
3
4
5
6
7
8
9
10
11 package org.mule.transport.polling;
12
13 import org.mule.DefaultMuleEvent;
14 import org.mule.DefaultMuleMessage;
15 import org.mule.api.MuleEvent;
16 import org.mule.api.MuleMessage;
17 import org.mule.api.config.MuleProperties;
18 import org.mule.api.construct.FlowConstruct;
19 import org.mule.api.endpoint.ImmutableEndpoint;
20 import org.mule.api.endpoint.InboundEndpoint;
21 import org.mule.api.endpoint.OutboundEndpoint;
22 import org.mule.api.lifecycle.CreateException;
23 import org.mule.api.lifecycle.InitialisationException;
24 import org.mule.api.processor.MessageProcessor;
25 import org.mule.api.transport.Connector;
26 import org.mule.config.i18n.CoreMessages;
27 import org.mule.session.DefaultMuleSession;
28 import org.mule.transport.AbstractConnector;
29 import org.mule.transport.AbstractPollingMessageReceiver;
30 import org.mule.transport.NullPayload;
31 import org.mule.util.StringUtils;
32
33 import java.util.Map;
34
35 public class MessageProcessorPollingMessageReceiver extends AbstractPollingMessageReceiver
36 {
37 public static final String SOURCE_MESSAGE_PROCESSOR_PROPERTY_NAME = MuleProperties.ENDPOINT_PROPERTY_PREFIX + "sourceMessageProcessor";
38
39 protected MessageProcessor sourceMessageProcessor;
40
41 public MessageProcessorPollingMessageReceiver(Connector connector,
42 FlowConstruct flowConstruct,
43 InboundEndpoint endpoint) throws CreateException
44 {
45 super(connector, flowConstruct, endpoint);
46 }
47
48 @Override
49 protected void doInitialise() throws InitialisationException
50 {
51 super.doInitialise();
52
53 sourceMessageProcessor = (MessageProcessor) endpoint.getProperty(SOURCE_MESSAGE_PROCESSOR_PROPERTY_NAME);
54
55 if (sourceMessageProcessor instanceof OutboundEndpoint
56 && !((OutboundEndpoint) sourceMessageProcessor).getExchangePattern().hasResponse())
57 {
58
59 throw new InitialisationException(CoreMessages.createStaticMessage(String.format(
60 "The endpoint %s does not return responses and therefore can't be used for polling.",
61 sourceMessageProcessor)), this);
62 }
63
64 Long tempPolling = (Long) endpoint.getProperties().get(AbstractConnector.PROPERTY_POLLING_FREQUENCY);
65 if (tempPolling != null)
66 {
67 setFrequency(tempPolling);
68 }
69 }
70
71 @Override
72 public void poll() throws Exception
73 {
74 MuleMessage request = new DefaultMuleMessage(StringUtils.EMPTY, (Map<String, Object>) null,
75 connector.getMuleContext());
76 ImmutableEndpoint ep = endpoint;
77 if (sourceMessageProcessor instanceof ImmutableEndpoint)
78 {
79 ep = (ImmutableEndpoint) sourceMessageProcessor;
80 }
81
82 MuleEvent event = new DefaultMuleEvent(request, ep.getExchangePattern(), new DefaultMuleSession(
83 flowConstruct, connector.getMuleContext()));
84
85 MuleEvent sourceEvent = sourceMessageProcessor.process(event);
86 if (isNewMessage(sourceEvent))
87 {
88 routeMessage(sourceEvent.getMessage());
89 }
90 else
91 {
92
93 logger.info(String.format("Polling of '%s' returned null, the flow will not be invoked.",
94 sourceMessageProcessor));
95 }
96 }
97
98 @Override
99 protected boolean pollOnPrimaryInstanceOnly()
100 {
101 return true;
102 }
103
104
105
106 protected boolean isNewMessage(MuleEvent sourceEvent)
107 {
108 if (sourceEvent != null && sourceEvent.getMessage() != null)
109 {
110 MuleMessage message = sourceEvent.getMessage();
111 if (message.getPayload().equals(NullPayload.getInstance()))
112 {
113 return false;
114 }
115 else
116 {
117 return true;
118 }
119 }
120 return false;
121 }
122 }