Spring Integration基本用法
关于Spring-Integration的介绍这里不需要赘述,现在我们需要实现的是发送一个param,里面有两个参数dataType和data,dataType是0和1除外的舍弃,剩余dataType=0的数据往oldChannel通道发,剩余dataType=1的数据往newChannel通道发,消费数据并且打印
1,配置文件 : application.xml
<beans:beans xmlns="http://www.springframework.org/schema/integration"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:beans="http://www.springframework.org/schema/beans"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd"><!-- 要输入的信息往这个通道里塞即可 -->
<channel id="inputChannel" /><!-- 这个过滤器可以过滤一些非法信息,重组消息体等 -->
<splitter input-channel="inputChannel" ref="splitService" output-channel="afterSplitChannel" /><!-- 路由转发,重新转发到另外的通道去 getDistritChannelId方法返回的是另外通道的id的名称 -->
<router input-channel="afterSplitChannel" ref="routerService" method="getDistritChannelId" /><service-activator input-channel="oldChannel" ref="doConsumerService" method="printOldMsg" /><service-activator input-channel="newChannel" ref="doConsumerService" method="printNewMsg" /><!-- 处理过滤器的业务方法,需要继承spring-integration core里面的 AbstractMessageSplitter -->
<beans:bean id="splitService" class="com.test.SplitService" /><!-- 路由业务处理类,里面一定需要getDistritChannelId方法返回通道的id的名称 -->
<beans:bean id="routerService" class="com.test.RouterService" /><!-- 通道消费端消费信息,里面需要有上面需要用到的printOldMsg和printNewMsg方法 -->
<beans:bean id="doConsumerService" class="com.test.DoConsumerService" /></beans:beans>
2,com.test.SplitService:
public class SplitService extends AbstractMessageSplitter {@Overrideprotected Object splitMessage(Message<?> msg) {if(msg.getPayload() == null) {return null;}Param param = (Param)msg.getPayload();if(param.getDataType !=0 || param.getDataType !=1) {return null;}//可以重新对param进行处理后再返回return param;}}
3,com.test.Param :
public class Param {private Integer dataType;private Object data;public Integer getDataType() {return this.dataType;}public Integer getData() {return this.data;}public void setDataType(Integer dataType) {this.dataType = dataType;}public void setData(Object data) {this.data = data;}
}
4,com.test.RouterService
/**路由重定向*/
public class RouterService {public String getDistritChannelId(Param param) {if(param.getDataType==0) {return "oldChannel";}if(param.getDataType==1) {return "newChannel";}return "oldChannel";}
}
5,com.test.DoConsumerService
public class DoConsumerService{public void printOldMsg(Param param) {System.out.println("--------处理Old通道信息:" + param);}public void printNewMsg(Param param) {System.out.println("--------处理New通道信息:" + param);}}
6,Main-来发送信息:
public class Main{public static void main(String[] args) {ApplicationContext app = new ClassPathXmlApplicationContext("classpath:application.xml");MessageChannel msgChannel = app.getBean("inputChannel",MessageChannel.class);Param param = new Param();for(int i = 0; i < 100; i++) {if(i % 2 == 0) {param.setDataType(0);} else {param.setDataType(1);}param.setData("{\"sum\":" + i + "}");Message<Param> msg = MessageBuilder.withPayload(param).build();msgChannel.send(msg);}}}
Spring Integration基本用法相关推荐
- Spring Integration入门
为什么使用Spring IntegrationSpring Integration是Spring框架创建的又一个API,面向企业应用集成(EAI).说到集成,并不缺"解决办法":硬 ...
- pmp访谈法和焦点小组区别_访谈和书评:Spring Integration in Action
pmp访谈法和焦点小组区别 由Mark Fisher,Jonas Partner,Marius Bogoevici和Iwein Fuld撰写的< Spring Integration in Ac ...
- #翻译NO.3# --- Spring Integration Framework
为什么80%的码农都做不了架构师?>>> 2.4 Message Endpoints A Message Endpoint represents the "filte ...
- Spring Integration学习资料
Spring Integration学习资料 1.1 背景 Spring框架的一个重要主题是控制反转.从广义上来说,Spring处理其上下文中管理的组件的职责.只要组件减轻了职责,它们同时也被 ...
- Spring Integration 4.3.10 发布,Spring 消息通信
Spring Integration 4.3.10 发布了.Spring Integration 能在基于 Spring 的应用中进行简单的消息通信,并通过简单的适配器与外部系统集成.这些适配器提供了 ...
- #翻译NO.5# --- Spring Integration Framework
为什么80%的码农都做不了架构师?>>> 本人觉得这一章很重要,那就是 Spring默认的channel 的实现 DirectChannel,这个要大家多按照原文理解,开发者为 ...
- ESB学习笔记(Spring Integration实战)
http://wangleifire.iteye.com/blog/351749 介绍 Spring Integration是Spring公司的一套ESB框架. 前面ESB介绍中我也做了一定了解.我们 ...
- #翻译NO.4# --- Spring Integration Framework
为什么80%的码农都做不了架构师?>>> Part III. Core Messaging This section covers all aspects of the cor ...
- java中channelmessage,MessageStore支持的QueueChannel与Spring Integration Java Config
Spring Integration reference guide指的是使用MessageStore实现为QueueChannel提供持久性. 提到了很多次,但是所有示例都使用XML配置,即 但是Q ...
最新文章
- ELK 搭建 TB 级海量日志监控系统,这个太强了!
- java之final
- centos安装zendopcache
- 干货总结:I2C总线详细要点
- 麒麟信安:“一云多芯”信创云桌面解决方案引领者
- PostgresSQL模式
- 【背包问题】基于matlab多目标粒子群算法求解多背包问题【含Matlab源码 654期】
- 冲动的惩罚 - 刀郎 - 新阿瓦尔古丽
- 计算机telnet命令大全,Telnet命令
- 使用matplotlib动态打印图片(RGB图片)
- 41-牛栏-最短路径
- GBase 8c 的安全特性
- cesium加载动图方案三:通过apng-js库实现
- 导入另一个 Git库到现有的Git库并保留提交记录
- 算法学习之道,应有三重境界
- biu biu biu
- 未来五年有颠覆性的IT技术都在这里
- Java web 项目技术文档目录结构
- Go语言bufio包(读与写)
- 删除buddypress相关功能