概述
框架中提供的基础服务都是通过接口将实现和调用分离,这样应用开发人员可以根据业务的需求定制符合用户需求的实现方式。
系统中的消息分两种:
- 信息,针对一个用户发送的信息,比如:待办提醒、日程提醒等待。
- 通知,针对一个群体,比如:部门、小组,的广播消息。
框架中和通知相关的接口有两个:
- NotificationCenter 根据消息发送策略向用户发送消息。
- MessageSender 真正的消息发送接口。
源码分析
消息内容
消息内容可以是在线个用户发送的用户查看的信息,也可以是也前端应用改变状态的信息。消息内容可以和应用或者应用中的操作关联。
public class NoticeMessage implements java.io.Serializable {
/**
* 消息类别 默认值 msg
* 客户端可以根据不同的消息类别提供不同的 响应方式 或者 展示方式
*/
private String msgType;
/** 消息主题 这不是必须的
* */
private String msgSubject;
/** 消息内容,为一个文本,不同的消息类别 可以有不同的格式
*/
private String msgContent;
/** 关联业务
*/
private String optId;
/**关联业务中的 方法、功能、模块
*/
private String optMethod;
/** 关联对象组件,多个主键用url参数的方式链接
*/
private String optTag;
接口MessageSender
MessageSender 只有一个需要实现的方法。它式真正发送消息的地方。
/**
* 发送内部系统消息
*
* @param sender 发送人内部用户编码
* @param receiver 接收人内部用户编码
* @param message 消息主体
* @return "OK" 表示成功,其他的为错误信息
*/
String sendMessage( String sender, String receiver, NoticeMessage message);
框架中内置了一些具体的实现:
- DummyMessageSenderImpl这个是一个哑实现,仅仅用于测试桩。
- EmailMessageSenderImpl是通过email发送消息的实现。
- 项目centit-framework-system中InnerMessageManagerImpl.java实现了一个通过数据库表来存储的内部消息机制。
接口NotificationCenter
NotificationCenter 提供的发送消息的方式和接口MessageSender类似,额外多了写个消息发送注册接口,将方式方式(noticeType/sendType)和MessageSender 关联起来。NotificationCenter可以通过指定的方式给用户发送,也可以根据用户设置的接收方式发送,用户可以定义多个接收方式。默认情况下通过后者给用户发送消息。框架中也有多个实现:
- SimpleNotificationCenterImpl 实现了唯一的发送方式的通知方案。
- NotificationCenterImpl 实现了可以根据用户设置接收方式的发送方案。
- 项目 centit-msgpusher 中的SimpleNotificationCenterPlusMsgPusherImpl在SimpleNotificationCenterImpl的基础上添加了对在线用户的websocket推送功能。使用这个功能需要引用msgpusher-notification 包。
- centit-msgpusher NotificationCenterPlusMsgPusherImpl和上面的相似在NotificationCenterImpl的基础上添加了对在线用户的websocket推送功能。
- centit-msgpusher MsgPusherNotificationImpl,这类是将消息发送的实现交给msgpusher-server服务,msgpusher-server可以通过websocket对pc在线用户发送消息,同时也可以对iOS和android用户实现实时的消息推送。使用这个功能需要引用msgpusher-client 包,同时要启动msgpusher-server服务,这个服务需要单独运行。
使用示例
使用通知中心,需要分四步:
- 定义消息发送类的bean(实现MessageSender接口的bean),可以有多个、也可以没有 在使用msgpusher-server时就不需要。
@Bean
public EmailMessageSenderImpl emailMessageSender() {
EmailMessageSenderImpl sender = new EmailMessageSenderImpl();
sender.setHostName("mail.centit.com");
sender.setUserName("suport");
sender.setUserPassword("********");
sender.setServerEmail("suport@centit.com");
return sender;
}
- 定义个通知中心bean(实现NotificationCenter接口的bean)。
@Bean
public NotificationCenter notificationCenter() {
NotificationCenterImpl notificationCenter = new NotificationCenterImpl();
//这个不是必须的,只是为了在没有真正的发送类时不报错
notificationCenter.initDummyMsgSenders();
//打开消息推送服务日志
notificationCenter.setWriteNoticeLog(true);
return notificationCenter;
}
- 在web初始化时将消息发送类的bean注册到通知中心bean中。
public class InstantiationServiceBeanPostProcessor
implements ApplicationListener<ContextRefreshedEvent>{
@Autowired
protected NotificationCenter notificationCenter;
@Autowired
private EmailMessageSenderImpl emailMessageSender;
@Override
public void onApplicationEvent(ContextRefreshedEvent event){
notificationCenter.registerMessageSender("email", emailMessageSender);
notificationCenter.appointDefaultSendType("email");
}
- 在使用的地方引入 通知中心bean 。
@Autowired
protected NotificationCenter notificationCenter;