消息队列MassageComponent示例

来源:互联网 发布:unity3d实现物体闪烁 编辑:程序博客网 时间:2024/05/22 00:36

/**
 * 消息推送组件
 * 
 */
@Component
public class MassageComponent {

    private static Logger logger = LoggerFactory.getLogger(AnalyticsServiceImpl.class);

    @Autowired
    private IPushService pushService;

    /** 消息队列 **/
    private BlockingQueue<BaseMsg> linkedBlockingQueue = new LinkedBlockingQueue<>();


    /**
     * 消息入列
     * 
     */
    public void addMassage(BaseMsg msg) {
        if (!this.linkedBlockingQueue.offer(msg)) {
            logger.warn("消息队列已满!size=" + linkedBlockingQueue.size());
        }
    }


    /**
     * 开始消息推送任务
     */
    public void startPush() {
        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(1);
        fixedThreadPool.execute(new Runnable() {
            public void run() {
                while (true) {
                    BaseMsg baseMsg = linkedBlockingQueue.poll();
                    if (baseMsg != null) {
                        /** 点对点推送 **/
                        if (baseMsg instanceof MsgInfo) {
                            MsgInfo msg = (MsgInfo) baseMsg;
                            Map<String, String> extras = new HashMap<>();
                            extras.put("url", "gmu://pushData?msg_id=" + msg.getMsg_id());
                            try {
                                pushService.pushByAlias(msg.getAuth_id(), msg.getMsg_digest(), extras);
                            } catch (APIConnectionException e) {
                                logger.info("用户:auth_id=" + msg.getAuth_id() + ",msg_id=" + msg.getMsg_id() + "推送失败!");
                                logger.error("连接失败,稍后再试", e);
                            } catch (APIRequestException e) {
                                logger.warn("HTTP Status: " + e.getStatus());
                                logger.warn("Error Code: " + e.getErrorCode());
                                logger.warn("Error Message: " + e.getErrorMessage());
                                logger.warn("Msg ID: " + e.getMsgId());
                                logger.info("用户:auth_id=" + msg.getAuth_id() + ",msg_id=" + msg.getMsg_id() + "推送失败!");
                            }
                            /** 广播 **/
                        } else if (baseMsg instanceof BroadCastInfo) {
                            BroadCastInfo bcInfo = (BroadCastInfo) baseMsg;
                            try {
                                pushService.boradCastAll(bcInfo.getMsg_digest());
                            } catch (APIConnectionException e) {
                                logger.error("连接失败,稍后再试", e);
                            } catch (APIRequestException e) {
                                logger.warn("HTTP Status: " + e.getStatus());
                                logger.warn("Error Code: " + e.getErrorCode());
                                logger.warn("Error Message: " + e.getErrorMessage());
                                logger.warn("Msg ID: " + e.getMsgId());
                                logger.info("咨询推送失败:msg_id=" + bcInfo.getMsg_id());
                            }
                        }
                    }
                }
            }
        });
    }

}
0 0
原创粉丝点击