时间:2021-05-20
Spring Integration Java DSL已经融合到Spring Integration Core 5.0,这是一个聪明而明显的举动,因为:
让我们看看基于ActiveMQ JMS的示例如何使用它。
Maven依赖:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> <dependency> <groupId>org.springframework.integration</groupId> <artifactId>spring-integration-core</artifactId> </dependency> <dependency> <groupId>org.springframework.integration</groupId> <artifactId>spring-integration-jms</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-kahadb-store</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework.integration/spring-integration-java-dsl --> <dependency> <groupId>org.springframework.integration</groupId> <artifactId>spring-integration-java-dsl</artifactId> <version>1.2.3.RELEASE</version> </dependency></dependencies>示例1:Jms入站网关
我们有以下ServiceActivator:
@Servicepublic class ActiveMQEndpoint { @ServiceActivator(inputChannel = "inboundChannel") public void processMessage(final String inboundPayload) { System.out.println("Inbound message: "+inboundPayload); }}如果您想使用SI Java DSL 将inboundPayload从Jms队列发送到Gateway风格的激活器,那么请使用DSLJms工厂:
@Beanpublic DynamicDestinationResolver dynamicDestinationResolver() { return new DynamicDestinationResolver();}@Beanpublic ActiveMQConnectionFactory connectionFactory() { return new ActiveMQConnectionFactory();}@Beanpublic DefaultMessageListenerContainer listenerContainer() { final DefaultMessageListenerContainer defaultMessageListenerContainer = new DefaultMessageListenerContainer(); defaultMessageListenerContainer.setDestinationResolver(dynamicDestinationResolver()); defaultMessageListenerContainer.setConnectionFactory(connectionFactory()); defaultMessageListenerContainer.setDestinationName("jms.activeMQ.Test"); return defaultMessageListenerContainer;}@Beanpublic MessageChannel inboundChannel() { return MessageChannels.direct("inboundChannel").get();}@Beanpublic JmsInboundGateway dataEndpoint() { return Jms.inboundGateway(listenerContainer()) .requestChannel(inboundChannel()).get();}通过dataEndpoint bean 返回JmsInboundGatewaySpec,您还可以向SI通道或Jms目标发送回复。查看文档。
示例2:Jms消息驱动的通道适配器
如果您正在寻找替换消息驱动通道适配器的XML JMS配置,那么JmsMessageDrivenChannelAdapter是一种适合您的方式:
@Beanpublic DynamicDestinationResolver dynamicDestinationResolver() { return new DynamicDestinationResolver();}@Beanpublic ActiveMQConnectionFactory connectionFactory() { return new ActiveMQConnectionFactory();}@Beanpublic DefaultMessageListenerContainer listenerContainer() { final DefaultMessageListenerContainer defaultMessageListenerContainer = new DefaultMessageListenerContainer(); defaultMessageListenerContainer.setDestinationResolver(dynamicDestinationResolver()); defaultMessageListenerContainer.setConnectionFactory(connectionFactory()); defaultMessageListenerContainer.setDestinationName("jms.activeMQ.Test"); return defaultMessageListenerContainer;}@Beanpublic MessageChannel inboundChannel() { return MessageChannels.direct("inboundChannel").get();}@Beanpublic JmsMessageDrivenChannelAdapter dataEndpoint() { final ChannelPublishingJmsMessageListener channelPublishingJmsMessageListener = new ChannelPublishingJmsMessageListener(); channelPublishingJmsMessageListener.setExpectReply(false); final JmsMessageDrivenChannelAdapter messageDrivenChannelAdapter = new JmsMessageDrivenChannelAdapter(listenerContainer(), channelPublishingJmsMessageListener ); messageDrivenChannelAdapter.setOutputChannel(inboundChannel()); return messageDrivenChannelAdapter;}与前面的示例一样,入站有效负载如样本1中一样发送给激活器。
示例3:使用JAXB的Jms消息驱动的通道适配器
在典型的场景中,您希望通过Jms接受XML作为文本消息,将其转换为JAXB存根并在服务激活器中处理它。我将向您展示如何使用SI Java DSL执行此操作,但首先让我们为xml处理添加两个依赖项:
<dependency> <groupId>org.springframework.integration</groupId> <artifactId>spring-integration-xml</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-oxm</artifactId> </dependency>我们将通过JMS接受shiporders ,所以首先XSD命名为shiporder.xsd:
<?xml version="1.0" encoding="UTF-8" ?><xs:schema xmlns:xs="http://.example.stubs");}inboundChannel的服务激活器:
/** * Sample 3, 5 * @param shiporder */@ServiceActivator(inputChannel = "inboundChannel")public void processMessage(final Shiporder shiporder) { System.out.println(shiporder.getOrderid()); System.out.println(shiporder.getOrderperson());}您不应该错过在项目中使用IntegrationFlowAdapter。我喜欢它的概念。
我最近在Embedit的新的基于Spring Boot的项目中开始使用Spring Integration Java DSL 。即使有一些配置,我发现它非常有用。
源码地址:https://bitbucket.org/tomask79/spring-integration-java-dsl
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
目前spring-boot成为了java开发的主流框架,cucumber作为一款支持dsl的自动化测试工具,很适合用户编写dsl优化过的单元测试等测试用例。本文
springboot集成mybatis关键代码如下:1,添加pom引用org.mybatis.spring.bootmybatis-spring-boot-st
SpringBoot集成activiti基础环境搭建添加依赖org.activitiactiviti-spring-boot-starter-basic6.0.
SpringBoot集成MyBatis和Pagehelper分页插件mybatis-spring-boot-starter依赖树如下:pom配置
java根据经纬度获取地址实现代码实现代码:publicclassGetLocation{publicstaticvoidmain(String[]args){