Kafka构建TB级异步消息系统(显示系统通知)
1.MessageMapper
Message selectLatestNotice ( int userId, String topic) ;
int selectNoticeCount ( int userId, String topic) ;
int selectNoticeUnreadCount ( int userId, String topic) ;
2.message-mapper.xml
< select id = " selectLatestNotice" resultType = " Message" >
select
< include refid = " selectFields" > </ include>
from message
where id in(
select max(id) from message
where status != 2
and from_id = 1
and to_id = #{userId}
and conversation_id = #{topic}
)
</ select>
< select id = " selectNoticeCount" resultType = " int" >
select count(id) from message
where status != 2
and from_id = 1
and to_id = #{userId}
and conversation_id = #{topic}
</ select>
< select id = " selectNoticeUnreadCount" resultType = " int" >
select count(id) from message
where status = 0
and from_id = 1
and to_id = #{userId}
< if test = " topic!=null and topic!= ' ' " >
and conversation_id = #{topic}
</ if>
</ select>
3.MessageService
public Message findLatestNotice ( int userId, String topic) {
Message message = messageMapper. selectLatestNotice ( userId, topic) ;
return message;
}
public int findNoticeCount ( int userId, String topic) {
return messageMapper. selectNoticeCount ( userId, topic) ;
}
public int findNoticeUnreadCount ( int userId, String topic) {
return messageMapper. selectNoticeUnreadCount ( userId, topic) ;
}
4.MessageController
@RequestMapping ( value = "/notice/list" , method = RequestMethod . GET)
public String getNoticeList ( Model model) {
User users = hostHolder. getUsers ( ) ;
Message latestNotice = messageService. findLatestNotice ( users. getId ( ) , TOPIC_COMMENT) ;
Map < String , Object > messageVo = new HashMap < > ( ) ;
if ( latestNotice != null ) {
messageVo. put ( "message" , latestNotice) ;
String content = HtmlUtils . htmlUnescape ( latestNotice. getContent ( ) ) ;
Map < String , Object > date = JSONObject . parseObject ( content, HashMap . class ) ;
messageVo. put ( "user" , userService. findByUserId ( ( Integer ) date. get ( "userId" ) ) ) ;
messageVo. put ( "entityType" , date. get ( "entityType" ) ) ;
messageVo. put ( "entityId" , date. get ( "entityId" ) ) ;
messageVo. put ( "postId" , date. get ( "postId" ) ) ;
int count = messageService. findNoticeCount ( users. getId ( ) , TOPIC_COMMENT) ;
messageVo. put ( "count" , count) ;
int unreadCount = messageService. findNoticeUnreadCount ( users. getId ( ) , TOPIC_COMMENT) ;
messageVo. put ( "unreadCount" , unreadCount) ;
}
model. addAttribute ( "commentNotice" , messageVo) ;
latestNotice = messageService. findLatestNotice ( users. getId ( ) , TOPIC_LIKE) ;
messageVo = new HashMap < > ( ) ;
if ( latestNotice != null ) {
messageVo. put ( "message" , latestNotice) ;
String content = HtmlUtils . htmlUnescape ( latestNotice. getContent ( ) ) ;
Map < String , Object > date = JSONObject . parseObject ( content, HashMap . class ) ;
messageVo. put ( "user" , userService. findByUserId ( ( Integer ) date. get ( "userId" ) ) ) ;
messageVo. put ( "entityType" , date. get ( "entityType" ) ) ;
messageVo. put ( "entityId" , date. get ( "entityId" ) ) ;
messageVo. put ( "postId" , date. get ( "postId" ) ) ;
int count = messageService. findNoticeCount ( users. getId ( ) , TOPIC_LIKE) ;
messageVo. put ( "count" , count) ;
int unreadCount = messageService. findNoticeUnreadCount ( users. getId ( ) , TOPIC_LIKE) ;
messageVo. put ( "unreadCount" , unreadCount) ;
}
model. addAttribute ( "likeNotice" , messageVo) ;
latestNotice = messageService. findLatestNotice ( users. getId ( ) , TOPIC_FOLLOW) ;
messageVo = new HashMap < > ( ) ;
if ( latestNotice != null ) {
messageVo. put ( "message" , latestNotice) ;
String content = HtmlUtils . htmlUnescape ( latestNotice. getContent ( ) ) ;
Map < String , Object > date = JSONObject . parseObject ( content, HashMap . class ) ;
messageVo. put ( "user" , userService. findByUserId ( ( Integer ) date. get ( "userId" ) ) ) ;
messageVo. put ( "entityType" , date. get ( "entityType" ) ) ;
messageVo. put ( "entityId" , date. get ( "entityId" ) ) ;
messageVo. put ( "postId" , date. get ( "postId" ) ) ;
int count = messageService. findNoticeCount ( users. getId ( ) , TOPIC_FOLLOW) ;
messageVo. put ( "count" , count) ;
int unreadCount = messageService. findNoticeUnreadCount ( users. getId ( ) , TOPIC_FOLLOW) ;
messageVo. put ( "unreadCount" , unreadCount) ;
}
model. addAttribute ( "followNotice" , messageVo) ;
int letterUnreadCount = messageService. findLetterUnreadCount ( users. getId ( ) , null ) ;
model. addAttribute ( "letterUnreadCount" , letterUnreadCount) ;
int noticeUnreadCount = messageService. findNoticeUnreadCount ( users. getId ( ) , null ) ;
model. addAttribute ( "noticeUnreadCount" , noticeUnreadCount) ;
return "/site/notice" ;
}