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>
Related
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.
I have already created a simple tcp server with spring integration which keeps a connection alive and responses to each request during the connection.
In that requestMethod, I'm also able to read the MessageHeder to get the connectionId.
Now I want to send messages from the server to the client.
As far as i understood the documentation i need to put the connectionid in the MessageHeader and then send the message. But I can't figure out how to do the latter one. I have the Message ready but how do i send/push it out?
Here is my xml-configuration:
<bean id="lfSerializer" class="org.springframework.integration.ip.tcp.serializer.ByteArrayLfSerializer"/>
<int-ip:tcp-connection-factory
id="socketserver"
type="server"
port="30124"
using-nio="true"
deserializer="lfSerializer"
serializer="lfSerializer"
single-use="false"/>
<int-ip:tcp-inbound-channel-adapter id="inboundServer"
channel="inputChannel"
connection-factory="socketserver"/>
<int-ip:tcp-outbound-channel-adapter id="outboundServer"
channel="outputChannel"
connection-factory="socketserver"
/>
<int:channel id="inputChannel">
<int:interceptors>
<int:wire-tap channel="logger"/>
</int:interceptors>
</int:channel>
<int:channel id="outputChannel">
<int:interceptors>
<int:wire-tap channel="logger"/>
</int:interceptors>
</int:channel>
<int:logging-channel-adapter id="logger" level="DEBUG" log-full-message="true"/>
<int:service-activator input-channel="inputChannel"
output-channel="outputChannel"
ref="echoService"
method="test"/>
<bean id="echoService"
class="com.examples.EchoService" />
I also tried to create a bean and another serviceactivator for output, then autowired that bean and called it's "send" method, but I don't know what to implement in that send-method to send out a message.
If it's a simple request/response scenario use an inbound gateway instead of channel adapters and the framework will take care of the correlation for you. This is used in the sample app. Simply have your POJO method return the reply payload.
If you want to send arbitrary messages to the client (i.e. NOT request/reply but, say, in, out, out, in, out, out, out etc) then, yes, you need to build the messages yourself, inserting the ip_connectionId header.
To send them, there are several options:
Inject outputChannel into your code
#Autowired
private MessageChannel outputChannel;
Use a MessagingTemplate to send to the channel (or simply call its send(Message<?> message) method directly).
Or
Use a MessagingGateway with a void return method and inject the gateway into your code.
EDIT:
Note, that if you want to start sending messages before receiving anything, you can obtain the connection id via the connection opened event.
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.
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.
I have a requirement when I need to write asynchronously to a queue in activemq. I am using Spring Jms to do it. This is the wiring in my spring context file
<bean id="amqProducerConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="${activemq.broker}"/>
</bean>
<bean id="pooledProducerConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop" lazy-init="true">
<property name="connectionFactory" ref="amqProducerConnectionFactory" />
</bean>
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="pooledProducerConnectionFactory" />
</bean>
And in my code...i do..
void sendMessage(JmsTemplate jmsTemplate, String message, String requestQueue) {
def messageCreator = { session ->
session.createTextMessage(message)
} as MessageCreator
jmsTemplate.send(requestQueue, messageCreator)
}
But the above seems to be working synchronously, not asynchrously. Is there anything that I need to add here that makes the process asynchronous(I mean, App 'A' writes to a queue. It should write to the queue and forget, not wait until App 'B' picks it up from the queue and processes it.)
The JmsTemplate send is never synchronous in terms of waiting for a consumer to take a Message of the Queue in the normal case. The send can however be synchronous in that it waits for a response from the Broker indicating that it has received and stored the Message. This is so that you have an indication of success as Queues must ensure that they are reliable. You can configure many things in the ActiveMQConnectionFactory to control this. Setting the options useAsyncSend will enforce that your send don't wait for a Broker ACK if that is what you want. These options are all documented.
You configure these on the connection URI like so: tcp://localhost:61616?jms.useAsyncSend=true
A send can however block if the ActiveMQ Broker's producer flow control kicks in to prevent a Producer from flooding the Broker with messages. This is configurable as well both in terms of disabling it altogether and also in that you can increase the memory limits on the Broker for when this might kick in.
Finally a Producer send could block if the Connection to the broker is lost and you are using the Failover Transport to have your client automatically reconnect.