use of org.eclipse.paho.client.mqttv3.MqttClient in project Applozic-Android-SDK by AppLozic.
the class ApplozicMqttService method subscribeToTypingTopic.
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();
}
}
use of org.eclipse.paho.client.mqttv3.MqttClient in project iaf by ibissource.
the class MqttFacade method configure.
public void configure() throws ConfigurationException {
if (StringUtils.isEmpty(getClientId())) {
throw new ConfigurationException("clientId must be specified");
}
if (StringUtils.isEmpty(getBrokerUrl())) {
throw new ConfigurationException("brokerUrl must be specified");
}
if (StringUtils.isEmpty(getTopic())) {
throw new ConfigurationException("topic must be specified");
}
if (StringUtils.isEmpty(getPersistenceDirectory())) {
throw new ConfigurationException("persistenceDirectory must be specified");
}
connectOptions = new MqttConnectOptions();
connectOptions.setCleanSession(isCleanSession());
connectOptions.setAutomaticReconnect(isAutomaticReconnect());
connectOptions.setConnectionTimeout(getTimeout());
connectOptions.setKeepAliveInterval(getKeepAliveInterval());
// Default: 0, V3.1: 3, V3.1.1: 4
connectOptions.setMqttVersion(MqttConnectOptions.MQTT_VERSION_DEFAULT);
if (!StringUtils.isEmpty(getAuthAlias()) || (!StringUtils.isEmpty(getUsername()) && !StringUtils.isEmpty(getPassword()))) {
CredentialFactory credentialFactory = new CredentialFactory(getAuthAlias(), getUsername(), getPassword());
connectOptions.setUserName(credentialFactory.getUsername());
connectOptions.setPassword(credentialFactory.getPassword().toCharArray());
}
MqttDefaultFilePersistence dataStore = new MqttDefaultFilePersistence(getPersistenceDirectory());
try {
client = new MqttClient(brokerUrl, clientId, dataStore);
} catch (MqttException e) {
throw new ConfigurationException("Could not create client", e);
}
}
use of org.eclipse.paho.client.mqttv3.MqttClient in project kafka-connect-mqtt by jcustenborder.
the class MqttSinkTask method start.
@Override
public void start(Map<String, String> settings) {
this.config = new MqttSinkConnectorConfig(settings);
this.converter = new SinkConverter(this.config);
try {
this.client = new MqttClient(this.config.serverUri.get(0), MqttClient.generateClientId());
} catch (MqttException e) {
throw new ConnectException(e);
}
try {
log.info("Connecting to Mqtt Server.");
this.client.connect(this.config.connectOptions());
} catch (MqttException e) {
throw new ConnectException(e);
}
}
use of org.eclipse.paho.client.mqttv3.MqttClient in project wildfly-camel by wildfly-extras.
the class PahoIntegrationTest method testPahoConsumer.
@Test
public void testPahoConsumer() throws Exception {
String conUrl = TestUtils.getResourceValue(getClass(), "/tcp-connection");
CamelContext camelctx = new DefaultCamelContext();
camelctx.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("paho:" + BrokerSetup.TEST_TOPIC + "?brokerUrl=" + conUrl).transform(body().prepend("Hello ")).to("seda:end");
}
});
camelctx.start();
try {
PollingConsumer consumer = camelctx.getEndpoint("seda:end").createPollingConsumer();
consumer.start();
MqttClient client = null;
try {
client = new MqttClient(conUrl, "MqttClient", new MemoryPersistence());
MqttConnectOptions opts = new MqttConnectOptions();
opts.setCleanSession(true);
client.connect(opts);
MqttMessage message = new MqttMessage("Kermit".getBytes());
message.setQos(2);
client.publish(BrokerSetup.TEST_TOPIC, message);
} finally {
client.disconnect();
}
String result = consumer.receive(3000).getIn().getBody(String.class);
Assert.assertEquals("Hello Kermit", result);
} finally {
camelctx.stop();
}
}
use of org.eclipse.paho.client.mqttv3.MqttClient in project wildfly-camel by wildfly-extras.
the class PahoIntegrationTest method testMQTTProducer.
@Test
public void testMQTTProducer() throws Exception {
String conUrl = TestUtils.getResourceValue(getClass(), "/tcp-connection");
CamelContext camelctx = new DefaultCamelContext();
camelctx.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("direct:start").transform(body().prepend("Hello ")).to("paho:" + BrokerSetup.TEST_TOPIC + "?brokerUrl=" + conUrl);
}
});
camelctx.start();
try {
MqttClient client = new MqttClient(conUrl, "MqttClient", new MemoryPersistence());
MqttConnectOptions opts = new MqttConnectOptions();
opts.setCleanSession(true);
client.connect(opts);
client.subscribe(BrokerSetup.TEST_TOPIC, 2);
final List<String> result = new ArrayList<>();
final CountDownLatch latch = new CountDownLatch(1);
client.setCallback(new MqttCallback() {
@Override
public void connectionLost(Throwable cause) {
}
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
result.add(new String(message.getPayload()));
latch.countDown();
}
@Override
public void deliveryComplete(IMqttDeliveryToken token) {
}
});
ProducerTemplate producer = camelctx.createProducerTemplate();
producer.asyncSendBody("direct:start", "Kermit");
Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
Assert.assertEquals("One message", 1, result.size());
Assert.assertEquals("Hello Kermit", result.get(0));
} finally {
camelctx.stop();
}
}
Aggregations