com.hp.message.service.event.OnlineEventsService Maven / Gradle / Ivy
package com.hp.message.service.event;
import com.hp.message.domain.EmqxDataMsg;
import com.hp.message.enums.MsgType;
import com.hp.message.event.SendEmqxEvent;
import com.hp.message.property.AppProperty;
import com.hp.message.service.common.AsyncCallService;
import com.hp.message.service.common.InnerEventService;
import com.hp.message.utils.MsgTopicUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Lazy;
import org.springframework.integration.mqtt.event.MqttSubscribedEvent;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
/**
* @author 尚肖磊
* @create 2021-06-25 10:46
* @Description: emqx订阅成功事件处理服务
*/
@Slf4j
@Service
public class OnlineEventsService implements ApplicationListener {
@Lazy
@Autowired
private AppProperty appProperty;
@Lazy
@Autowired
private InnerEventService eventService;
@Lazy
@Autowired
private AsyncCallService asyncCallService;
/**
* 消息处理函数
*
* @param event 消息对象
*/
@Override
public void onApplicationEvent(MqttSubscribedEvent event) {
// 登录成功 发送上线消息
log.debug("client login success {}", event.getMessage());
// 发送SDK上线事件消息
asyncCallService.addDelayTask(() -> {
eventService.publishEvent(getEmqxLoginEvent());
}, 200, TimeUnit.MILLISECONDS);
}
/**
* 构造SDK上线消息
*
* @return
*/
private SendEmqxEvent getEmqxLoginEvent(){
String topic = MsgTopicUtil.getAppPushTopic(appProperty.getAppId());
EmqxDataMsg emqxDataMsg = new EmqxDataMsg();
emqxDataMsg.setMsgType(MsgType.SDK_ONLINE.getMsgTypeId());
SendEmqxEvent sendEmqxEvent = new SendEmqxEvent(this, emqxDataMsg, topic);
return sendEmqxEvent;
}
}