首页 > 其他 > 详细

引入其他类定义的静态变量

时间:2019-05-07 18:45:49      阅读:126      评论:0      收藏:0      [点我收藏+]

1. 引入方式

  import  static  类路径.*;

2. 示例

2.1 需要被导入的类

package com.streamax.s17.mcs.business.message;import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

/**
 * @Describe:
 * @Author: chenfan
 * @Date: 2019/4/29 13:26
 */
@Slf4j
@Component
public class MqttMessageListener implements IMqttMessageListener {

    public static final String TRANSFER_BEGIN = "s17/media/handout/op_session_client_pull_begin";
    public static final String TRANSFER_END = "s17/media/handout/op_session_client_pull_end";
    public static final String TRANSFER_CONTROL = "s17/media/op_session_control";
}

2.2 需要导入的类

package com.streamax.s17.mcs.business.message;

import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.streamax.s17.mcs.business.message.MqttMessageListener.*;

/**
 * @Describe:
 * @Author: chenfan
 * @Date: 2019/4/26 15:17
 */
@Slf4j
@Component
public class MessageHandler {

    private MqttMessageListener listener;

    private MqttClient mqttClient;

    public static final String filter = "/#";

    @Autowired
    public MessageHandler(MqttClient mqttClient,MqttMessageListener listener ) {
        this.mqttClient = mqttClient;
        this.listener = listener;
        init();
    }

    /**
     * 订阅关心事件
     */
    public void init(){
        try {
       // 导入后即可通过变量名直接使用,不需要通过类名引用变量 mqttClient.subscribe(TRANSFER_BEGIN
+ filter, 1, listener); mqttClient.subscribe(TRANSFER_END + filter, 1, listener); mqttClient.subscribe(TRANSFER_CONTROL + filter, 1, listener); log.info("subscribe topics success"); } catch (MqttException e) { log.error("subscribe topics occur a exception:{ }",e); } } }

 

引入其他类定义的静态变量

原文:https://www.cnblogs.com/virgosnail/p/10826870.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!