简体   繁体   中英

Spring Integration - Exceptions and retries

My setup:
I have a message daemon using ActiveMQ which will consume JSON messages.
The publisher of JSON messages adds type header with value, for ex, com.example.Foo which is the type of the json message. I use this to transform json to pojo.

Spring config:
Once the message is received, these are the steps it goes through:
1. Transformer: Transforms json to pojo
2. Payload type router: Based on the type of the pojo, routes the pojo to appropriate service activator.
3. Service activator: Process the message.

<int:chain input-channel="transformerChannel">
    <int:transformer id="jsonToPojoTransformer" ref="JsonToPojoTransformer" method="transform" />

    <int:payload-type-router default-output-channel="defaultChannel">
        <int:mapping type="com.example.Foo" channel="fooHandlerChannel"/>
        <int:mapping type="com.example.Bar" channel="barHandlerChannel"/>
    </int:payload-type-router>
</int:chain>

<int:service-activator input-channel="fooHandlerChannel" ref="fooHandler" method="onMessage"/>
<int:service-activator input-channel="barHandlerChannel" ref="barHandler" method="onMessage"/>

Service activator definition:

public class FooHandler {
    public void onMessage(Foo foo) {...}
}

Problem:

  1. I want to know how to access the message headers in the service activator. It seems like the service activator does not have access to the message headers since the transformer is returning a pojo.

  2. Lets say the service activator is unable to call a down stream rest service for whatever reason. I want to skip processing this message now and I want to retry this message later. Or lets say there was an exception in processing this message. I want to retry processing this message after some delay. How do I accomplish this?

--edit--
Removed details to reduce question size as per Artem's comment.

Please, try do not make so long topics here in SO. It's hard to answer particular question if there are to many of them.

Absolutely not clear why you can't get access to header from your service activator method. You can accept the whole Message<> , and call its getHeaders(). You can use getHeaders(). You can use @Headers annotation on the Map arg to get headers from the message. You can use arg to get headers from the message. You can use @Header` annotation to extract exactly particular header from the message.

Even if your transformer method returns just a POJO that doesn't mean that it isn't wrapped to the Message with the headers from requestMessage. If you need to return specific header alongside with your POJO, you should create Message yourself, using MessageBuilder and don't forget to copy requestMessage headers, just because transformer doesn't copy request headers if the whole message is returned.

You have to support TX on your JMS consumer, so that RuntimeException will lead to the rollback and, therefore, redelivery eventually. And you should ensure that all the flow is performed in the same thread. Otherwise TX is committed and message is acked on the broker. The same happens when you don't have transactions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM