/**
* 消息推送组件
*
*/
@Component
public class MassageComponent {
private static Logger logger = LoggerFactory.getLogger(AnalyticsServiceImpl.class);
@Autowiredprivate 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());
}
}
}
}
}
});
}
}