SOA专题
基于Spring集成的EDA
原作者 nereuschen
基于Spring Integration集成包进行改造为EDA的开源项目
开源项目
1. Apache Camel
本项目的目标
1.基于Spring Integration的core包进行改造
2.轻量级化(NoSpring,less-3rd.jar)
3.添加新特性
项目地址
EDA(Event Driven-Architecture)的优势
两种消息模型:队列Queue的点对点1:1和Topic主题的1:N
这两种类型的核心API如下:
MessageChannel |
method |
implement |
PollableChannel |
send()
receive() |
QueueChannel |
SubscribableChannel |
send()
subscribe()
unsubscribe() |
DirectChannel
ExecutorChannel
PublishSubscribeChannel |
对于Topic的SubscribableChannel有三个之类实现,DirectChannel ExecutorChannel 和PublishSubscribeChannel,这三个不同之处如下:
选择不同类型channel需要考虑的因素:事务 消息模型 消息缓冲区,堵塞等等。
实例代码如下:
发送消息
QueueChannel queueChannel = new QueueChannel();
// register the queueChannel
ChannelRegistry.registerMessageChannel(channelName,queueChannel);
// build a new message
Message<String> message = MessageBuilder.withPayload("this is a message")
.setHeader("headerA", "headerValue").build();
// send a message to the queueChannel
queueChannel.send(message);
接收消息:
// lookup the queueChannl,may be an empty channel before the registried
QueueChannel queueChannel = ChannelRegistry.lookupQueueChannel(channelName);
// blocking until there is a message
Message message = queueChannel.receive();
System.out.println("Received message : " + message.toString());