通过代码,如何使用多个MessageHandler 去处理消息?
"SubscribableChannel messageChannel = new DirectChannel(); messageChannel.subscribe(msg -> { System.out.println(""receive1: "" + msg.getPayload()); }); messageChannel.subscribe(msg -> { System.out.println(""receive2: "" + msg.getPayload()); }); messageChannel.send(MessageBuilder.withPayload(""msg from alibaba"").build()); messageChannel.send(MessageBuilder.withPayload(""msg from alibaba"").build()); 由于 DirectChannel 内部的消息分发器是 UnicastingDispatcher 单播的方式,并且采用轮询的负载均衡策略,所以这里两次的消费分别对应这两个 MessageHandler。控制台打印出: receive1: msg from alibaba receive2: msg from alibaba 既然存在单播的消息分发器 UnicastingDispatcher,必然也会存在广播的消息分发 器,那就是 BroadcastingDispatcher,它被 PublishSubscribeChannel 这个消息通道 所使用。广播消息分发器会把消息分发给所有的 MessageHandler: SubscribableChannel messageChannel = new PublishSubscribeChannel(); messageChannel.subscribe(msg -> { System.out.println(""receive1: "" + msg.getPayload()); }); messageChannel.subscribe(msg -> { System.out.println(""receive2: "" + msg.getPayload()); }); messageChannel.send(MessageBuilder.withPayload(""msg from alibaba"").build()); messageChannel.send(MessageBuilder.withPayload(""msg from alibaba"").build()); 发送两个消息,都被所有的 MessageHandler 所消费。控制台打印: receive1: msg from alibaba receive2: msg from alibaba receive1: msg from alibaba receive2: msg from alibaba
这本电子书收录开发者藏经阁,下载连接:https://developer.aliyun.com/topic/download?id=1216"
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。