本文整理汇总了Java中org.eclipse.paho.client.mqttv3.MqttClient.connect方法的典型用法代码示例。如果您正苦于以下问题:Java MqttClient.connect方法的具体用法?Java MqttClient.connect怎么用?Java MqttClient.connect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.eclipse.paho.client.mqttv3.MqttClient
的用法示例。
在下文中一共展示了MqttClient.connect方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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;
}
}
示例2: 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());
}
示例3: MqttClientKetiSub
import org.eclipse.paho.client.mqttv3.MqttClient; //导入方法依赖的package包/类
public MqttClientKetiSub(String serverUrl) {
this.mqttServerUrl = serverUrl;
System.out.println("[KETI MQTT Client] Client Initialize");
try {
mqc = new MqttClient(mqttServerUrl, mqttClientId, persistence);
while(!mqc.isConnected()){
mqc.connect();
System.out.println("[KETI MQTT Client] Connection try");
}
System.out.println("[KETI MQTT Client] Connected to Server - " + mqttServerUrl);
} catch (MqttException e) {
e.printStackTrace();
}
}
示例4: MqttClientKetiPub
import org.eclipse.paho.client.mqttv3.MqttClient; //导入方法依赖的package包/类
public MqttClientKetiPub(String serverUrl) {
this.mqttServerUrl = serverUrl;
System.out.println("[KETI MQTT Client] Client Initialize");
try {
mqc = new MqttClient(mqttServerUrl, mqttClientId, persistence);
while(!mqc.isConnected()){
mqc.connect();
System.out.println("[KETI MQTT Client] Connection try");
}
System.out.println("[KETI MQTT Client] Connected to Server - " + mqttServerUrl);
} catch (MqttException e) {
e.printStackTrace();
}
}
示例5: initializeMqttClient
import org.eclipse.paho.client.mqttv3.MqttClient; //导入方法依赖的package包/类
private void initializeMqttClient()
throws MqttException, IOException, NoSuchAlgorithmException, InvalidKeySpecException {
mqttClient = new MqttClient(cloudIotOptions.getBrokerUrl(),
cloudIotOptions.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(CloudIotOptions.UNUSED_ACCOUNT_NAME);
// generate the jwt password
options.setPassword(mqttAuth.createJwt(cloudIotOptions.getProjectId()));
mqttClient.connect(options);
mReady.set(true);
}
示例6: 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();
}
}
示例7: 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;
}
}
示例8: testInvalidClientIdentifier
import org.eclipse.paho.client.mqttv3.MqttClient; //导入方法依赖的package包/类
@Test
public void testInvalidClientIdentifier(TestContext context) throws Exception {
MemoryPersistence persistence = new MemoryPersistence();
MqttClient client = new MqttClient(String.format("tcp://%s:%d", MQTT_SERVER_HOST, MQTT_SERVER_PORT), "invalid-id-with-24-chars", persistence);
MqttConnectOptions options = new MqttConnectOptions();
options.setMqttVersion(MQTT_VERSION_3_1);
try {
client.connect(options);
context.assertTrue(false);
} catch (MqttException ignore) {
context.assertTrue(true);
}
}
示例9: 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();
}
}
示例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: subscribe
import org.eclipse.paho.client.mqttv3.MqttClient; //导入方法依赖的package包/类
private void subscribe(TestContext context, String topic, int expectedQos) {
this.async = context.async();
try {
MemoryPersistence persistence = new MemoryPersistence();
MqttClient client = new MqttClient(String.format("tcp://%s:%d", MQTT_SERVER_HOST, MQTT_SERVER_PORT), "12345", persistence);
client.connect();
String[] topics = new String[]{topic};
int[] qos = new int[]{expectedQos};
// after calling subscribe, the qos is replaced with granted QoS that should be the same
client.subscribe(topics, qos);
this.async.await();
context.assertTrue(qos[0] == expectedQos);
} catch (MqttException e) {
context.assertTrue(!topic.equals(MQTT_TOPIC_FAILURE) ? false : true);
e.printStackTrace();
}
}
示例12: 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;
}
}
示例13: main
import org.eclipse.paho.client.mqttv3.MqttClient; //导入方法依赖的package包/类
public static void main(String args[]) {
String topic = "iot/iot";
String content = "Hello ith";
int qos = 2;
String broker = "tcp://127.0.0.1:1883";
String clientId = "sample";
MemoryPersistence persistence = new MemoryPersistence();
try {
MqttClient sampleClient = new MqttClient(broker, clientId, persistence);
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setCleanSession(true);
System.out.println("Connecting to broker");
sampleClient.connect(connOpts);
System.out.println("connected");
System.out.println("Publishing meessage: " + content);
MqttMessage message = new MqttMessage(content.getBytes());
message.setQos(qos);
sampleClient.publish(topic, message);
System.out.println("Message published");
sampleClient.disconnect();
System.out.println("Disconnected");
System.exit(0);
} catch (MqttException e){
System.out.println("reason " + e.getReasonCode());
System.out.println("msg " + e.getMessage());
System.out.println("loc " + e.getLocalizedMessage());
System.out.println("cause " + e.getCause());
System.out.println("exxcep " + e);
}
}
示例14: pubMsg
import org.eclipse.paho.client.mqttv3.MqttClient; //导入方法依赖的package包/类
public static void pubMsg(String tcpUrl, String clientId, String topicName,
String message) throws MqttException, UnsupportedEncodingException {
MqttClient client = new MqttClient(tcpUrl, clientId);
MqttConnectOptions mqcConf = new MqttConnectOptions();
mqcConf.setConnectionTimeout(300);
mqcConf.setKeepAliveInterval(1200);
client.connect(mqcConf);
MqttTopic topic = client.getTopic(topicName);
topic.publish(message.getBytes("utf8"), 1, false);
// client.close();
}
示例15: 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();
}
}