Claim Check

在前面的部分中,我们介绍了几个内容扩充组件,它们可以帮助您处理消息缺少数据的情况。我们还讨论了内容筛选,这允许您从消息中删除数据项。但是,有时我们希望暂时隐藏数据。例如,在分布式系统中,我们可能会收到带有非常大的有效负载的消息。一些间歇性消息处理步骤可能不需要访问此有效负载,而另一些可能只需要访问某些头,因此在每个处理步骤中携带大的消息有效负载可能会导致性能下降、产生安全风险,并且可能使调试变得更加困难。

In earlier sections, we covered several content enricher components that can help you deal with situations where a message is missing a piece of data. We also discussed content filtering, which lets you remove data items from a message. However, there are times when we want to hide data temporarily. For example, in a distributed system, we may receive a message with a very large payload. Some intermittent message processing steps may not need access to this payload and some may only need to access certain headers, so carrying the large message payload through each processing step may cause performance degradation, may produce a security risk, and may make debugging more difficult.

store in library(或索赔支票)模式描述了一种机制,它允许你在众所周知的位置存储数据,同时仅保留一个指针(索赔支票),指向数据的所在位置。你可以将该指针作为一条新消息的有效负载传递,从而让消息流中的任何组件在其需要时立即获取实际数据。此方法非常类似于认证邮件流程,你可以在信箱中收到索赔支票,然后必须去邮局索取实际包裹。它还与航班或酒店中的行李领取是一样的。

The store in library (or claim check) pattern describes a mechanism that lets you store data in a well known place while maintaining only a pointer (a claim check) to where that data is located. You can pass that pointer around as the payload of a new message, thereby letting any component within the message flow get the actual data as soon as it needs it. This approach is very similar to the certified mail process, where you get a claim check in your mailbox and then have to go to the post office to claim your actual package. It is also the same idea as baggage claim after a flight or in a hotel.

Spring Integration 提供两种声明检查转换器:

Spring Integration provides two types of claim check transformers:

  • Incoming Claim Check Transformer

  • Outgoing Claim Check Transformer

有方便基于名称空间的机制可用于配置它们。

Convenient namespace-based mechanisms are available to configure them.

Incoming Claim Check Transformer

传入声明检查转换器通过将其存储在由其 message-store 属性标识的消息存储中来转换传入消息。以下示例定义了一个传入声明检查转换器:

An incoming claim check transformer transforms an incoming message by storing it in the message store identified by its message-store attribute. The following example defines an incoming claim check transformer:

<int:claim-check-in id="checkin"
        input-channel="checkinChannel"
        message-store="testMessageStore"
        output-channel="output"/>

在前面的配置中,在 input-channel 上接收到的消息将持久保存到由 message-store 属性标识且用生成 ID 编制索引的消息存储。该 ID 是该消息的声明检查。声明检查还成为发送到 output-channel 的新(转换)消息的有效负载。

In the preceding configuration, the message that is received on the input-channel is persisted to the message store identified with the message-store attribute and indexed with a generated ID. That ID is the claim check for that message. The claim check also becomes the payload of the new (transformed) message that is sent to the output-channel.

现在,假设在某个时刻您确实需要访问实际消息。您可以手动访问消息存储并获取消息的内容,或者可以使用相同的方法(创建转换器),但现在您可以使用传出声明检查转换器将声明检查转换为实际消息。

Now, assume that at some point you do need access to the actual message. You can access the message store manually and get the contents of the message, or you can use the same approach (creating a transformer) except that now you transform the Claim Check to the actual message by using an outgoing claim check transformer.

以下清单提供了传入声明检查转换器的所有可用参数的概述:

The following listing provides an overview of all available parameters of an incoming claim check transformer:

<int:claim-check-in auto-startup="true"             1
                    id=""                           2
                    input-channel=""                3
                    message-store="messageStore"    4
                    order=""                        5
                    output-channel=""               6
                    send-timeout="">                7
    <int:poller></int:poller>                       8
</int:claim-check-in>
1 Lifecycle attribute signaling whether this component should be started during application context startup. It defaults to true. This attribute is not available inside a Chain element. Optional.
2 ID identifying the underlying bean definition (MessageTransformingHandler). This attribute is not available inside a Chain element. Optional.
3 The receiving message channel of this endpoint. This attribute is not available inside a Chain element. Optional.
4 Reference to the MessageStore to be used by this claim check transformer. If not specified, the default reference is to a bean named messageStore. Optional.
5 Specifies the order for invocation when this endpoint is connected as a subscriber to a channel. This is particularly relevant when that channel uses a failover dispatching strategy. It has no effect when this endpoint is itself a polling consumer for a channel with a queue. This attribute is not available inside a Chain element. Optional.
6 Identifies the message channel where the message is sent after being processed by this endpoint. This attribute is not available inside a Chain element. Optional.
7 Specifies the maximum amount of time (in milliseconds) to wait when sending a reply message to the output channel. Defaults to 30 seconds. This attribute is not available inside a Chain element. Optional.
8 Defines a poller. This element is not available inside a Chain element. Optional.

Outgoing Claim Check Transformer

传出声明检查转换器允许您将带有声明检查有效负载的消息转换为具有原始内容作为其有效负载的消息。

An outgoing claim check transformer lets you transform a message with a claim check payload into a message with the original content as its payload.

<int:claim-check-out id="checkout"
        input-channel="checkoutChannel"
        message-store="testMessageStore"
        output-channel="output"/>

在前面的配置中,在 input-channel 上收到的消息应具有声明检查作为其有效负载。传出声明检查转换器通过使用由提供的声明检查标识的消息查询消息存储,将其转换为带有原始有效负载的消息。然后,它将新签出的消息发送到 output-channel

In the preceding configuration, the message received on the input-channel should have a claim check as its payload. The outgoing claim check transformer transforms it into a message with the original payload by querying the message store for a message identified by the provided claim check. It then sends the newly checked-out message to the output-channel.

以下列表提供了传出声明检查转换器的所有可用参数的概述:

The following listing provides an overview of all available parameters of an outgoing claim check transformer:

<int:claim-check-out auto-startup="true"             1
                     id=""                           2
                     input-channel=""                3
                     message-store="messageStore"    4
                     order=""                        5
                     output-channel=""               6
                     remove-message="false"          7
                     send-timeout="">                8
    <int:poller></int:poller>                        9
</int:claim-check-out>
1 Lifecycle attribute signaling whether this component should be started during application context startup. It defaults to true. This attribute is not available inside a Chain element. Optional.
2 ID identifying the underlying bean definition (MessageTransformingHandler). This attribute is not available inside a Chain element. Optional.
3 The receiving message channel of this endpoint. This attribute is not available inside a Chain element. Optional.
4 Reference to the MessageStore to be used by this claim check transformer. If not specified, the default reference is to a bean named messageStore. Optional.
5 Specifies the order for invocation when this endpoint is connected as a subscriber to a channel. This is particularly relevant when that channel is using a failover dispatching strategy. It has no effect when this endpoint is itself a polling consumer for a channel with a queue. This attribute is not available inside a Chain element. Optional.
6 Identifies the message channel where the message is sent after being processed by this endpoint. This attribute is not available inside a Chain element. Optional.
7 If set to true, the message is removed from the MessageStore by this transformer. This setting is useful when Message can be “claimed” only once. It defaults to false. Optional.
8 Specifies the maximum amount of time (in milliseconds) to wait when sending a reply message to the output channel. It defaults to 30 seconds. This attribute is not available inside a Chain element. Optional.
9 Defines a poller. This element is not available inside a Chain element. Optional.

Claim Once

有时,特定消息只能声明一次。作为一个类比,考虑处理飞机行李的过程。您正在办理登机手续并领取行李。一旦行李被认领,如果没有先将行李重新托运,就不能再次认领。为了适应这种情况,我们在 claim-check-out 转换器上引入了 remove-message 布尔属性。默认情况下此属性设置为 false。但是,如果设置为 true,则声明的消息将从 MessageStore 中删除,因此无法再次声明。

Sometimes, a particular message must be claimed only once. As an analogy, consider process of handling airplane luggage. You’re checking in your luggage on departure and claiming it on arrival. Once the luggage has been claimed, it can not be claimed again without first checking it back in. To accommodate such cases, we introduced a remove-message boolean attribute on the claim-check-out transformer. This attribute is set to false by default. However, if set to true, the claimed message is removed from the MessageStore so that it cannot be claimed again.

此特性在存储空间方面有影响,尤其是在基于 MapSimpleMessageStore 的内存情况下,如果未能删除消息,最终可能会导致 OutOfMemoryException。因此,如果您不希望进行多次声明,我们建议您将 remove-message 属性的值设置为 true。以下示例显示了如何使用 remove-message 属性:

This feature has an impact in terms of storage space, especially in the case of the in-memory Map-based SimpleMessageStore, where failing to remove messages could ultimately lead to an OutOfMemoryException. Therefore, if you do not expect multiple claims to be made, we recommend that you set the remove-message attribute’s value to true. The following example show how to use the remove-message attribute:

<int:claim-check-out id="checkout"
        input-channel="checkoutChannel"
        message-store="testMessageStore"
        output-channel="output"
        remove-message="true"/>

A Word on Message Store

尽管我们很少关心声明检查的详细信息(只要它们有效),您应该知道 Spring 集成中实际声明检查(指针)的当前实现使用 UUID 来确保唯一性。

Although we rarely care about the details of the claim checks (as long as they work), you should know that the current implementation of the actual claim check (the pointer) in Spring Integration uses a UUID to ensure uniqueness.

org.springframework.integration.store.MessageStore 是用于存储和检索消息的策略接口。Spring 集成提供了两个方便的实现:

org.springframework.integration.store.MessageStore is a strategy interface for storing and retrieving messages. Spring Integration provides two convenient implementations of it:

  • SimpleMessageStore: An in-memory, Map-based implementation (the default, good for testing)

  • JdbcMessageStore: An implementation that uses a relational database over JDBC