int-http:inbound-gateway with pollable request channel - java

I'm looking through some code that is based on spring integration and I've noticed that all int-http:inbound-gateways use pollable request channels:
<int-http:inbound-gateway id="someId"
request-channel="queue-channel"
reply-channel="reply-channel"
request-payload-type="java.lang.String"
supported-methods="POST"
path="/rest/notifications"
auto-startup="true" />
<int:channel id="queue-channel" datatype="java.lang.String">
<int:queue capacity="100" />
</int:channel>
An explicit poller is specified in the configuration:
<int:poller id="mainSystemPoller" default="true" fixed-delay="500" max-messages-per-poll="1">
<int:transactional transaction-manager="transactionManager" propagation="REQUIRES_NEW" isolation="DEFAULT"/>
</int:poller>
So the very first channel up the stream is pollable. What are the benefits of using this approach? Does it just give us more flexibility over the business flow(transactional configurations, queue capacity, etc)?

Without the transaction configuration, there is little, if any, value in using a poller there because the http thread will wait in the gateway for the reply anyway.
However, in your case, it will start a transaction and cause everything downstream of the gateway to run in a transaction. But, as configured, this will single-thread your requests; you would need a task executor to handle multiple concurrent requests.
There are other ways to run concurrent web requests in transactions; you can use a transactional gateway instead and then the flow will run on on the web container thread, with the concurrency managed by your web container.

Related

Spring Integration chain threads

I try to use Spring Integration and don't find in doc some info about chain threads. I have xml:
<task:executor
id="worker"
pool-size="5"
queue-capacity="5"
rejection-policy="CALLER_RUNS"/>
<int:channel id="inputChannel" datatype="javax.xml.bind.JAXBElement">
<int:dispatcher task-executor="worker"/>
</int:channel>
<int:chain input-channel="inputChannel" id="someChain">
<int-ws:header-enricher>
<int-ws:soap-action value="${someValue}"/>
</int-ws:header-enricher>
<int:header-enricher>
<int:header name="url" value="${someUri}"/>
</int:header-enricher>
<int-ws:outbound-gateway uri="${someUri}"
ignore-empty-responses="true"
requires-reply="true"
marshaller="someMarshaller"
interceptors="someMessageInterceptors">
<int-ws:request-handler-advice-chain>
<ref bean="integrationInterceptor" />
</int-ws:request-handler-advice-chain>
</int-ws:outbound-gateway>
</int:chain>
And my question: Will all endpoints in that chain work consequentially but all in separate threads? Will gateway response and request work in separate threads? Can i use ThreadLocal for save some info in one chain endpoint and use that info in next endpoint ot that chain?
Yes, each component in the chain will run on the same thread.
Yes, you can use ThreadLocals to convey data between components, but often message headers are used for that purpose instead.

Spring integration - need immediate response from webservice gateway with asynchronous processing in backend

We have a requirement where the external client are calling one of our spring integration inbound webservice gateway and we need to acknowledge them right away with a generic OK status. Meanwhile, the payload will be submitted to a channel for asynchronous processing and we don't need the response(email will be sent based on some business validation).
We tried using asynchronous gateway and executor channel(with task executor), but can't figure out how to make the gateway respond immediately. Both the cases it is working as a one way webservice with no response.
Configuration:
<context:component-scan base-package="com.myapp.springintegration" />
<channel id="helloWorldRequestChannel">
<dispatcher task-executor="taskExecutor"/>
</channel>
<channel id="helloWorldReplyChannel"/>
<task:executor id="taskExecutor" pool-size="2"/>
<gateway id="helloServiceGateway"
service-interface="com.myapp.springintegration.HelloService"
default-request-channel="helloWorldRequestChannel" default-reply-channel="helloWorldReplyChannel"/>
<service-activator input-channel="helloWorldRequestChannel" ref="helloServiceImpl" method="hello" />
I don't see any <int-ws:inbound-gateway> but the requirements may be implemented like this:
<int-ws:inbound-gateway request-channel="requestChannel"/>
<int:recipient-list-router input-channel="requestChannel">
<int:recipient channel="response" />
<int:recipient channel="process" />
</int:recipient-list-router>
<int:transformer input-channel="response" expression="'OK'"/>
<int:channel id="process">
<int:dispatcher task-executor="testExecutor"/>
</int:channel>
You send request to the recipient-list-router, that distributes your message to two other channels. The first one (response) just returns simple reply to the WS immediately. The second (process) shifts the message to the separate Thread, so the subscriber to that channel will do its work not blocking the WS Thread.
Hope I am clear.

Spring intgeration - recipientlistrouter - parallel processing

How does I can achieve parallel processing in spring-integration with recipient-list-router.
My goal is the router has to be content based and sending messages to various channel on parallel processing just like multicast. I tried multicast in Camel with camel-spring-integration but couldn't configure it for content based
Please help if something cane be done
Thanks
If I'm understanding the question right, you just need to use publish subscribe channels as the output channels of the router. The router will direct the message to the right pubsub channel based on content, then all handlers subscribed to that channel will be executed in parallel, assuming the output channel task executor is configured to have multiple threads.
<int:publish-subscribe-channel id="channel1" task-executor="someExecutor"/>
<int:publish-subscribe-channel id="channel2" task-executor="someExecutor"/>
<int:recipient-list-router id="customRouter" input-channel="routingChannel">
<int:recipient channel="channel1" selector-expression="payload.equals('foo')"/>
<int:recipient channel="channel2" selector-expression="headers.containsKey('bar')"/>
</int:recipient-list-router>
The above recipient list router configuration is copied from section 5.1 of the Spring Integration reference manual.
I have achieved similar sort of requirements by extending the Spring-Integrations's RecipientListRouter as follows:
public class CententBasedRecipientListRouter extends RecipientListRouter {
#Override
protected void handleMessageInternal(final Message<?> message) {
......
}
}
in overridden method, you can implement any customized content based routing logic as per your requirements.
This custom router can be configured as follows:
<bean id="customRecipientListRouter" class="CententBasedRecipientListRouter">
<property name="channels">
<list>
<ref bean="parallelProcessingChannel1"/>
<ref bean="parallelProcessingChannel2"/>
<ref bean="parallelProcessingChannel3"/>
</list>
</property>
</bean>
<int:router ref="customRecipientListRouter"
input-channel="routingChannel"
default-output-channel="errorChannel" />
In order to achieve Parallel Processing, you can have Task Executor channels as follows:
<int:channel id="parallelProcessingChannel1">
<int:dispatcher task-executor="threadPoolExecutor"/>
</int:channel>

How to wait for completion of multi-threaded publish-subscribe-channel

I have a Spring Integration project where I want to process a message concurrently through multiple actions. So I have set up a publish-subscribe-channel with a task-executor. However I want to wait for all processing to complete before moving on. How would I do this?
<publish-subscribe-channel id="myPubSub" task-executor="my10ThreadPool"/>
<channel id="myOutputChannel"/>
<service-activator input-channel="myPubSub" output-channel="myOutputChannel"
ref="beanA" method="blah"/>
<service-activator input-channel="myPubSub" output-channel="myOutputChannel"
ref="beanB" method="blah"/>
<service-activator id="afterThreadingProcessor" input-channel="myOutputChannel" .../>
So in the above case, I want my afterThreadingProcessor to be invoked only once after both beanA and beanB have completed their work. However, in the above afterThreadingProcessor will be invoked twice.
Add apply-sequence="true" to the pub-sub channel (this adds default correlation data to the messages, including correlationId, sequenceSize, and sequenceNumber and allows default strategies to be used on downstream components).
Add an <aggregator/> before afterThreadingProcessor and route the output from the two <service-activator/>s to it.
Add a <splitter/> after the aggregator - the default splitter will split the collection made by the aggregator into two messages.
afterThreadingProcessor will be invoked once for each message on the second thread that completes its work.
You can make the configuration easier by using a chain...
<chain input-channel="myOutputChannel">
<aggregator />
<splitter />
<service-activator id="afterThreadingProcessor" input-channel="myOutputChannel" .../>
</chain>
To make a single call to the final service, just change your service to take a Collection<?> instead of adding the splitter.
EDIT:
In order to do what you want in comment #3 (run the final service on the original thread), something this should work...
<int:channel id="foo" />
<int:service-activator ref="twoServicesGateway" input-channel="foo"
output-channel="myOutputChannel" />
<int:gateway id="twoServicesGateway" default-request-channel="myPubSub"/>
<int:publish-subscribe-channel id="myPubSub" task-executor="my10ThreadPool"
apply-sequence="true"/>
<int:service-activator input-channel="myPubSub" output-channel="aggregatorChannel"
ref="beanA" method="blah"/>
<int:service-activator input-channel="myPubSub" output-channel="aggregatorChannel"
ref="beanB" method="blah"/>
<int:aggregator input-channel="aggregatorChannel" />
<int:service-activator id="afterThreadingProcessor" input-channel="myOutputChannel" .../>
In this case, the gateway encapsulates the two other services and the aggregator; the default service-interface is a simple RequestReplyExchanger. The calling thread will wait for the output. Since the aggregator has no output-channel the framework will send the reply to the gateway, and the waiting thread will receive it, return to the <service-activator/> and the result will then be sent to the final service.
You would probably want to put a reply-timeout on the gateway because, by default, it will wait indefinitely and, if one of the services returns a null, no agreggated response will ever be received.
Note that I indented the gateway flow just to show it runs from the gateway, they are NOT child elements of the gateway.
Same kind of behavior can now be achieved using a more cleaner approach introduced in Spring Integration 4.1.0 as an implementation of EIP Scatter-Gather pattern.
Checkout Scatter-Gather example gist:
https://gist.github.com/noorulhaq/b13a19b9054941985109

Spring - Async Queue for sending mail

I have this:
<si:poller max-messages-per-poll="10" id="defaultPoller" default="true">
<si:interval-trigger interval="5000"/>
</si:poller>
<si:channel id="emailIn"/>
<si:channel id="emailOut"/>
<si:service-activator input-channel="emailIn" output-channel="emailOut" ref="mailService" method="recieveMessage"/>
<si:gateway id="gateway" service-interface="com.blah.MailSender" default-request-channel="emailIn"/>
<si:outbound-channel-adapter channel="emailOut" ref="mailService" method="recieveMessage" />
And I thought what I was configuring was an async Queue. I want to be able to drop messages onto it, and have a nother thread pick them up and process then later. However, at the momment it seems to do it in a synchronous way.
Am i doing it wrong (obvioulsy yes), but wondering if there is something i'm missing in this config, or whether i just have the wrong approach?
Cheers
By default all channels in Spring Integration are synchronous. This is a conscious design decision that will help you keep transaction boundaries and security contexts for example. When you want to do asynchronous hand-off you should add a task executor to your dispatcher or a queue to your channel:
<channel>
<dispatcher task-executor="pool"/>
</channel>
<channel>
<queue capacity="10"/>
</channel>
Look at channel configurations in the reference guide for some details on dispatchers and queues. See also the section on DirectChannel and the section on ExecutorChannel below that one.

Categories