本文整理汇总了Java中org.eclipse.paho.client.mqttv3.MqttClient.subscribe方法的典型用法代码示例。如果您正苦于以下问题:Java MqttClient.subscribe方法的具体用法?Java MqttClient.subscribe怎么用?Java MqttClient.subscribe使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.eclipse.paho.client.mqttv3.MqttClient
的用法示例。
在下文中一共展示了MqttClient.subscribe方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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());
}
示例2: 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;
}
}
示例3: 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;
}
}
示例4: testMQtt
import org.eclipse.paho.client.mqttv3.MqttClient; //导入方法依赖的package包/类
@Test
public void testMQtt() throws Exception {
CountDownLatch latch = new CountDownLatch(1);
MqttClient client = new MqttClient("tcp://localhost:" + MQTT_PORT, MqttClient.generateClientId(), new MemoryPersistence());
client.connect();
MqttComponent mqtt = new MqttComponent();
mqtt.client = client;
Publisher<byte[]> fromTopic = mqtt.from("input", byte[].class);
Subscriber<byte[]> toTopic = mqtt.to("output", byte[].class);
Flux.from(fromTopic)
.log()
.subscribe(toTopic);
client.subscribe("output", (topic, message) -> {
result = new Integer(new String(message.getPayload()));
latch.countDown();
});
client.publish("input", new MqttMessage(new Integer(2).toString().getBytes()));
client.publish("input", new MqttMessage(new Integer(2).toString().getBytes()));
latch.await(100, TimeUnit.SECONDS);
Assert.assertEquals(2, result, 0.1);
client.disconnect();
client.close();
}
示例5: subscribeToTypingTopic
import org.eclipse.paho.client.mqttv3.MqttClient; //导入方法依赖的package包/类
public synchronized void subscribeToTypingTopic(Channel channel) {
try {
String currentId = null;
if (channel != null) {
currentId = String.valueOf(channel.getKey());
} else {
MobiComUserPreference mobiComUserPreference = MobiComUserPreference.getInstance(context);
currentId = mobiComUserPreference.getUserId();
}
final MqttClient client = connect();
if (client == null || !client.isConnected()) {
return;
}
client.subscribe("typing-" + getApplicationKey(context) + "-" + currentId, 0);
Utils.printLog(context,TAG, "Subscribed to topic: " + "typing-" + getApplicationKey(context) + "-" + currentId);
} catch (Exception e) {
e.printStackTrace();
}
}
示例6: subscribeToTypingTopic
import org.eclipse.paho.client.mqttv3.MqttClient; //导入方法依赖的package包/类
public synchronized void subscribeToTypingTopic(Channel channel) {
try {
String currentId = null;
if (channel != null) {
currentId = String.valueOf(channel.getKey());
} else {
MobiComUserPreference mobiComUserPreference = MobiComUserPreference.getInstance(context);
currentId = mobiComUserPreference.getUserId();
}
final MqttClient client = connect();
if (client == null || !client.isConnected()) {
return;
}
client.subscribe("typing-" + getApplicationKey(context) + "-" + currentId, 0);
Utils.printLog(context, TAG, "Subscribed to topic: " + "typing-" + getApplicationKey(context) + "-" + currentId);
} catch (Exception e) {
e.printStackTrace();
}
}
示例7: 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);
}
}
示例8: 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");
}
示例9: initMqttClient
import org.eclipse.paho.client.mqttv3.MqttClient; //导入方法依赖的package包/类
/**
* init Mqtt Client
* @param mqtt_connect_count is connect size
*/
private void initMqttClient(String preClientID,long offset,long mqtt_connect_count,String topics[]) {
logger.info("MqttPerformanceClient.performanceTestRun() starting");
long end = offset+mqtt_connect_count;
for (long i = offset; i < end; i++) {
try {
MqttClient client = getMqttClient(preClientID + i);
client.subscribe(topics);
clientMap.put(preClientID + i, client);
logger.info("connected by ID :"+preClientID + i);
} catch (Exception e) {
e.printStackTrace();
}
}
logger.info("MqttPerformanceClient.performanceTestRun() end");
}
示例10: mqttReceiver
import org.eclipse.paho.client.mqttv3.MqttClient; //导入方法依赖的package包/类
private void mqttReceiver(TestContext context, String topic, int qos) {
try {
MemoryPersistence persistence = new MemoryPersistence();
MqttClient client = new MqttClient(String.format("tcp://%s:%d", MQTT_BIND_ADDRESS, MQTT_LISTEN_PORT), SUBSCRIBER_ID, persistence);
client.connect();
client.subscribe(topic, qos, (t, m) -> {
LOG.info("topic: {}, message: {}", t, m);
this.receivedQos = m.getQos();
this.async.complete();
});
} catch (MqttException e) {
context.assertTrue(false);
e.printStackTrace();
}
}
示例11: mqttReceiver
import org.eclipse.paho.client.mqttv3.MqttClient; //导入方法依赖的package包/类
private void mqttReceiver(TestContext context, String topic, int qos) {
try {
MemoryPersistence persistence = new MemoryPersistence();
MqttClient client = new MqttClient(String.format("tcp://%s:%d", MQTT_BIND_ADDRESS, MQTT_LISTEN_PORT), CLIENT_ID, persistence);
client.connect();
client.subscribe(topic, qos, (t, m) -> {
LOG.info("topic: {}, message: {}", t, m);
this.async.complete();
});
} catch (MqttException e) {
context.assertTrue(false);
e.printStackTrace();
}
}
示例12: subscribe
import org.eclipse.paho.client.mqttv3.MqttClient; //导入方法依赖的package包/类
@Test
public void subscribe(TestContext context) {
try {
MemoryPersistence persistence = new MemoryPersistence();
MqttClient client = new MqttClient(String.format("tcp://%s:%d", MQTT_BIND_ADDRESS, MQTT_LISTEN_PORT), SUBSCRIBER_ID, persistence);
client.connect();
String[] topics = new String[]{ MQTT_TOPIC };
int[] qos = new int[]{ 1 };
// after calling subscribe, the qos is replaced with granted QoS that should be the same
client.subscribe(topics, qos);
context.assertTrue(qos[0] == 1);
} catch (MqttException e) {
context.assertTrue(false);
e.printStackTrace();
}
}
示例13: 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();
}
}
示例14: 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);
}
示例15: 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);
}