当前位置: 首页>>代码示例>>Java>>正文


Java MqttClient.setCallback方法代码示例

本文整理汇总了Java中org.eclipse.paho.client.mqttv3.MqttClient.setCallback方法的典型用法代码示例。如果您正苦于以下问题:Java MqttClient.setCallback方法的具体用法?Java MqttClient.setCallback怎么用?Java MqttClient.setCallback使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.eclipse.paho.client.mqttv3.MqttClient的用法示例。


在下文中一共展示了MqttClient.setCallback方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getMqttClient

import org.eclipse.paho.client.mqttv3.MqttClient; //导入方法依赖的package包/类
/**
 * get MqttClient by clientKey
 * @param clientKey
 * @return
 * @throws MqttException
 */
public static MqttClient getMqttClient(String serverURI, String clientId,StringRedisTemplate redisTemplate) 
                throws MqttException{
	 String clientKey=serverURI.concat(clientId);
     if(clientMap.get(clientKey)==null){
         lock.lock();
             if(clientMap.get(clientKey)==null){
            	 MqttClientPersistence persistence = new MemoryPersistence();
            	
                 MqttClient client = new MqttClient(serverURI, clientId, persistence);
                 MqttConnectOptions connOpts = new MqttConnectOptions();
                 
                 MqttCallback callback = new IMMqttCallBack(client,redisTemplate);
                 client.setCallback(callback);
                 
                 connOpts.setCleanSession(true);
                 client.connect(connOpts);
                 clientMap.put(clientKey, client);
             }
          lock.unlock();
     }
      return clientMap.get(clientKey);
}
 
开发者ID:projectsrepos,项目名称:jim,代码行数:29,代码来源:MqttClientFactory.java

示例2: init

import org.eclipse.paho.client.mqttv3.MqttClient; //导入方法依赖的package包/类
public void init() throws MqttException {
    try {
        String url = mqttProperties.getHostname() + ":" + mqttProperties.getPort();
        LOGGER.info("Opening MQTT connection: '{}'", url);
        LOGGER.info("properties: {}", mqttProperties);
        MqttConnectOptions connectOptions = new MqttConnectOptions();
        connectOptions.setUserName(mqttProperties.getUsername());
        connectOptions.setPassword(mqttProperties.getPassword().toCharArray());
        connectOptions.setCleanSession(false);
        client = new MqttClient(url, mqttProperties.getClientName(), new MemoryPersistence());
        client.setCallback(onMessageArrived);
        client.connect(connectOptions);
        client.subscribe(mqttProperties.getTopic());
    } catch (MqttException e) {
        LOGGER.error(e.getMessage(), e);
        throw e;
    }
}
 
开发者ID:trustedanalytics,项目名称:mqtt-listener-example,代码行数:19,代码来源:Ingestion.java

示例3: connectAndSubscribe

import org.eclipse.paho.client.mqttv3.MqttClient; //导入方法依赖的package包/类
/*********************************************************************************************************************************************************************
 *
 */
private void connectAndSubscribe() throws Exception {

  ConfigHandler configHandler = serialMqttBridge.getConfigHandler();

  mqttClient = new MqttClient(configHandler.getMqttBrokerUrl(), configHandler.getMqttClientId(), null);
  MqttConnectOptions connOpts = new MqttConnectOptions();
  connOpts.setCleanSession(true);
  connOpts.setAutomaticReconnect(true);

  // Authentication
  if (configHandler.getMqttBrokerUsername() != null && configHandler.getMqttBrokerPassword() != null) {
    connOpts.setUserName(configHandler.getMqttBrokerUsername());
    connOpts.setPassword(configHandler.getMqttBrokerPassword().toCharArray());
  }

  // MqttCallback
  mqttCallback = new MqttSubscriptionCallback(this);
  mqttClient.setCallback(mqttCallback);

  mqttClient.connect(connOpts);

  // Subscribe to defined inbound topic
  mqttClient.subscribe(configHandler.getMqttTopicSubscribe(), configHandler.getMqttQosSubscribe());
}
 
开发者ID:DerTomm,项目名称:SerialMqttBridge,代码行数:28,代码来源:MqttHandler.java

示例4: connectClient

import org.eclipse.paho.client.mqttv3.MqttClient; //导入方法依赖的package包/类
private void connectClient() {
  try {
    client = new MqttClient(broker, clientId);
    client.setCallback(this);
    MqttConnectOptions connOpts = new MqttConnectOptions();
    connOpts.setUserName(user);
    connOpts.setPassword(password.toCharArray());
    connOpts.setCleanSession(true);
    connOpts.setKeepAliveInterval(OUTGOING_MQTT_KEEP_ALIVE);
    logger.debug("Connecting to broker:  " + broker);
    client.connect(connOpts);
    logger.debug("Connected");
  } catch (MqttException e) {
    logger.error("Failed to connect to MQTT client ( " + broker + "/" + clientId
        + ") for outbound messages");
    logger.error(e.getLocalizedMessage());
    e.printStackTrace();
  }
}
 
开发者ID:edgexfoundry,项目名称:device-mqtt,代码行数:20,代码来源:OutgoingSender.java

示例5: startListening

import org.eclipse.paho.client.mqttv3.MqttClient; //导入方法依赖的package包/类
private void startListening() {
  logger.debug("Starting listening for response traffic");
  try {
    String url =
        cmdrespMqttBrokerProtocol + "://" + cmdrespMqttBroker + ":" + cmdrespMqttBrokerPort;
    client = new MqttClient(url, cmdrespMqttClientId);
    MqttConnectOptions connOpts = new MqttConnectOptions();
    connOpts.setUserName(cmdrespMqttUser);
    connOpts.setPassword(cmdrespMqttPassword.toCharArray());
    connOpts.setCleanSession(true);
    connOpts.setKeepAliveInterval(cmdrespMqttKeepAlive);
    logger.debug("Connecting to response message broker:  " + cmdrespMqttBroker);
    client.connect(connOpts);
    logger.debug("Connected to response message broker");
    client.setCallback(this);
    client.subscribe(cmdrespMqttTopic, cmdrespMqttQos);
  } catch (MqttException e) {
    logger.error("Unable to connect to response message queue.  "
        + "Unable to respond to command requests.");
    e.printStackTrace();
    client = null;
  }
}
 
开发者ID:edgexfoundry,项目名称:device-mqtt,代码行数:24,代码来源:CommandResponseListener.java

示例6: startListening

import org.eclipse.paho.client.mqttv3.MqttClient; //导入方法依赖的package包/类
private void startListening() {
  logger.debug("Starting listening for incoming traffic");
  try {
    String url =
        incomingMqttBrokerProtocol + "://" + incomingMqttBroker + ":" + incomingMqttBrokerPort;
    client = new MqttClient(url, incomingMqttClientId);
    MqttConnectOptions connOpts = new MqttConnectOptions();
    connOpts.setUserName(incomingMqttUser);
    connOpts.setPassword(incomingMqttPassword.toCharArray());
    connOpts.setCleanSession(true);
    connOpts.setKeepAliveInterval(incomingMqttKeepAlive);
    logger.debug("Connecting to incoming message broker:  " + incomingMqttBroker);
    client.connect(connOpts);
    logger.debug("Connected to incoming message broker");
    client.setCallback(this);
    client.subscribe(incomingMqttTopic, incomingMqttQos);
  } catch (MqttException e) {
    logger.error("Unable to connect to incoming message queue.");
    e.printStackTrace();
    client = null;
  }
}
 
开发者ID:edgexfoundry,项目名称:device-mqtt,代码行数:23,代码来源:IncomingListener.java

示例7: subscribe

import org.eclipse.paho.client.mqttv3.MqttClient; //导入方法依赖的package包/类
public synchronized void subscribe() {
    if (!Utils.isInternetAvailable(context)) {
        return;
    }
    final String deviceKeyString = MobiComUserPreference.getInstance(context).getDeviceKeyString();
    final String userKeyString = MobiComUserPreference.getInstance(context).getSuUserKeyString();
    if (TextUtils.isEmpty(deviceKeyString) || TextUtils.isEmpty(userKeyString)) {
        return;
    }
    try {
        final MqttClient client = connect();
        if (client == null || !client.isConnected()) {
            return;
        }
        connectPublish(userKeyString, deviceKeyString, "1");
        subscribeToConversation();
        if (client != null) {
            client.setCallback(ApplozicMqttService.this);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
开发者ID:AppLozic,项目名称:Applozic-Android-Chat-Sample,代码行数:24,代码来源:ApplozicMqttService.java

示例8: ProtobufMqttProtocolHandler

import org.eclipse.paho.client.mqttv3.MqttClient; //导入方法依赖的package包/类
public ProtobufMqttProtocolHandler(NativeDeviceFactoryInterface deviceFactory) {
	super(deviceFactory);

	String mqtt_url = PropertyUtil.getProperty(MQTT_URL_PROP, null);
	if (mqtt_url == null) {
		throw new RuntimeIOException("Property '" + MQTT_URL_PROP + "' must be set");
	}

	try {
		mqttClient = new MqttClient(mqtt_url, MqttClient.generateClientId(), new MemoryPersistence());
		mqttClient.setCallback(this);
		MqttConnectOptions con_opts = new MqttConnectOptions();
		con_opts.setAutomaticReconnect(true);
		con_opts.setCleanSession(true);
		mqttClient.connect(con_opts);
		Logger.debug("Connected to {}", mqtt_url);

		// Subscribe
		Logger.debug("Subscribing to response and notification topics...");
		mqttClient.subscribe(MqttProviderConstants.RESPONSE_TOPIC);
		mqttClient.subscribe(MqttProviderConstants.GPIO_NOTIFICATION_TOPIC);
		Logger.debug("Subscribed");
	} catch (MqttException e) {
		throw new RuntimeIOException(e);
	}
}
 
开发者ID:mattjlewis,项目名称:diozero,代码行数:27,代码来源:ProtobufMqttProtocolHandler.java

示例9: MqttTestClient

import org.eclipse.paho.client.mqttv3.MqttClient; //导入方法依赖的package包/类
public MqttTestClient(String mqttUrl) throws MqttException {
	mqttClient = new MqttClient(mqttUrl, MqttClient.generateClientId(), new MemoryPersistence());
	mqttClient.setCallback(this);
	MqttConnectOptions con_opts = new MqttConnectOptions();
	con_opts.setAutomaticReconnect(true);
	con_opts.setCleanSession(true);
	mqttClient.connect(con_opts);
	Logger.debug("Connected to {}", mqttUrl);

	lock = new ReentrantLock();
	conditions = new HashMap<>();
	responses = new HashMap<>();

	// Subscribe
	Logger.debug("Subscribing to {}...", MqttProviderConstants.RESPONSE_TOPIC);
	mqttClient.subscribe(MqttProviderConstants.RESPONSE_TOPIC);
	Logger.debug("Subscribed");
}
 
开发者ID:mattjlewis,项目名称:diozero,代码行数:19,代码来源:MqttTestClient.java

示例10: run

import org.eclipse.paho.client.mqttv3.MqttClient; //导入方法依赖的package包/类
public void run() {
	try {
		// Connect to the MQTT Server
		MqttConnectOptions options = new MqttConnectOptions();
		options.setAutomaticReconnect(true);
		options.setCleanSession(true);
		options.setConnectionTimeout(30);
		options.setKeepAliveInterval(30);
		options.setUserName(username);
		options.setPassword(password.toCharArray());
		client = new MqttClient(serverUrl, clientId);
		client.setTimeToWait(5000);						// short timeout on failure to connect
		client.connect(options);
		client.setCallback(this);
		
		// Just listen to all DDATA messages on spAv1.0 topics and wait for inbound messages
		client.subscribe("spBv1.0/#", 0);
	} catch(Exception e) {
		e.printStackTrace();
	}
}
 
开发者ID:Cirrus-Link,项目名称:Sparkplug,代码行数:22,代码来源:SparkplugListener.java

示例11: connectClient

import org.eclipse.paho.client.mqttv3.MqttClient; //导入方法依赖的package包/类
private void connectClient() throws Exception {
	try {
	    String mqttServerAddress = String.format("ssl://%s:%s", mqttBridgeHostname, mqttBridgePort);

		MqttConnectOptions connectOptions = new MqttConnectOptions();
	    connectOptions.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1_1);
	    connectOptions.setUserName(username);
	    password = createJwt(projectId, privateKeyFile, algorithm);
	    connectOptions.setPassword(password.toCharArray());

		connectOptions.setCleanSession(true);
		connectOptions.setKeepAliveInterval(keepAlive);

	    client = new MqttClient(mqttServerAddress, clientId, new MemoryPersistence());
		client.setCallback(this);

		logger.debug("Connecting to broker:  " + mqttServerAddress);
		client.connect(connectOptions);
		logger.debug("Connected");
		
	} catch (Exception e) {
		logger.error("Failed to connect to MQTT client ( " + mqttBridgeHostname + ":" + mqttBridgePort + "/" + clientId + ") for outbound messages");
		throw e;
	}
}
 
开发者ID:edgexfoundry,项目名称:export-distro,代码行数:26,代码来源:IotCoreMQTTSender.java

示例12: doDemo

import org.eclipse.paho.client.mqttv3.MqttClient; //导入方法依赖的package包/类
public void doDemo(String tcpUrl, String clientId, String topicName) {
	try {
	  MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
	  mqttConnectOptions.setMqttVersion(4);
		client = new MqttClient(tcpUrl, clientId);
		client.connect(mqttConnectOptions);
		client.setCallback(this);
		client.subscribe(topicName);
	} catch (MqttException e) {
		e.printStackTrace();
	}
}
 
开发者ID:osswangxining,项目名称:mqttserver,代码行数:13,代码来源:SubscribeMessage.java

示例13: initializeMqttClient

import org.eclipse.paho.client.mqttv3.MqttClient; //导入方法依赖的package包/类
private void initializeMqttClient()
    throws MqttException, IOException, NoSuchAlgorithmException, InvalidKeySpecException {

    mqttClient = new MqttClient(mMqttOptions.getBrokerUrl(),
            mMqttOptions.getClientId(), new MemoryPersistence());

    MqttConnectOptions options = new MqttConnectOptions();
    // Note that the the Google Cloud IoT only supports MQTT 3.1.1, and Paho requires that we
    // explicitly set this. If you don't set MQTT version, the server will immediately close its
    // connection to your device.
    options.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1_1);
    options.setUserName(CloudIotCoreOptions.UNUSED_ACCOUNT_NAME);
    options.setAutomaticReconnect(true);

    // generate the jwt password
    options.setPassword(mqttAuth.createJwt(mMqttOptions.getProjectId()));

    mqttClient.setCallback(this);
    mqttClient.connect(options);

    if(mqttClient.isConnected()) {
        try{
            mSubTopic = "/devices/sense_hub_2.0_android_things/config";// + NetworkUtils.getMACAddress(mContext);
            Log.i(TAG, "initializeMqttClient subscribe topic=" + mSubTopic);
            mqttClient.subscribe(mSubTopic, 1);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    mReady.set(true);
}
 
开发者ID:dmtan90,项目名称:Sense-Hub-Android-Things,代码行数:32,代码来源:MqttIoTPublisher.java

示例14: initializeMqttClient

import org.eclipse.paho.client.mqttv3.MqttClient; //导入方法依赖的package包/类
private void initializeMqttClient()
        throws MqttException, IOException, NoSuchAlgorithmException, InvalidKeySpecException {
    Log.d(TAG, "initializeMqttClient broker=" + mMqttOptions.getBrokerUrl() +
            " clientID=" + mMqttOptions.getClientId() +
            " username=" + mMqttOptions.getUsername() +
            " password=" + mMqttOptions.getPassword());
    mqttClient = new MqttClient(mMqttOptions.getBrokerUrl(),
            mMqttOptions.getClientId(), new MemoryPersistence());

    MqttConnectOptions options = new MqttConnectOptions();
    // Note that the the Google Cloud IoT only supports MQTT 3.1.1, and Paho requires that we
    // explicitly set this. If you don't set MQTT version, the server will immediately close its
    // connection to your device.
    options.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1_1);
    options.setUserName(mMqttOptions.getUsername());
    options.setPassword(mMqttOptions.getPassword().toCharArray());

    options.setAutomaticReconnect(true);

    mqttClient.setCallback(this);
    mqttClient.connect(options);

    if(mqttClient.isConnected()) {
        try{
            Log.i(TAG, "initializeMqttClient subscribe topic=" + mMqttOptions.getSubscribeTopicName());
            mqttClient.subscribe(mMqttOptions.getSubscribeTopicName(), 1);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    else{
        Log.e(TAG, "Can't connect to " + mMqttOptions.getBrokerUrl());
    }
    mReady.set(true);
}
 
开发者ID:dmtan90,项目名称:Sense-Hub-Android-Things,代码行数:36,代码来源:MqttPublisher.java

示例15: startMQTTClient

import org.eclipse.paho.client.mqttv3.MqttClient; //导入方法依赖的package包/类
private static void startMQTTClient() throws MqttException {
	System.out.println("Starting MQTT Client ...");
	mqttClient = new MqttClient(mqttServer, "client-for-led-" + ledControllerHost);
	mqttClient.setCallback(new Callback(ledHandler, udpClient));
	mqttClient.connect();
	mqttClient.subscribe(topic + "/+/+");
	System.out.println("Connected and subscribed to " + topic);
}
 
开发者ID:magcode,项目名称:sunricher-wifi-mqtt,代码行数:9,代码来源:Client.java


注:本文中的org.eclipse.paho.client.mqttv3.MqttClient.setCallback方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。