com.kaka.notice.RemoteMessagePostman Maven / Gradle / Ivy
package com.kaka.notice;
import com.kaka.util.StringUtils;
import java.util.Map;
/**
* 远端事件处理器
* 目前一个事件仅支持获取一次处理结果,即广播状态,事件处理结果以最先获得为准,后面获得的处理结果直接丢弃
* 分布式环境需谨慎使用事件远端广播,因广播事件时所有拥有事件逻辑处理器{@link Command}的远程服务都将处理事件逻辑
*
* @author zkpursuit
*/
abstract public class RemoteMessagePostman {
protected Facade facade;
protected final RemoteMessageCache remoteMessageCache;
protected final String event_topic; //收到远端事件数据后处理事件
protected final String event_result_topic; //收到远端事件处理后的结果给本地缓存的事件赋值处理结果
/**
* 处理远端事件
*/
private static class RemoteMessageMediator extends Mediator {
private final RemoteMessagePostman postman;
RemoteMessageMediator(RemoteMessagePostman postman) {
super("remote_message_mediator_" + StringUtils.randomString(16, true));
this.postman = postman;
}
@Override
public void handleMessage(Message msg) {
RemoteMessage remoteMessage = (RemoteMessage) msg;
String cmd = msg.getWhat().toString();
if (this.postman.event_topic.equals(cmd)) {
//接收远端事件并执行事件处理逻辑
String id = remoteMessage.id;
Message localEventMessage = this.postman.remoteMessageCache.remove(id);
if (localEventMessage != null) {
//远端事件接收方如果与事件发送方同源则直接处理
this.sendMessage(localEventMessage);
return;
}
Message remoteEventMessage = (Message) remoteMessage.getBody();
this.sendMessage(remoteEventMessage);
if (remoteEventMessage.resultMap != null && !remoteEventMessage.resultMap.isEmpty()) {
remoteMessage.what = this.postman.event_result_topic;
this.postman.sendRemoteMessage(remoteMessage);
}
return;
}
if (this.postman.event_result_topic.equals(cmd)) {
//事件消息发送方接收到来自远端的处理结果
String id = remoteMessage.id;
Message remoteEventMessage = (Message) remoteMessage.getBody();
Message localEventMessage = this.postman.remoteMessageCache.remove(id);
if (localEventMessage == null) {
this.getFacade().sendMessage(new Message("remote_processed_message", remoteEventMessage));
return;
}
Map