PageRenderTime 39ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/linbox/im/server/router/handlers/SyncSystemUnreadHandler.java

https://gitlab.com/Mr.Tomato/linbox_server
Java | 110 lines | 85 code | 22 blank | 3 comment | 2 complexity | 7284ca6d50a0c2a2b2d4afcabb25f100 MD5 | raw file
  1. package com.linbox.im.server.router.handlers;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.linbox.im.exceptions.IMConsumerException;
  5. import com.linbox.im.message.MessageWrapper;
  6. import com.linbox.im.message.system.SyncSystemUnreadRequest;
  7. import com.linbox.im.message.system.SyncSystemUnreadResponse;
  8. import com.linbox.im.message.system.SystemMessage;
  9. import com.linbox.im.message.system.SystemMessageTypes;
  10. import com.linbox.im.message.system.content.SystemUnreadContent;
  11. import com.linbox.im.server.service.IOutboxService;
  12. import com.linbox.im.server.storage.dao.IMessageDAO;
  13. import org.apache.kafka.clients.consumer.ConsumerRecord;
  14. import org.slf4j.Logger;
  15. import org.slf4j.LoggerFactory;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.beans.factory.annotation.Qualifier;
  18. import org.springframework.stereotype.Service;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. /**
  22. * Created by lrsec on 9/24/15.
  23. */
  24. @Service
  25. @Qualifier("syncSystemUnreadHandler")
  26. public class SyncSystemUnreadHandler implements Handler<String, String> {
  27. private static Logger logger = LoggerFactory.getLogger(SyncSystemUnreadHandler.class);
  28. @Autowired
  29. private IMessageDAO messageDAO;
  30. @Autowired
  31. private IOutboxService outboxService;
  32. @Override
  33. public void handle(ConsumerRecord<String, String> record) {
  34. String json = record.value();
  35. try {
  36. logger.debug("Start handling SyncSystemUnreadRequest: {}", json);
  37. MessageWrapper wrapper = JSON.parseObject(json, MessageWrapper.class);
  38. SyncSystemUnreadRequest request = JSON.parseObject(((JSONObject)wrapper.content).toJSONString(), SyncSystemUnreadRequest.class);
  39. wrapper.content = request;
  40. if (request == null) {
  41. logger.error("SyncSystemUnreadCallback format is not correct. Json string: {}", json);
  42. return;
  43. }
  44. String userId = request.userId;
  45. try {
  46. long newFriendsCount = messageDAO.getNewFriendCount(userId);
  47. long newMessageCount = messageDAO.getNewMessageCount(userId);
  48. List<SystemMessage> messages = new ArrayList<>(2);
  49. SystemUnreadContent newFriendsContent = new SystemUnreadContent();
  50. newFriendsContent.unread = newFriendsCount;
  51. SystemMessage newFriends = new SystemMessage();
  52. newFriends.systemType = SystemMessageTypes.NewFriend.getValue();
  53. newFriends.content = JSON.toJSONString(newFriendsContent);
  54. messages.add(newFriends);
  55. SystemUnreadContent newMessageContent = new SystemUnreadContent();
  56. newMessageContent.unread = newMessageCount;
  57. SystemMessage newMessages = new SystemMessage();
  58. newMessages.systemType = SystemMessageTypes.NoticeNotify.getValue();
  59. newMessages.content = JSON.toJSONString(newMessageContent);
  60. messages.add(newMessages);
  61. sendSuccessResponse(userId, messages, request.rId );
  62. } catch (Exception e) {
  63. logger.error("Exception in SyncSystemUnreadCallback", e);
  64. sendFailResponse(userId, e.toString(), request.rId);
  65. }
  66. } catch (Exception e) {
  67. throw new IMConsumerException(e, json);
  68. }
  69. }
  70. private void sendSuccessResponse(String userId, List<SystemMessage> messages, long rid) {
  71. SyncSystemUnreadResponse response = new SyncSystemUnreadResponse();
  72. response.status = 200;
  73. response.errMsg = "";
  74. response.userId = userId;
  75. response.rId = rid;
  76. response.unreads = messages.toArray(new SystemMessage[0]);
  77. outboxService.put(userId, response.toWrapperJson());
  78. }
  79. private void sendFailResponse(String userId, String errMsg, long rid) {
  80. SyncSystemUnreadResponse response = new SyncSystemUnreadResponse();
  81. response.status = 500;
  82. response.errMsg = errMsg;
  83. response.userId = userId;
  84. response.rId = rid;
  85. response.unreads = new SystemMessage[0];
  86. outboxService.put(userId, response.toWrapperJson());
  87. }
  88. }