简体   繁体   English

Spring集成-异常和重试

[英]Spring Integration - Exceptions and retries

My setup: 我的设置:
I have a message daemon using ActiveMQ which will consume JSON messages. 我有一个使用ActiveMQ的消息守护程序,它将使用JSON消息。
The publisher of JSON messages adds type header with value, for ex, com.example.Foo which is the type of the json message. JSON消息的发布者添加type标头和值,例如json消息的类型com.example.Foo I use this to transform json to pojo. 我用它来将json转换为pojo。

Spring config: 春季配置:
Once the message is received, these are the steps it goes through: 收到消息后,将按照以下步骤进行操作:
1. Transformer: Transforms json to pojo 1.变压器:将json转换为pojo
2. Payload type router: Based on the type of the pojo, routes the pojo to appropriate service activator. 2.有效负载类型路由器:根据pojo的类型,将pojo路由到适当的服务激活器。
3. Service activator: Process the message. 3.服务激活器:处理消息。

<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. 由于转换器正在返回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. 根据Artem的评论,删除了详细信息以减小问题的大小。

Please, try do not make so long topics here in SO. 请,不要在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 您可以接受整个Message<> ,并调用其getHeaders(). You can use getHeaders(). You can use @Headers annotation on the Map arg to get headers from the message. You can use getHeaders(). You can use annotation on the Map arg to get headers from the message. You can use annotation on the getHeaders(). You can use @Headers annotation on the 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. arg to get headers from the message. You can use @ Header`注释从邮件中提取确切的特定标题。

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. 即使您的转换器方法仅返回一个POJO,也不意味着它不会被带有requestMessage头的Message包装到Message 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. 如果您需要随POJO一起返回特定的标头,则应该使用MessageBuilder自己创建Message ,并且不要忘记复制requestMessage标头,因为在返回整个消息时,转换器不会复制请求标头。

You have to support TX on your JMS consumer, so that RuntimeException will lead to the rollback and, therefore, redelivery eventually. 您必须在JMS使用者上支持TX,以便RuntimeException会导致回滚,并因此最终重新交付。 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. 否则,将提交TX,并在代理上确认消息。 The same happens when you don't have transactions. 当您没有交易时,也会发生同样的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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