spring学习

关于endpoint配置补充
endpoint配置相对比较灵活,下面再来看一个例子:

<endpoint input-channel="inputChannel"
default-output-channel="outputChannel"
handler-ref="helloService"
handler-method="sayHello"/>
<beans:bean id="helloService" class="org.springframework.integration.samples.helloworld.HelloService"/>


1 public class HelloService {
2
3 public String sayHello(String name) {
4 return "Hello " + name;
5 }
6 }
上面这个例子就演示了把 HelloService配置成一个MessageEndpoint组件,消息从"inputChannel"队列接收后,调用HelloService.sayHello方法,等sayHello方法返回后,根据default-output-channel="outputChannel"的配置把返回的结果保存到message.payload属性后发送给"outputChannel"队列

也可改成Annotation的方式,配置方法如下:

 <annotation-driven/>
<message-bus auto-create-channels="true"/>
<context:component-scan base-package="org.springframework.integration.samples.helloworld"/>

<beans:bean id="helloService" class="org.springframework.integration.samples.helloworld.HelloService"/>


1 @MessageEndpoint(input="inputChannel", defaultOutput="outputChannel")
2 public class HelloService {
3
4 @Handler
5 public String sayHello(String name) {
6 return "Hello " + name;
7 }
8 }
设置并发操作属性

xml配置:

<endpoint input-channel="exampleChannel" handler-ref="exampleHandler"/>
<!-- 设置并发设置 core核心线程数 max最大线程数 queue-capacity 队列最大消息数 keep-alive idle线程生命时间-->
<concurrency core="5" max="25" queue-capacity="20" keep-alive="120"/>
</endpoint>



annotation配置

1 @MessageEndpoint(input="fooChannel")
2 @Concurrency(coreSize=5, maxSize=20, queueCapacity=20, keepAliveSeconds=120)
3 public class FooService {
4
5 @Handler
6 public void bar(Foo foo) {
7
8 }
9 }

下面总结一下常见的annotation的使用方法
@MessageEndpoint

它表示处理消息对象的终端节点。一般与其它的元数据标记一起使用。

下面会具体介绍与该元数据标记一起使用的其它标识的使用方法。

@MessageEndpoint源代码:

1 @Target(ElementType.TYPE)
2 @Retention(RetentionPolicy.RUNTIME)
3 @Inherited
4 @Documented
5 @Component
6 public @interface MessageEndpoint {
7
8 String input() default ""; //接收消息的队列名称
9 String defaultOutput() default ""; //默认发送消息的队列名称(只有在不设置@Router情况下才有效)
10 int pollPeriod() default 0; //发送消息的轮循时间间隔(只有在不设置@Router情况下才有效)
11 }

@Handler

消息回调处理的方法。与@MessageEndpoint一起配置(只限M3版,M4以及后续版本可以单独使用,具体使用方法还要等具体的实现出来),收到input队列的消息后,回调@Handler标识的方法回调方法的参数类型必须与message.payload属性类型相同注:如果回调方法有返回值, 则回调方法处理完成后,会将返回值设置到message.payload属性后,发送消息到@MessageEndpoint的defaultOutput队列。如果defaultOutput没有设定,则将抛出异常。

@Handler源代码:

1 @Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
2 @Retention(RetentionPolicy.RUNTIME)
3 @Inherited
4 @Documented
5 public @interface Handler {
6
7 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值