Search in sources :

Example 1 with MessageType

use of info.xiancloud.core.distribution.MessageType in project xian by happyyangyuan.

the class NonblockingMqttCallBack method messageArrived.

@Override
public void messageArrived(String topicIgnored, MqttMessage message) throws Exception {
    try {
        /*第一个参数如果包含"."会被eclipse paho转为"/",所以,不要依赖这个参数!*/
        String msgFullStr = message.toString();
        JSONObject json = JSONObject.parseObject(msgFullStr);
        IdManager.makeSureMsgId(json);
        MessageType messageType = MessageType.getMessageType(json);
        switch(messageType) {
            case offline:
                LOG.info(String.format("离线广播: %s", json));
                break;
            case request:
                UnitRequest request = Reflection.toType(json, UnitRequest.class);
                logMqttFly(request, msgFullStr);
                String group = request.getContext().getGroup(), unit = request.getContext().getUnit();
                request.getContext().setFromRemote(true);
                ISequencer.build(group, unit, json).sequence(// run this runnable if succeeded.
                () -> new DefaultLocalAsyncSender(request, new NotifyHandler() {

                    protected void handle(UnitResponse unitResponse) {
                        LocalNodeManager.sendBack(unitResponse);
                    }
                }).send(), /*failed directly, call this handler*/
                new NotifyHandler() {

                    protected void handle(UnitResponse unitResponse) {
                        LocalNodeManager.sendBack(unitResponse);
                    }
                });
                break;
            case response:
                LOG.debug("ssid只在远程请求和响应时才会有值");
                UnitResponse response = Reflection.toType(json, UnitResponse.class);
                logMqttFly(response, msgFullStr);
                String ssid = response.getContext().getSsid();
                NotifyHandler handler = LocalNodeManager.handleMap.getIfPresent(ssid);
                LocalNodeManager.handleMap.invalidate(ssid);
                UnitResponse responseUnitResponse = UnitResponse.create(json);
                if (handler == null) {
                    LOG.error(String.format("ssid=%s的消息没有找到对应的notifyHandler!整个消息内容=%s,", ssid, json), new Throwable());
                    break;
                }
                try {
                    ThreadPoolManager.execute(() -> {
                        handler.callback(responseUnitResponse);
                    });
                } catch (RejectedExecutionException threadPoolAlreadyShutdown) {
                    LOG.info("线程池已关闭,这里使用临时线程执行任务,针对停服务时线程池已关闭的情况。");
                    new Thread(() -> handler.callback(responseUnitResponse)).start();
                }
                break;
            default:
                LOG.error("未知的mqtt消息类型:" + messageType, new RuntimeException());
        }
    } catch (Throwable e) {
        LOG.error(e);
    } finally {
        MsgIdHolder.clear();
    }
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) UnitRequest(info.xiancloud.core.message.UnitRequest) UnitResponse(info.xiancloud.core.message.UnitResponse) NotifyHandler(info.xiancloud.core.NotifyHandler) DefaultLocalAsyncSender(info.xiancloud.core.message.sender.local.DefaultLocalAsyncSender) MessageType(info.xiancloud.core.distribution.MessageType) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Aggregations

JSONObject (com.alibaba.fastjson.JSONObject)1 NotifyHandler (info.xiancloud.core.NotifyHandler)1 MessageType (info.xiancloud.core.distribution.MessageType)1 UnitRequest (info.xiancloud.core.message.UnitRequest)1 UnitResponse (info.xiancloud.core.message.UnitResponse)1 DefaultLocalAsyncSender (info.xiancloud.core.message.sender.local.DefaultLocalAsyncSender)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1