当前位置: 首页 > article >正文

Spring-messaging-MessageHandler接口实现类ServiceActivatingHandler

ServiceActivatingHandler实现了MessageHandler接口,所以它是一个MessageHandler,在spring-integration中,它也叫做服务激活器(Service Activitor),因为这个类是依赖spring容器BeanFactory的,所以在这里我不能直接使用硬编码的方式通过main方法直接构造ServiceActivatingHandler,而是使用@ServiceActivator注解配合spring

ServiceActivatingHandler类

这个类重要的一点就是允许带有返回值,如果有返回值,则需要配置一个输出通道,将这个返回值,通过该输出通道,传递到下一步(也可以通过该通道传递给发送者)

导入的包

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.config.EnableIntegration;
import org.springframework.integration.handler.ServiceActivatingHandler;
import org.springframework.messaging.*;
import org.springframework.messaging.support.GenericMessage;

代码示例:A发送消息->B接收方处理->B返回值->A处理返回值

下面的代码演示了A发送给B,B处理,又返回给A,A再处理,非常类似一次HTTP请求

@Configuration
@EnableIntegration
public class E5ServiceActivatingHandler {// 创建一个通道,用这个通道给B(messageHandler方法)发消息@Beanpublic SubscribableChannel inputChannel(){return new DirectChannel();}// B收到消息之后,如何处理,注意inputChannel 和 outputChannel @ServiceActivator(inputChannel = "inputChannel", outputChannel = "outputChannel")public String messageHandler(Message<String> msg) throws InterruptedException {System.out.println("收到入参了:" + msg.getPayload());Thread.sleep(10000);// 收到消息并处理完成之后,返回给发送方A一个返回值return "只买特斯拉";}// B返回给发送方A返回值是通过这个通道发送的@Beanpublic SubscribableChannel outputChannel(){return new DirectChannel();}public static void main(String[] args) {AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(E5ServiceActivatingHandler.class);// 获取返回通道,接收方B的返回值会通过该通道发给发送方ASubscribableChannel outputChannel = context.getBean("outputChannel", SubscribableChannel.class);// 发送方A如何处理B的返回值,这里我们通过MyHandler类outputChannel.subscribe(new MyHandler());// 获取输入通道,发送方A通过该通道,将入参给BSubscribableChannel inputChannel = context.getBean("inputChannel", SubscribableChannel.class);// 创建发送方A的入参,参数内容为:"坚决不买国产新能源"Message<String> message = new GenericMessage<>("坚决不买国产新能源");// A发送给BinputChannel.send(message);}// 这个类决定A收到B的返回值之后如何处理这个返回值public static class MyHandler implements MessageHandler {@Overridepublic void handleMessage(Message<?> message) throws MessagingException {System.out.println("对方返回值:" + message.getPayload());}}}
http://www.lryc.cn/news/2379609.html

相关文章:

  • asp.net core api RESTful 风格控制器
  • 【甲方安全建设】Python 项目静态扫描工具 Bandit 安装使用详细教程
  • 实习记录小程序|基于SSM+Vue的实习记录小程序设计与实现(源码+数据库+文档)
  • 老旧设备升级利器:Modbus TCP转 Profinet让能效监控更智能
  • 【从基础到模型网络】深度学习-语义分割-ROI
  • Qt控件:交互控件
  • 前端下载ZIP包方法总结
  • 掌握Docker:从运行到挂载的全面指南
  • Pandas pyecharts数据可视化基础③
  • QMK固件OLED显示屏配置教程:从零开始实现个性化键盘显示(实操部分)
  • 数据库中关于查询选课问题的解法
  • 基于Bootstrap 的网页html css 登录页制作成品
  • python中http.cookiejar和http.cookie的区别
  • 【NLP 71、常见大模型的模型结构对比】
  • 组件导航 (Navigation)+flutter项目搭建-混合开发+分栏
  • HGDB企业版迁移到HGDB安全版
  • ProfibusDP主站转modbusTCP网关与ABB电机保护器数据交互
  • AM32电调学习解读六:main.c文件的函数介绍
  • ubuntu24.04上安装NVIDIA driver+CUDA+cuDNN+Anaconda+Pytorch
  • AutoVACUUM (PostgreSQL) 与 DBMS_STATS.GATHER_DATABASE_STATS_JOB_PROC (Oracle) 对比
  • Rust中的交叉编译与vendered特性
  • 3、函数和约束
  • PhpStudy | PhpStudy 工具安装 —— Windows 系统安装 PhpStudy
  • Debezium快照事件监听器系统设计
  • 基于vue框架的订单管理系统r3771(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
  • 【2025年前端高频场景题系列】使用同一个链接,如何实现PC打开是web应用、手机打是-个H5 应用?
  • 语音识别-2
  • React useState 的同步/异步行为及设计原理解析
  • 语音识别——语音转文字
  • 兰亭妙微:用系统化思维重构智能座舱 UI 体验