消息通道
顶层接口,返回true即为发送成功,返回false即为发送失败或者超时。
public interface MessageChannel {
boolean send(Message message);
boolean send(Message message, long timeout);
}
轮询接口,返回一个消息,超时或者打断返回null。会缓冲消息。
public interface PollableChannel extends MessageChannel {
Message<?> receive();
Message<?> receive(long timeout);
}
订阅接口,直接发送消息到处理器。不轮询,不会缓冲消息。
public interface SubscribableChannel extends MessageChannel {
boolean subscribe(MessageHandler handler);
boolean unsubscribe(MessageHandler handler);
}
通道实现,实现了上述接口的主要通道有:
- PublishSubscribeChannel
- QueueChannel
- PriorityChannel
- RendezvousChannel
- DirectChannel
- ExecutorChannel
- FluxMessageChannel
拦截器,拦截通道的消息,加入自定义过程
public interface ChannelInterceptor {
Message<?> preSend(Message<?> message, MessageChannel channel);
void postSend(Message<?> message, MessageChannel channel, boolean sent);
void afterSendCompletion(Message<?> message, MessageChannel channel, boolean sent, Exception ex);
boolean preReceive(MessageChannel channel);
Message<?> postReceive(Message<?> message, MessageChannel channel);
void afterReceiveCompletion(Message<?> message, MessageChannel channel, Exception ex);
}
通道添加拦截器
channel.addInterceptor(someChannelInterceptor);
消息模板,提供非倾入式的消息分发接口
MessagingTemplate template = new MessagingTemplate();
Message reply = template.sendAndReceive(someChannel, new GenericMessage("test"));
还有一些其他的交互方法
public boolean send(final MessageChannel channel, final Message<?> message) { ...
}
public Message<?> sendAndReceive(final MessageChannel channel, final Message<?> request) { ...
}
public Message<?> receive(final PollableChannel<?> channel) { ...
}
配置消息通道,各种通道的个性化配置
- DirectChannel(SubscribableChannel)
failover
load-balancer,负载均衡器
datatype,数据类型 - QueueChannel(PollableChannel),可以缓冲在内存,也可以缓冲在磁盘
capacity,容量 - PublishSubscribeChannel(SubscribableChannel)
task-executor,任务执行器
apply-sequence - ExecutorChannel(SubscribableChannel)
task-executor - PriorityChannel(QueueChannel)
capacity,容量 - RendezvousChannel (QueueChannel)
- 作用域通道
scope - 拦截器配置
channel-interceptor - 全局拦截器配置
order
pattern - 窃听器
- 条件窃听器
- 全局窃听器
特殊通道
- nullChannel(有点像/dev/null)
- errorChannel
轮询器
本章节介绍轮询怎么工作。
扫描二维码关注公众号,回复:
8880175 查看本文章
轮询消费者
当一个消息端点(通道适配器)连接到适配器时,会产生其中的一个消费者实例:
- PollingConsumer
- EventDrivenConsumer
当连接到SubscribableChannel时,会生成一个EventDrivenConsumer实例;而连接到PollableChannel时,会产生一个PollingConsumer实例。