/src/main/java/com/linbox/im/server/router/handlers/dispatcher/SendDispatcher.java
Java | 60 lines | 46 code | 9 blank | 5 comment | 4 complexity | 679fa4b58aa872962cc6d25f72fd1981 MD5 | raw file
- package com.linbox.im.server.router.handlers.dispatcher;
- import com.alibaba.fastjson.JSON;
- import com.linbox.im.exceptions.IMException;
- import com.linbox.im.message.MessageType;
- import com.linbox.im.message.Message;
- import com.linbox.im.server.constant.MessageTopic;
- import org.apache.kafka.clients.producer.KafkaProducer;
- import org.apache.kafka.clients.producer.ProducerRecord;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- /**
- * Created by lrsec on 8/26/15.
- */
- @Service
- public class SendDispatcher {
- private static Logger logger = LoggerFactory.getLogger(SendDispatcher.class);
- @Autowired
- private KafkaProducer<String, String> kafkaProducer;
- // dispatch to single user
- public void dispatchToSingle(String userId, String remoteId, String sessionKey, MessageType type, Message msg) {
- try {
- SendDispatchMessage message = new SendDispatchMessage();
- message.setUserId(userId);
- message.setRemoteId(remoteId);
- message.setSessionKey(sessionKey);
- message.setMessage(msg);
- message.setType(type);
- String json = JSON.toJSONString(message);
- if (json == null) {
- throw new IMException("Message to user " + userId + "can not be parsed to json correctly.");
- }
- kafkaProducer.send(new ProducerRecord<String, String>(MessageTopic.TOPIC_DISPATCH_SEND_SINGLE, json));
- } catch (Exception e) {
- logger.error("Dispatch send message to single user " + userId + "fail.", e);
- }
- }
- //dispatch to group
- public void dispatchToGroup(String groupId, Message message) {
- try {
- String json = JSON.toJSONString(message);
- if (json == null) {
- throw new IMException("Message to group " + groupId + "can not be parsed to json correctly.");
- }
- kafkaProducer.send(new ProducerRecord<String, String>(MessageTopic.TOPIC_DISPATCH_SEND_GROUP, json));
- } catch (Exception e) {
- logger.error("Dispatch send message to group " + groupId + " fail.", e);
- }
- }
- }