javascript
Spring Integration Publisher
考慮一個假設的要求–您的應用程序中有一個服務類,并且想要捕獲有關此服務調用的一些信息:
AOP非常適合這種需求,它允許干凈地捕獲方法調用(切入點)周圍的信息,并使用此信息進行一些處理(建議):
public class AuditAspect {private static final Logger logger = LoggerFactory.getLogger(AuditAspect.class);@Pointcut("execution( * core.SampleBean.call(model.Request)) && args(r)")public void beanCalls(Request r){}@Around("beanCalls(r)")public Object auditCalls(ProceedingJoinPoint pjp, Request r) {logger.info("Capturing request: " + r);try{Object response = pjp.proceed();logger.info("Capturing response: " + response);return response;}catch(Throwable e) {throw new RuntimeException(e);}} }這似乎足夠好。 現在,如果我想立即將響應返回給客戶端,但繼續處理方法調用的上下文,該怎么辦?我們可以使用ThreadPool將Advice的邏輯放在單獨的線程中。 現在讓我增加另一層復雜性,如果我們要絕對確保不丟失上下文,該怎么辦?一個好的方法是將方法調用的上下文保留在JVM之外,通常像RabbitMQ和ActiveMQ這樣的消息傳遞提供者將非常適合
考慮到這些額外的要求,一個更簡單的解決方案(尤其是在正在使用消息傳遞場景的情況下)將使用Spring Integration。 讓我們首先定義一個新的Spring Integration應用程序上下文:
<?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="http://www.springframework.org/schema/integration"xmlns:beans="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><annotation-config/><channel id="tobeprocessedlater"/><logging-channel-adapter channel="tobeprocessedlater" log-full-message="true"/></beans:beans>它只是具有一個通道的定義,以及一個出站適配器,該適配器從該通道讀取并記錄完整的消息。 為了捕獲對SampleBean的調用的上下文,可以將Publisher注釋添加到SampleBean的相關方法,該方法將“東西”定向到添加到注釋的通道。
@Service public class SampleBean {private static final Logger logger = LoggerFactory.getLogger(SampleBean.class);@Publisher(channel = "tobeprocessedlater")public Response call(@Header("request") Request request) {logger.info("SampleBean.call invoked");return new Response(true);} }通過附加注釋指定將什么“東西”發送到此“ tobeprocessedlater”信道–默認情況下,該方法的返回值發送到該信道,此外,我還使用@Header注釋標記了請求,這將使請求作為響應消息的頭發送。 為了完整起見,集成上下文具有一個<annotation-config />標記,該標記注冊尋找@Publisher注釋的相關組件,并在發現一個組件時將其織入要執行的其他操作中。
如果現在執行此代碼,則輸出將遵循以下幾行:
core.SampleBean - SampleBean.call invoked o.s.integration.handler.LoggingHandler - [Payload=Response{success=true}][Headers={request=RequestType1{attr='null'}, id=52997b10-dc8e-e0eb-a82a-88c1df68fca5, timestamp=1389268390212}]現在,為了滿足第一個需求,在單獨的執行線程中處理建議(在本例中為日志記錄):
只需更改配置即可完成! –在此示例中,我選擇使用執行者通道,而不是將消息發布到直接通道,而是將其發布為可以緩沖消息或使用執行程序來分派消息的通道類型:
<channel id="tobeprocessedlater"><dispatcher task-executor="taskExecutor"/></channel>現在,為了增加將異步消息處理發布到外部消息傳遞提供程序(并稍后處理消息)以使其更加可靠的要求,讓我通過將消息發布到RabbitMQ進行演示,代碼更改再次是純配置,代碼中沒有任何變化!:
<channel id="tobeprocessedlater"/><int-amqp:outbound-channel-adapter amqp-template="amqpTemplate" channel="tobeprocessedlater" /> 消息傳遞接收器可以是任何東西–數據庫,文件系統,ActiveMQ,而需要進行的更改是純配置。
翻譯自: https://www.javacodegeeks.com/2014/01/spring-integration-publisher.html
總結
以上是生活随笔為你收集整理的Spring Integration Publisher的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 苹果手机白点在哪里设置出来
- 下一篇: Spring XD 1.0.0.M5在这