訊息歷史

訊息架構的主要優點是鬆散耦合,參與元件彼此之間沒有任何感知。這一事實本身就使得應用程式極其靈活,允許您更改元件而不影響流程的其餘部分、更改訊息路由、更改訊息消費方式(輪詢與事件驅動)等等。然而,當出現問題時,這種樸素的架構風格可能難以除錯。在除錯時,您可能希望儘可能多地獲取關於訊息的資訊(其來源、它經過的通道以及其他詳細資訊)。

訊息歷史是幫助實現這一目標的模式之一,它提供了一個選項,讓您可以維護訊息路徑的某些層面的感知,無論是用於除錯目的還是用於維護審計跟蹤。Spring Integration 提供了一種簡單的方法來配置您的訊息流以維護訊息歷史,方法是向訊息新增一個頭,並在訊息每次透過被跟蹤的元件時更新該頭。

訊息歷史配置

要啟用訊息歷史,您只需在配置中定義 message-history 元素(或 @EnableMessageHistory),如以下示例所示

  • Java

  • XML

@Configuration
@EnableIntegration
@EnableMessageHistory
<int:message-history/>

現在,每個命名元件(定義了“id”)都會被跟蹤。框架會在您的訊息中設定“history”頭。它的值是一個 List<Properties>

考慮以下配置示例

  • Java

  • XML

@MessagingGateway(defaultRequestChannel = "bridgeInChannel")
public interface SampleGateway {
   ...
}

@Bean
@Transformer(inputChannel = "enricherChannel", outputChannel="filterChannel")
HeaderEnricher sampleEnricher() {
    HeaderEnricher enricher =
           new HeaderEnricher(Collections.singletonMap("baz", new StaticHeaderValueMessageProcessor("baz")));
    return enricher;
}
<int:gateway id="sampleGateway"
    service-interface="org.springframework.integration.history.sample.SampleGateway"
    default-request-channel="bridgeInChannel"/>

<int:header-enricher id="sampleEnricher" input-channel="enricherChannel" output-channel="filterChannel">
    <int:header name="baz" value="baz"/>
</int:header-enricher>

上述配置生成了一個簡單的訊息歷史結構,輸出類似於以下內容

[{name=sampleGateway, type=gateway, timestamp=1283281668091},
 {name=sampleEnricher, type=header-enricher, timestamp=1283281668094}]

要訪問訊息歷史,您只需訪問 MessageHistory 頭。以下示例顯示瞭如何操作

Iterator<Properties> historyIterator =
    message.getHeaders().get(MessageHistory.HEADER_NAME, MessageHistory.class).iterator();
assertTrue(historyIterator.hasNext());
Properties gatewayHistory = historyIterator.next();
assertEquals("sampleGateway", gatewayHistory.get("name"));
assertTrue(historyIterator.hasNext());
Properties chainHistory = historyIterator.next();
assertEquals("sampleChain", chainHistory.get("name"));

您可能不想跟蹤所有元件。為了將歷史記錄限制在基於其名稱的特定元件,您可以提供 tracked-components 屬性並指定一個逗號分隔的元件名稱和與您要跟蹤的元件匹配的模式列表。以下示例顯示瞭如何操作

  • Java

  • XML

@Configuration
@EnableIntegration
@EnableMessageHistory("*Gateway", "sample*", "aName")
<int:message-history tracked-components="*Gateway, sample*, aName"/>

在前面的示例中,訊息歷史僅針對以“Gateway”結尾、以“sample”開頭或精確匹配名稱“aName”的元件進行維護。

此外,MessageHistoryConfigurer bean 現在透過 IntegrationMBeanExporter(參見 MBean 匯出器)作為 JMX MBean 公開,允許您在執行時更改模式。但是請注意,必須停止 bean(關閉訊息歷史)才能更改模式。此功能可能有助於暫時開啟歷史以分析系統。MBean 的物件名稱是 <domain>:name=messageHistoryConfigurer,type=MessageHistoryConfigurer

在應用程式上下文中,必須只宣告一個 @EnableMessageHistory(或 <message-history/>),作為元件跟蹤配置的單一來源。不要為 MessageHistoryConfigurer 使用通用 bean 定義。
在 6.3 版本之前,訊息歷史頭是不可變的(您無法重寫歷史):每次跟蹤不僅會建立 MessageHistory 的新例項,還會建立一個全新的訊息副本。現在它以追加模式工作:第一次跟蹤會建立一個新訊息,其中包含新的 MessageHistory 容器。所有其餘的 MessageHistory.write() 呼叫都會向現有頭新增新條目,並且不會建立新訊息。這顯著提高了應用程式效能。框架中的所有元件,其中相同的訊息可以傳送給多個消費者(PublishSubscribeChannelAbstractMessageRouterWireTap 等),或者拆分器根據輸入訊息生成多個輸出,現在都將現有的 MessageHistory 頭克隆到這些新訊息中。對於框架範圍之外的任何其他多生產者用例,建議使用 AbstractIntegrationMessageBuilder.cloneMessageHistoryIfAny() API,以確保並行下游子流貢獻自己的訊息歷史跟蹤。
© . This site is unofficial and not affiliated with VMware.