本文整理汇总了Java中org.eclipse.paho.client.mqttv3.MqttClient类的典型用法代码示例。如果您正苦于以下问题:Java MqttClient类的具体用法?Java MqttClient怎么用?Java MqttClient使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
MqttClient类属于org.eclipse.paho.client.mqttv3包,在下文中一共展示了MqttClient类的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);
}
示例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;
}
}
示例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());
}
示例4: 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();
}
}
示例5: 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();
}
}
示例6: 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);
}
示例7: 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();
}
}
示例8: 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;
}
}
示例9: 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;
}
}
示例10: 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();
}
示例11: connectPublish
import org.eclipse.paho.client.mqttv3.MqttClient; //导入依赖的package包/类
public synchronized void connectPublish(final String userKeyString, final String deviceKeyString, final String status) {
try {
final MqttClient client = connect();
if (client == null || !client.isConnected()) {
return;
}
MqttMessage message = new MqttMessage();
message.setRetained(false);
message.setPayload((userKeyString + "," + deviceKeyString + "," + status).getBytes());
Utils.printLog(context,TAG, "UserKeyString,DeviceKeyString,status:" + userKeyString + "," + deviceKeyString + "," + status);
message.setQos(0);
client.publish(STATUS, message);
} catch (Exception e) {
e.printStackTrace();
}
}
示例12: 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();
}
}
示例13: publishTopic
import org.eclipse.paho.client.mqttv3.MqttClient; //导入依赖的package包/类
public synchronized void publishTopic(final String applicationId, final String status, final String loggedInUserId, final String userId) {
try {
final MqttClient client = connect();
if (client == null || !client.isConnected()) {
return;
}
MqttMessage message = new MqttMessage();
message.setRetained(false);
message.setPayload((applicationId + "," + loggedInUserId + "," + status).getBytes());
message.setQos(0);
client.publish("typing" + "-" + applicationId + "-" + userId, message);
Utils.printLog(context,TAG, "Published " + new String(message.getPayload()) + " to topic: " + "typing" + "-" + applicationId + "-" + userId);
} catch (Exception e) {
e.printStackTrace();
}
}
示例14: unSubscribeToTypingTopic
import org.eclipse.paho.client.mqttv3.MqttClient; //导入依赖的package包/类
public synchronized void unSubscribeToTypingTopic(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.unsubscribe("typing-" + getApplicationKey(context) + "-" + currentId);
Utils.printLog(context,TAG, "UnSubscribed to topic: " + "typing-" + getApplicationKey(context) + "-" + currentId);
} catch (Exception e) {
e.printStackTrace();
}
}
示例15: publishTopic
import org.eclipse.paho.client.mqttv3.MqttClient; //导入依赖的package包/类
public synchronized void publishTopic(final String applicationId, final String status, final String loggedInUserId, final String userId) {
try {
final MqttClient client = connect();
if (client == null || !client.isConnected()) {
return;
}
MqttMessage message = new MqttMessage();
message.setRetained(false);
message.setPayload((applicationId + "," + loggedInUserId + "," + status).getBytes());
message.setQos(0);
client.publish("typing" + "-" + applicationId + "-" + userId, message);
Utils.printLog(context, TAG, "Published " + new String(message.getPayload()) + " to topic: " + "typing" + "-" + applicationId + "-" + userId);
} catch (Exception e) {
e.printStackTrace();
}
}