use of org.eclipse.paho.mqttv5.common.MqttException in project dew by gudaoxuri.
the class MqttAdapter method init.
private void init() throws MqttException {
var clientId = mqttConfig.getClientId();
if (clientId == null || clientId.trim().isEmpty()) {
clientId = "Dew_Cluster_" + Cluster.instanceId;
}
MqttClientPersistence persistence;
if ("memory".equalsIgnoreCase(mqttConfig.getPersistence())) {
persistence = new MemoryPersistence();
} else {
persistence = new MqttDefaultFilePersistence();
}
client = new MqttClient(mqttConfig.getBroker(), clientId, persistence);
var connOpts = new MqttConnectionOptions();
if (mqttConfig.getUserName() != null) {
connOpts.setUserName(mqttConfig.getUserName());
}
if (mqttConfig.getPassword() != null) {
connOpts.setPassword(mqttConfig.getPassword().getBytes());
}
if (mqttConfig.getTimeoutSec() != null) {
connOpts.setConnectionTimeout(mqttConfig.getTimeoutSec());
}
if (mqttConfig.getKeepAliveIntervalSec() != null) {
connOpts.setKeepAliveInterval(mqttConfig.getKeepAliveIntervalSec());
}
connOpts.setAutomaticReconnect(true);
logger.info("[" + clientId + "] Connecting to broker: " + mqttConfig.getBroker());
client.connect(connOpts);
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
try {
client.disconnect();
client.close();
} catch (MqttException e) {
e.printStackTrace();
}
}));
}
use of org.eclipse.paho.mqttv5.common.MqttException in project ConnectorPlugin by Phoenix616.
the class MqttConnection method sendMessage.
public void sendMessage(String senderName, Message message) {
byte[] messageData = message.writeToByteArray();
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF(message.getGroup());
out.writeUTF(senderName != null ? senderName : "");
out.writeInt(messageData.length);
out.write(messageData);
byte[] dataToSend = out.toByteArray();
plugin.runAsync(() -> {
try {
client.publish(plugin.getMessageChannel(), dataToSend, 1, false);
} catch (MqttException e) {
e.printStackTrace();
}
});
}
use of org.eclipse.paho.mqttv5.common.MqttException in project java-example by saxingz.
the class ConsumerDemo method main.
public static void main(String[] args) {
String topic = "testExamples/p2p/1";
String broker = "tcp://192.168.50.47:1883";
String clientId = "JavaConsumerSample-2";
MemoryPersistence persistence = new MemoryPersistence();
try {
MqttConnectionOptions connOpts = new MqttConnectionOptions();
connOpts.setCleanStart(false);
// session 一天过期
connOpts.setSessionExpiryInterval(86400L);
connOpts.setUserName("admin");
connOpts.setPassword("password".getBytes());
MqttAsyncClient sampleClient = new MqttAsyncClient(broker, clientId, persistence);
System.out.println("Connecting to broker: " + broker);
IMqttToken token = sampleClient.connect(connOpts);
token.waitForCompletion();
System.out.println("Connected");
int[] topicQos = { 0, 1, 2 };
String[] topicNames = new String[] { topic };
IMqttToken subscribe = sampleClient.subscribe(topicNames, topicQos, null, null);
sampleClient.setCallback(new MessageReceiver(clientId));
subscribe.waitForCompletion();
// System.out.println("Disconnected");
// System.out.println("Close client.");
// sampleClient.disconnect();
// sampleClient.close();
// System.exit(0);
} catch (MqttException me) {
System.out.println("reason " + me.getReasonCode());
System.out.println("msg " + me.getMessage());
System.out.println("loc " + me.getLocalizedMessage());
System.out.println("cause " + me.getCause());
System.out.println("excep " + me);
me.printStackTrace();
}
}
use of org.eclipse.paho.mqttv5.common.MqttException in project httpx by httpx-sh.
the class MessageSubscribeExecutor method subscribeMqtt5.
public void subscribeMqtt5(URI mqttURI, HttpRequest httpRequest) {
MqttClient mqttClient = null;
try {
UriAndSubject uriAndTopic = getMqttUriAndTopic(mqttURI, httpRequest);
final String clientId = "httpx-" + UUID.randomUUID();
mqttClient = new MqttClient(uriAndTopic.uri(), clientId, new MemoryPersistence());
MqttConnectionOptions connOpts = new MqttConnectionOptions();
connOpts.setCleanStart(true);
String[] usernameAndPassword = httpRequest.getBasicAuthorization();
if (usernameAndPassword != null) {
connOpts.setUserName(usernameAndPassword[0]);
connOpts.setPassword(usernameAndPassword[1].getBytes(StandardCharsets.UTF_8));
}
mqttClient.setCallback(new AbstractMqttCallback() {
private final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
System.out.println(colorOutput("bold,green", dateFormat.format(new Date()) + " message received: "));
final String content = new String(message.getPayload(), StandardCharsets.UTF_8);
if (content.startsWith("{")) {
// pretty json output
System.out.println(prettyJsonFormat(content));
} else {
System.out.println(content);
}
}
});
mqttClient.connect(connOpts);
mqttClient.subscribe(uriAndTopic.subject(), 1);
System.out.println("Succeeded to subscribe: " + uriAndTopic.subject() + "!");
latch();
} catch (Exception e) {
log.error("HTX-105-500", mqttURI, e);
} finally {
if (mqttClient != null) {
try {
mqttClient.disconnect();
} catch (MqttException ignore) {
}
}
}
}
use of org.eclipse.paho.mqttv5.common.MqttException in project pinpoint by naver.
the class PahoMqttV5ClientIT method before.
@BeforeClass
public static void before() throws MqttException {
container.start();
mqttClient = new MqttAsyncClient(container.getBrokerUrl(), UUID.randomUUID().toString(), new MemoryPersistence());
IMqttToken token = mqttClient.connect();
token.waitForCompletion(WAIT_FOR_COMPLETION);
subscribe();
}
Aggregations