Search in sources :

Example 21 with MqttClient

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();
    }
}
Also used : MqttClient(org.eclipse.paho.client.mqttv3.MqttClient) MobiComUserPreference(com.applozic.mobicomkit.api.account.user.MobiComUserPreference) MqttException(org.eclipse.paho.client.mqttv3.MqttException)

Example 22 with MqttClient

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);
    }
}
Also used : MqttClient(org.eclipse.paho.client.mqttv3.MqttClient) ConfigurationException(nl.nn.adapterframework.configuration.ConfigurationException) MqttConnectOptions(org.eclipse.paho.client.mqttv3.MqttConnectOptions) CredentialFactory(nl.nn.adapterframework.util.CredentialFactory) MqttException(org.eclipse.paho.client.mqttv3.MqttException) MqttDefaultFilePersistence(org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence)

Example 23 with MqttClient

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);
    }
}
Also used : IMqttClient(org.eclipse.paho.client.mqttv3.IMqttClient) MqttClient(org.eclipse.paho.client.mqttv3.MqttClient) MqttException(org.eclipse.paho.client.mqttv3.MqttException) ConnectException(org.apache.kafka.connect.errors.ConnectException)

Example 24 with MqttClient

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();
    }
}
Also used : DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) CamelContext(org.apache.camel.CamelContext) MqttClient(org.eclipse.paho.client.mqttv3.MqttClient) MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) PollingConsumer(org.apache.camel.PollingConsumer) RouteBuilder(org.apache.camel.builder.RouteBuilder) MemoryPersistence(org.eclipse.paho.client.mqttv3.persist.MemoryPersistence) MqttConnectOptions(org.eclipse.paho.client.mqttv3.MqttConnectOptions) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) Test(org.junit.Test)

Example 25 with MqttClient

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();
    }
}
Also used : DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) CamelContext(org.apache.camel.CamelContext) MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) ProducerTemplate(org.apache.camel.ProducerTemplate) RouteBuilder(org.apache.camel.builder.RouteBuilder) MemoryPersistence(org.eclipse.paho.client.mqttv3.persist.MemoryPersistence) ArrayList(java.util.ArrayList) MqttCallback(org.eclipse.paho.client.mqttv3.MqttCallback) CountDownLatch(java.util.concurrent.CountDownLatch) IMqttDeliveryToken(org.eclipse.paho.client.mqttv3.IMqttDeliveryToken) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) MqttClient(org.eclipse.paho.client.mqttv3.MqttClient) MqttConnectOptions(org.eclipse.paho.client.mqttv3.MqttConnectOptions) Test(org.junit.Test)

Aggregations

MqttClient (org.eclipse.paho.client.mqttv3.MqttClient)46 MqttException (org.eclipse.paho.client.mqttv3.MqttException)37 MqttConnectOptions (org.eclipse.paho.client.mqttv3.MqttConnectOptions)23 MemoryPersistence (org.eclipse.paho.client.mqttv3.persist.MemoryPersistence)23 MqttMessage (org.eclipse.paho.client.mqttv3.MqttMessage)18 Test (org.junit.Test)12 Properties (java.util.Properties)6 IMqttClient (org.eclipse.paho.client.mqttv3.IMqttClient)6 MqttDefaultFilePersistence (org.eclipse.paho.client.mqttv3.persist.MqttDefaultFilePersistence)5 IOException (java.io.IOException)4 IMqttDeliveryToken (org.eclipse.paho.client.mqttv3.IMqttDeliveryToken)4 MqttCallback (org.eclipse.paho.client.mqttv3.MqttCallback)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 JsonArray (com.google.gson.JsonArray)3 JsonElement (com.google.gson.JsonElement)3 JsonObject (com.google.gson.JsonObject)3 SSLSecurityManager (it.unibo.arces.wot.sepa.commons.protocol.SSLSecurityManager)3 KeyManagementException (java.security.KeyManagementException)3 KeyStoreException (java.security.KeyStoreException)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3