考虑一个假设的要求–您的应用程序中有一个服务类,并且想要捕获有关此服务调用的一些信息:

@Service
public class SampleBean {private static final Logger logger = LoggerFactory.getLogger(SampleBean.class);public Response call(Request request) {logger.info("SampleBean.call invoked");return new Response(true);}
}

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,而需要进行的更改是纯配置。

参考: all和其他博客中的JCG合作伙伴 Biju Kunjummen的Spring Integration Publisher 。

翻译自: https://www.javacodegeeks.com/2014/01/spring-integration-publisher.html

Spring Integration Publisher相关推荐

  1. Spring Integration 实例讲解

    Spring Integration 简介 组件介绍 实例演示 买饮料 jms做一个简单的示例. 实例三集成JDBC 简介 最近学习到的工具,资料很少,但还是要记录下自己目前的理解,官方的说发就不说了 ...

  2. #翻译NO.3# --- Spring Integration Framework

    为什么80%的码农都做不了架构师?>>>    2.4 Message Endpoints A Message Endpoint represents the "filte ...

  3. Spring Integration学习资料

    Spring Integration学习资料 1.1     背景 Spring框架的一个重要主题是控制反转.从广义上来说,Spring处理其上下文中管理的组件的职责.只要组件减轻了职责,它们同时也被 ...

  4. Spring Integration 4.3.10 发布,Spring 消息通信

    Spring Integration 4.3.10 发布了.Spring Integration 能在基于 Spring 的应用中进行简单的消息通信,并通过简单的适配器与外部系统集成.这些适配器提供了 ...

  5. #翻译NO.5# --- Spring Integration Framework

    为什么80%的码农都做不了架构师?>>>    本人觉得这一章很重要,那就是 Spring默认的channel 的实现 DirectChannel,这个要大家多按照原文理解,开发者为 ...

  6. ESB学习笔记(Spring Integration实战)

    http://wangleifire.iteye.com/blog/351749 介绍 Spring Integration是Spring公司的一套ESB框架. 前面ESB介绍中我也做了一定了解.我们 ...

  7. #翻译NO.4# --- Spring Integration Framework

    为什么80%的码农都做不了架构师?>>>    Part III. Core Messaging This section covers all aspects of the cor ...

  8. java中channelmessage,MessageStore支持的QueueChannel与Spring Integration Java Config

    Spring Integration reference guide指的是使用MessageStore实现为QueueChannel提供持久性. 提到了很多次,但是所有示例都使用XML配置,即 但是Q ...

  9. Java开源项目:Spring Integration

    2019独角兽企业重金招聘Python工程师标准>>> Java开源项目:Spring Integration http://www.importnew.com/16538.html ...

最新文章

  1. 手把手教你看懂并理解Arduino PID控制库——调参改变
  2. spring boot(一):入门篇
  3. Linux集群架构(LVS DR模式搭建、keepalived + LVS)
  4. 开发和编程是一样的吗?区别有哪些?
  5. 为什么使用GB28181而不是直接rtsp拉流
  6. C#接口的作用(经典)
  7. CMake Error at cmake/boost.cmake:76 (MESSAGE)
  8. 30岁前成功的12条黄金法则
  9. java 显示数据库_java连接数据库并显示数据
  10. 2018华为软挑参赛体验
  11. 数据清洗Chap5——数据转换
  12. 使用BabeLua在VS中创建Lua项目
  13. maven环境变量配置?
  14. oracle有rtf函数,Delphi中对Oracle存取RTF文档_Delphi
  15. 解决能登录微信却登不上网页的问题
  16. 禁用U盘,不影响其他设备的使用
  17. 淘宝直通车什么情况能退款?怎么退?
  18. 机器人聊天软件c#_聊天机器人_c#应用
  19. 使用JS获取客户端的IP地址
  20. 数据分析实战案例:手把手教你用 Python 分析千万级淘宝数据

热门文章

  1. 通过网页查看服务器算法,java分析html算法(java网页蜘蛛算法示例)
  2. 计算机应用基础期中上机考试,期中考试计算机应用基础试卷
  3. 调用toString()方法的注意事项
  4. 优先队列——斐波那契堆(without source code)
  5. vaadin_Vaadin提示:延迟加载和商品标识
  6. 使用互联网了解的两个月里_我两个月来对Quarkus的了解
  7. angular1.2.27_Angular 8 + Spring Boot 2.2:立即构建一个CRUD应用程序!
  8. oracle jdk_两个Oracle JDK的故事
  9. finalizer_Java Finalizer和Java文件输入/输出流
  10. 因此,Oracle杀死了java.net