NutzCN Logo
问答 nutz-wk-mini集成mqtt调用 mqtt回调方法 依赖注入是空的
发布于 1230天前 作者 hryc 1417 次浏览 复制 上一个帖子 下一个帖子
标签:

nutz-wk-mini集成mqtt调用 mqtt回调方法 依赖注入是空的

package cn.wizzer.app.web.commons.mqtt;


        import cn.wizzer.app.intellindust.modules.models.Device;
        import cn.wizzer.app.intellindust.modules.services.DeviceService;
        import cn.wizzer.app.intellindust.modules.services.SnapshotInfoService;
        import com.alibaba.fastjson.JSON;
        import com.alibaba.fastjson.JSONArray;
        import com.alibaba.fastjson.JSONObject;
        import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
        import org.eclipse.paho.client.mqttv3.MqttCallback;
        import org.eclipse.paho.client.mqttv3.MqttMessage;
        import org.nutz.boot.AppContext;
        import org.nutz.dao.Cnd;
        import org.nutz.integration.jedis.RedisService;
        import org.nutz.ioc.Ioc;
        import org.nutz.ioc.loader.annotation.Inject;
        import org.nutz.ioc.loader.annotation.IocBean;

/**
 * 发布消息的回调类
 *
 * 必须实现MqttCallback的接口并实现对应的相关接口方法CallBack 类将实现 MqttCallBack。
 * 每个客户机标识都需要一个回调实例。在此示例中,构造函数传递客户机标识以另存为实例数据。
 * 在回调中,将它用来标识已经启动了该回调的哪个实例。
 * 必须在回调类中实现三个方法:
 *
 *  public void messageArrived(MqttTopic topic, MqttMessage message)接收已经预订的发布。
 *
 *  public void connectionLost(Throwable cause)在断开连接时调用。
 *
 *  public void deliveryComplete(MqttDeliveryToken token))
 *  接收到已经发布的 QoS 1 或 QoS 2 消息的传递令牌时调用。
 *  由 MqttClient.connect 激活此回调。
 *
 */
@IocBean
public class PushCallback implements MqttCallback {

    @Inject
    private  DeviceService deviceService;

    @Inject
    private  SnapshotInfoService snapshotInfoService;



    public void connectionLost(Throwable cause) {

        // 连接丢失后,一般在这里面进行重连
        System.out.println("连接断开,可以做重连");
    }

    public void deliveryComplete(IMqttDeliveryToken token) {
        System.out.println("deliveryComplete---------" + token.isComplete());
    }

    public void messageArrived(String topic, MqttMessage message) throws Exception {
        // subscribe后得到的消息会执行到这里面
        System.out.println("接收消息主题 : " + topic);
        System.out.println("接收消息Qos : " + message.getQos());
        System.out.println("接收消息内容 : " + new String(message.getPayload()));

        try {
            String str = new String(message.getPayload());
            if(!"close".equals(str)){
                JSONObject jsonObject = JSON.parseObject(str);
                String command = jsonObject.getString("command");
                //获取设备信息
                if ("findCaptureDevice".equals(command)) {
                    JSONObject response = jsonObject.getJSONObject("response");
                    JSONArray jsonArray = response.getJSONArray("data");
                    for (Object obj : jsonArray) {
                        JSONObject d = (JSONObject) obj;
                        //根据deviceId 查询设备是否存在
                        String deviceId = d.getString("deviceId");
                        Cnd cnd = Cnd.NEW();
                        cnd.and("deviceId", "=", deviceId);
                        Device device = deviceService.fetch(cnd);
                        if (device == null) {
                            Device deviceIn = new Device();
                            deviceIn.setDeviceId(deviceId);
                            String deviceName = d.getString("deviceName");
                            deviceIn.setDeviceName(deviceName);
                            deviceService.insert(deviceIn);
                        } else {
                            deviceService.update(device);
                        }
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }

}

mqtt 订阅回调方法 messageArrived 里面的依赖注入deviceService 都是空的

1 回复

显然你使用了new:

mqttClient.setCallback(new PushCallback());

应该改成:

mqttClient.setCallback(ioc.get(PushCallback.class));
添加回复
请先登陆
回到顶部