本章关于事件消息回调通知的处理流程:
- 首先,事件消息的监听在 trigger 层的监听,之后调用领域层的通知服务方法。这里需要新增加一个领域方法。
- 之后,领域服务方法,要以 notifyType 不同类型进行通知操作。如;http、mq,这部分你将来想扩展其他的,就在这里添加。
- 最后,http 和 mq,都会进入基础设施层完成服务的调用处理。http 使用的是 retrofit2 框架进行封装。mq(RabbitMQ)直接使用 RabbitTemplate 模板 push 消息即可。
HTTP 框架使用
public class LocalTaskMessageAutoConfig {
@Bean
public OkHttpClient okHttpClient(){
ConnectionPool pool = new ConnectionPool(10, 5, TimeUnit.MINUTES);
return new OkHttpClient.Builder()
.connectionPool(pool)
.retryOnConnectionFailure(true)
.connectTimeout(100, TimeUnit.SECONDS)
.readTimeout(300, TimeUnit.SECONDS)
.writeTimeout(300, TimeUnit.SECONDS)
.build();
}
@Bean
public GenericHttpGateway genericHttpGateway(OkHttpClient okHttpClient) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://127.0.0.1/")
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build();
return retrofit.create(GenericHttpGateway.class);
}
}在基础设施层,定义 GenericHttpGateway 一个通用的 HTTP 服务接口。之后在 config 配置下的 LocalTaskMessageAutoConfig 类里进行实例化操作
MQ 消息推送
@Slf4j
@Component
public class RabbitMQEvent {
/**
* required = false 避免用户没有使用 RabbitMQ 而导致报错
*/
@Autowired(required = false)
private RabbitTemplate rabbitTemplate;
public void publish(String exchange, String routingKey, String message) {
try {
if (null == rabbitTemplate){
log.error("应用服务方,尚未配置 RabbitMQ Template 不能完成 MQ 发送");
return;
}
rabbitTemplate.convertAndSend(exchange, routingKey, message, m -> {
// 持久化消息配置
m.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
return m;
});
} catch (Exception e) {
log.error("发送MQ消息失败 exchange:{} routingKey:{} message:{}", exchange, routingKey, message, e);
throw e;
}
}
}RabbitMQ 推送需要使用 RabbitTemplate 模板,不过注入的时候要配置 @Autowired(required = false) 这样如果用户没有使用 RabbitMQ 也不会报错。
通知策略

public interface INotifyStrategy {
String notify(TaskMessageEntityCommand command) throws Exception;
}@Component("httpNotifyStrategy")
public class HttpNotifyStrategy implements INotifyStrategy {
@Resource
private ILocalTaskMessagePort port;
@Override
public String notify(TaskMessageEntityCommand command) throws Exception {
try {
return port.notify2http(command);
} catch (Exception e) {
throw e;
}
}
}@Component("rabbitMqNotifyStrategy")
public class RabbitMqNotifyStrategy implements INotifyStrategy {
@Reference
private ILocalTaskMessagePort port;
@Override
public String notify(TaskMessageEntityCommand command) throws Exception {
try {
return port.notify2rabbitmq(command);
}catch (Exception e){
throw e;
}
}
} 在 TaskNotifyEnum 枚举中维护不同的策略使用的 bean 名称
public enum TaskNotifyEnum {
HTTP("http", "httpNotifyStrategy", "HTTP通知"),
RABBIT_MQ("rabbit_mq", "rabbitMQNotifyStrategy", "MQ通知"),
;
private String type;
private String strategy;
private String desc;
public static TaskNotifyEnum of(String type) {
if (type == null) return null;
for (TaskNotifyEnum value : TaskNotifyEnum.values()) {
if (value.getType() != null && value.getType().equalsIgnoreCase(type)) {
return value;
}
}
return null;
}
public static String getStrategyByType(String type) {
TaskNotifyEnum e = of(type);
return e == null ? null : e.getStrategy();
}
} 在LocalTaskMessageNotifyService中进行具体的分发:
@Slf4j
@Service
@RequiredArgsConstructor
public class LocalTaskMessageNotifyService implements ILocalTaskMessageNotifyService {
private final Map<String, INotifyStrategy> notifyStrategyMap;
@Override
public String notify(TaskMessageEntityCommand command) throws Exception {
String strategyBeanName = TaskNotifyEnum.getStrategyByType(command.getNotifyType());
INotifyStrategy notifyStrategy = notifyStrategyMap.get(strategyBeanName);
return notifyStrategy.notify(command);
}
}