I have http outbound gateway that post json message to rest service, now the rest will response with http error status with json message type in case if there is any error which should captured by our application. in happy scenario which post json message to that rest service and got http success status and json message also should captured with our application.
Now what I achieved through spring integration I was able to send message and got success response and capture it but in error status the spring behavior it throw an exception how can I change the behavior?
also how can make retry if there is any timeout ?
below the http outbound gateway configuration
<int-http:outbound-gateway request-channel="aggregatorChannel" reply-channel="responseChannel" charset="UTF-8"
url="http://localhost:8090/receiveGateway" http-method="POST" reply-timeout="180000">
<int-http:request-handler-advice-chain >
<int:retry-advice max-attempts="5" recovery-channel="aggregatorChannel" >
<int:exponential-back-off initial="1000" multiplier="5.0" maximum="600000" />
</int:retry-advice>
</int-http:request-handler-advice-chain>
</int-http:outbound-gateway>
the above configuration will retry on the error message which I don't want retry on that, I want to retry if there is only timeout.
What do you want to change the behavior to (when there's an error)?
If you wire up the RetryTemplate for the retry interceptor manually, as a <bean/>, you can specify in the RetryPolicy which exceptions are retryable.
EDIT:
It's not entirely clear what you are trying to do, but maybe this will point you in the right direction...
<int-http:outbound-gateway request-channel="requestChannel"
url="http://localhost:8080/http/receiveGateway"
http-method="POST"
request-factory="requestFactory"
expected-response-type="java.lang.String">
<int-http:request-handler-advice-chain >
<int:retry-advice max-attempts="5" recovery-channel="foo">
<int:exponential-back-off initial="1000" multiplier="1.1" maximum="600000" />
</int:retry-advice>
<bean class="foo.MyExceptionAdvice" />
</int-http:request-handler-advice-chain>
</int-http:outbound-gateway>
<bean id="requestFactory" class="org.springframework.http.client.SimpleClientHttpRequestFactory">
<property name="connectTimeout" value="5000"/>
<property name="readTimeout" value="5000"/>
</bean>
If the custom exception advice throws an exception, the message will be retried and eventually sent to he recovery channel. If the custom advice consumes the error, it can send a return value. I have put a simple on this gist.
Related
Summary - Message loss during systematic shutdown of application.
I have a application written in spring integration and I am consuming requests from external systems using 'jms:message-driven-channel-adapter'. This is the configuration of the channel adapter -
<jms:message-driven-channel-adapter id="InboundAdapter" destination="InQueue"
connection-factory="connectionFactory"
channel="responseChannel"
error-channel="errorChannel"
acknowledge="transacted"
receive-timeout="50000"
auto-startup="true"/>
my response channel looks like this -
<int:chain id="processorChain" input-channel="responseChannel">
<int:service-activator method="doProcess" ref="inputProcessor" />
<int:router id="inputRouter" method="route">
<int:mapping value="REQUIRED" channel="builderChannel"/>
<int:mapping value="NOT_REQUIRED" channel="TerminateChannel"/>
<bean id="Router" class="xxx.xxx.Router">
</bean>
</int:router>
</int:chain>
Now when i perform 'kill -9 pid', then I see that the message is rolled back to the queue and it is all good. But when I am performing 'kill pid', than the message is lost somewhere. I have enabled teh jms logs and I can see that JMS listener is waiting for the message in transit to complete before closing the JMS consumer but still I don't the message rolling back to my queue. This is the logs snippet that I see in my logs
trace: 15.11.18 15:42:46.619 [Thread-10] DEBUG org.springframework.jms.listener.DefaultMessageListenerContainer - Waiting for shutdown of message listener invokers
trace: 15.11.18 15:42:46.619 [Thread-10] DEBUG org.springframework.jms.listener.DefaultMessageListenerContainer - Still waiting for shutdown of 1 message listener invokers (iteration 0)
After this logs, it is calling the service activators which are defined in the response channel.
Can someone please shed some light on above behavior, I was expecting that when we issue kill command than all messages which are in transit should be rolled back to the queue, instead of this, it is trying to call the activators defined with in channel and after calling last component which is router, it just finishes.
Any help on this topic will be really helpful!!
After some more debugging and banging my head with the system I found the exact scenario where it is happening. Let me put it thru to see if it makes sense. When a systematic shutdown is triggered, JMS is waiting for the messages in transit to finish before stopping the application, till this point everything is good. Currently this chain is being executed -
<int:chain input-channel="inputchain">
<int:transformer id="xxx" method="transform">
<bean class="xxx" />
</int:transformer>
<int:service-activator id="xxx" method="doProcess">
<bean class="xx">
<constructor-arg ref="xxx"/>
</bean>
</int:service-activator>
<int:service-activator id="xxx" ref="rulesProcessor" method="doProcess"/>
<int:service-activator id="xxx" ref="xxx" method="doProcess"/>
<!-- Existing Flow Continues -->
<int:router id="xxxRequiredRouter" method="xxxRequired">
<int:mapping value="Required" channel="firstChannel"/>
<int:mapping value="NotRequired" channel="secondChannel"/>
<bean id="xxxRouter" class="xxx.Router" />
</bean>
</int:router>
</int:chain>
<int:chain input-channel="secondChannel">
some logic
</int:chain>
So the thread finishes this chain and exits gracefully. It doesn't call my next chain 'secondChannel'. My transaction boundary doesn't finish on this chain 'inputchain', it finishes at the end of secondChannel. So I have not comnmitted this transction to database as transaction boundary is at the end of next chain, so this is not available in DB and application is thinking that chain execution is finished so it is complete, so it is not rolled back to teh queue as well. So in teh end I don't have this message in my database and it is not in queue.
So is this the case that only the chain which is being executed when shutdown was triggered will finish and it will not delegate processing to subsequent chains?
A SIGTERM kill will wait for the thread to complete its work.
Eventually the container will interrupt it, but that will only help if it's doing something that's interruptible.
I recently update from Spring Integration 4.1.3 to 4.2.6 and noticed that I started getting a 500 response from an http:inbound-gateway.
Upon investigation this was due to an empty reply and the gateway interpreting this as a timeout in MessagingGatewaySupport
if (reply == null && this.errorOnTimeout) {
This makes sense so I changed this to an http:inbound-channel-adapter and it solves this problem, but then the error handling doesn't behave as expected.
I previously had and error-channel on the gateway with
<int-http:inbound-gateway id="inboundGateway" request-channel="httpInChannel" reply-channel="httpResponseChannel" path="/**/status*" supported-methods="POST" error-channel="httpErrorChannel"/>
<int:chain input-channel="httpInChannel" output-channel="serviceChannel">
...
</int:chain>
<int:chain input-channel="httpErrorChannel">
<int:header-enricher>
<int:header name="http_statusCode" value="500" />
</int:header-enricher>
<int:transformer expression="payload.localizedMessage" />
</int:chain>
<int:service-activator input-channel="serviceChannel" ref="someController" method="someVoidMethod"/>
I suspected it may not work but I modified this slightly to
<int-http:inbound-channel-adapter id="inboundAdapter" channel="httpInChannel" path="/**/status*" supported-methods="POST" error-channel="httpErrorChannel"/>
<int:chain input-channel="httpInChannel" output-channel="serviceChannel">
...
</int:chain>
<int:chain input-channel="httpErrorChannel">
<int:header-enricher>
<int:header name="http_statusCode" value="500" />
</int:header-enricher>
<int:transformer expression="payload.localizedMessage" />
</int:chain>
<int:service-activator input-channel="serviceChannel" ref="someController" method="someVoidMethod"/>
Now, it works fine for a normal valid POST request, but if I send an invalid message that errors I get a 500 response with full error stack in response (have tried changing the status code in the header enricher too). The error is
org.springframework.messaging.core.DestinationResolutionException: no output-channel or replyChannel header available
which makes sense as the error-channel does not have an output (although it doesn't in the docs either http://docs.spring.io/spring-integration/docs/4.3.0.RELEASE/reference/htmlsingle/#http-response-statuscode).
Is there a way to change the error response for the inbound adapter in a similar way to that of the gateway?
I think it's a bug, please open a JIRA Issue.
As a work around, in the main flow, just after the adapter, add a mid-flow gateway...
<int:service-activator ref="gw" input-channel="httpInChannel" >
<int:gateway id="gw" error-channel="httpErrorChannel"
default-request-channel="nextChannelInMainFlow" default-reply-timeout="0" />
The gateway is like a try/catch block around the flow.
The reply timeout is important since you do not expect a reply.
I have a jms-inbound-gateway that reads requests from a WebsphereMQ broker, passes them though my integration system and then replies with a response message.
I need to log the messages with the jms_messageId and jms_correlationId headers set, so I can match request/reply messages in the log file (and show it to my client when he says that my response does not have the correct jms_correlationId)
Is there a way to intercept the method producer.sendReply(...) after the correlationId header is set?
There is no need to intercept there; the headers are available in the Spring Integration messsage in the gateway reply message before it gets to the gateway.
Simply make the reply-channel a publish-subscribe-channel and add a <logging-channel-adapter/> that has it as its input channel.
The reply message will be sent to both the gateway and the logger.
If you are using the default mechanism to route the reply (no output-channel on your last integration component), simply add an output-channel and route to the reply channel.
This is the gist of my solution after Gary's input:
<jms:inbound-gateway
id="inboundDestination"
connection-factory="connectionFactory"
request-destination="nmRequestsQueue"
request-channel="request-begin"
reply-channel="request-end"
error-channel="normaErrorChannel"
concurrent-consumers="1"
acknowledge="transacted" />
<int:logging-channel-adapter id="request-response-logger"
log-full-message="true"
level="DEBUG"
logger-name="com.audaxys.si.messages" />
<int:channel id="request-begin">
<int:interceptors>
<int:wire-tap channel="request-response-logger" />
</int:interceptors>
</int:channel>
<int:chain input-channel="request-begin" output-channel="request-end">
... Do Stuff ...
</int:chain>
<int:publish-subscribe-channel id="request-end">
<int:interceptors>
<int:wire-tap channel="request-response-logger" />
</int:interceptors>
</int:publish-subscribe-channel>
I'm relatively new to spring integration but I have been tasked with implementing a tcp gateway that needs to:
Listen for messages on a socket
Receive a message
Process the message and write some data to a queue
Return a response message to the original socket.
In my experience with spring integration, the message flow has not been bi-directional. I've only configured routers to listen, handle messages, and output to queue/topic. In this case, though, I need to accept messages and return a response while also forwarding on some message to a queue. Suggestions?
This is my integration xml so far.
<int:chain input-channel="tcpChannel">
<int:service-activator ref="tcpHandler" method="handleInput" />
</int:chain>
<int-ip:tcp-inbound-gateway
id="tcpGateway"
connection-factory="tcpServer"
request-channel="tcpChannel" />
How can I have the output of handleInput be forwarded to a queue but also have some response sent back from the gateway?
Edit: After the conversation below with Gary, this seems to be the pattern we want to follow:
<int-ip:tcp-inbound-gateway id="tcpGateway"
connection-factory="tcpServer"
request-channel="tcpChannel"
reply-channel="tcpReplyChannel"/>
<int:publish-subscribe-channel id="tcpChannel" />
<int:chain input-channel="tcpChannel">
<!-- int:json-to-object-transformer type="com.heb.revo.events.RxPosCredit" /-->
<int:service-activator ref="tcpHandler" method="handleInputToQueue" />
<jms:outbound-channel-adapter destination-name="${queue.response}" />
</int:chain>
<int:service-activator id="tcpResponseHandler"
ref="tcpHandler" method="replyToSocket"
input-channel="tcpChannel"
output-channel="tcpReplyChannel" />
<int:publish-subscribe-channel id="tcpReplyChannel" />
Since your chain has no output-channel the framework will route the return value of the handleInput method back to the gateway automatically.
If you want to capture the result and send it somewhere else (as well as a reply), create a <int:publish-subscribe-channel id="foo"/>, set the output-channel of the chain to foo, set the reply-channel on the gateway to foo, and subscribe another endpoint to foo (as an input-channel.
we are using Spring Integration 2.1 for persisting messages into database sent by clients.
There is a queue which will be filled by a custom adapter. The configured service activator polls this queue and releases the message to a spring managed #Repository bean. All errors will be captured to an error channel and will be handled by a service. The configuration works so far fine.
My concern is that if the database is not available the service-activators polls all incoming message from the queue and puts them into the error channel. Is there a way to prevent the service-activator to poll the message if the database is obviously not available, for example by sending a test query ?
My configuraton:
<int:channel id="inChannel">
<int:queue />
</int:channel>
<bean id="service" class="some.service.Service" />
<int:service-activator ref="service"
method="write" input-channel="inChannel">
<int:poller fixed-rate="100" task-executor="srvTaskExecutor"
receive-timeout="90" error-channel="errChannel" />
</int:service-activator>
<task:executor id="srvTaskExecutor" pool-size="2-10"
queue-capacity="0" rejection-policy="DISCARD" />
<int:channel id="errChannel" />
<int:service-activator input-channel="errChannel"
ref="errorService" method="write"/>
Regards.
If you give the polling service-activator an "id", you can refer to that instance and call start() or stop() on it based on the DB being available or not. Most likely you'd want to set auto-startup="false" on that service-activator as well.
Additionally, you can even define a "control-bus" element and then send messages like "myActivator.start()" and "myActivator.stop()" to that control bus' input-channel.
Hope that helps,
Mark