Search in sources :

Example 1 with MqttException

use of org.eclipse.paho.client.mqttv3.MqttException in project openhab1-addons by openhab.

the class MqttBrokerConnection method connectionLost.

@Override
public synchronized void connectionLost(Throwable t) {
    logger.error("MQTT connection to broker was lost", t);
    if (t instanceof MqttException) {
        MqttException e = (MqttException) t;
        logger.error("MQTT connection to '{}' was lost: {} : ReasonCode {} : Cause : {}", new Object[] { name, e.getMessage(), e.getReasonCode(), (e.getCause() == null ? "Unknown" : e.getCause().getMessage()) });
    } else {
        logger.error("MQTT connection to '{}' was lost: {}", name, t.getMessage());
    }
    started = false;
    logger.info("Starting connection helper to periodically try restore connection to broker '{}'", name);
    MqttBrokerConnectionHelper helper = new MqttBrokerConnectionHelper(this);
    reconnectTimer = new Timer(true);
    reconnectTimer.schedule(helper, 10000, RECONNECT_FREQUENCY);
}
Also used : Timer(java.util.Timer) MqttException(org.eclipse.paho.client.mqttv3.MqttException)

Example 2 with MqttException

use of org.eclipse.paho.client.mqttv3.MqttException in project ignite by apache.

the class IgniteMqttStreamerTest method sendMessages.

/**
     * @param topics Topics.
     * @param fromIdx From index.
     * @param cnt Count.
     * @param singleMsg Single message flag.
     * @throws MqttException If failed.
     */
private void sendMessages(final List<String> topics, int fromIdx, int cnt, boolean singleMsg) throws MqttException {
    if (singleMsg) {
        final List<StringBuilder> sbs = new ArrayList<>(topics.size());
        // initialize String Builders for each topic
        F.forEach(topics, new IgniteInClosure<String>() {

            @Override
            public void apply(String s) {
                sbs.add(new StringBuilder());
            }
        });
        // fill String Builders for each topic
        F.forEach(F.range(fromIdx, fromIdx + cnt), new IgniteInClosure<Integer>() {

            @Override
            public void apply(Integer integer) {
                sbs.get(integer % topics.size()).append(integer.toString() + "," + TEST_DATA.get(integer) + "\n");
            }
        });
        // send each buffer out
        for (int i = 0; i < topics.size(); i++) {
            MqttMessage msg = new MqttMessage(sbs.get(i).toString().getBytes());
            client.publish(topics.get(i % topics.size()), msg);
        }
    } else {
        for (int i = fromIdx; i < fromIdx + cnt; i++) {
            byte[] payload = (i + "," + TEST_DATA.get(i)).getBytes();
            MqttMessage msg = new MqttMessage(payload);
            client.publish(topics.get(i % topics.size()), msg);
        }
    }
}
Also used : MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) ArrayList(java.util.ArrayList)

Example 3 with MqttException

use of org.eclipse.paho.client.mqttv3.MqttException in project ha-bridge by bwssytems.

the class MQTTHandler method publishMessage.

public void publishMessage(String topic, String content) {
    MqttMessage message = new MqttMessage(StringEscapeUtils.unescapeJava(content).getBytes());
    message.setQos(qos);
    try {
        myClient.publish(topic, message);
    } catch (MqttPersistenceException e) {
        log.error("Could not publish to MQTT client for name: " + myConfig.getName() + " and ip: " + myConfig.getIp() + " with message: " + e.getMessage());
    } catch (MqttException e) {
        log.error("Could not publish to MQTT client for name: " + myConfig.getName() + " and ip: " + myConfig.getIp() + " with message: " + e.getMessage());
    }
}
Also used : MqttMessage(org.eclipse.paho.client.mqttv3.MqttMessage) MqttException(org.eclipse.paho.client.mqttv3.MqttException) MqttPersistenceException(org.eclipse.paho.client.mqttv3.MqttPersistenceException)

Example 4 with MqttException

use of org.eclipse.paho.client.mqttv3.MqttException in project azure-iot-sdk-java by Azure.

the class MqttTest method constructorThrowsExceptionIfMqttAsyncClientFails.

/*
    **Tests_SRS_Mqtt_25_045: [**The constructor throws IOException if MqttException is thrown and doesn't instantiate this instance.**]**
     */
@Test(expected = IOException.class)
public void constructorThrowsExceptionIfMqttAsyncClientFails() throws IOException, MqttException {
    Mqtt mockMqtt = null;
    try {
        //arrange
        new NonStrictExpectations() {

            {
                new MemoryPersistence();
                result = mockMemoryPersistence;
                new MqttAsyncClient(serverUri, clientId, mockMemoryPersistence);
                result = mockMqttException;
            }
        };
        //act
        mockMqtt = instantiateMqtt(true);
    } finally {
        testCleanUp(mockMqtt);
    }
}
Also used : Mqtt(com.microsoft.azure.sdk.iot.device.transport.mqtt.Mqtt) MemoryPersistence(org.eclipse.paho.client.mqttv3.persist.MemoryPersistence) Test(org.junit.Test)

Example 5 with MqttException

use of org.eclipse.paho.client.mqttv3.MqttException in project azure-iot-sdk-java by Azure.

the class MqttTest method connectionLostAttemptsToReconnect.

/*
    **Tests_SRS_Mqtt_25_026: [**The function shall notify all its concrete classes by calling abstract method onReconnect at the entry of the function**]**
     */
/*
    **Tests_SRS_Mqtt_25_029: [**The function shall notify all its concrete classes by calling abstract method onReconnectComplete at the exit of the function**]**
     */
@Test
public void connectionLostAttemptsToReconnect() throws IOException, MqttException {
    //arrange
    Mqtt mockMqtt = null;
    Throwable t = new Throwable();
    try {
        new StrictExpectations() {

            {
                new MemoryPersistence();
                result = mockMemoryPersistence;
                new MqttAsyncClient(serverUri, clientId, mockMemoryPersistence);
                result = mockMqttAsyncClient;
                mockMqttAsyncClient.setCallback((Mqtt) any);
                new MqttConnectOptions();
                result = mockMqttConnectionOptions;
                mockMqttConnectionOptions.setKeepAliveInterval(anyInt);
                mockMqttConnectionOptions.setCleanSession(anyBoolean);
                mockMqttConnectionOptions.setMqttVersion(anyInt);
                mockMqttConnectionOptions.setUserName(anyString);
                mockMqttConnectionOptions.setPassword(password.toCharArray());
                mockMqttConnectionOptions.setSocketFactory(mockIotHubSSLContext.getIotHubSSlContext().getSocketFactory());
                mockMqttAsyncClient.isConnected();
                result = false;
                mockMqttAsyncClient.isConnected();
                result = false;
                mockMqttAsyncClient.connect(mockMqttConnectionOptions);
                result = mockMqttToken;
                mockMqttToken.waitForCompletion();
                mockMqttAsyncClient.isConnected();
                result = true;
            }
        };
        //act
        try {
            mockMqtt = instantiateMqtt(true);
            mockMqtt.connectionLost(t);
        } catch (Exception e) {
            System.out.print("Completed throwing exception - " + e.getCause() + e.getMessage());
        }
    } finally {
        testCleanUp(mockMqtt);
    }
}
Also used : Mqtt(com.microsoft.azure.sdk.iot.device.transport.mqtt.Mqtt) MemoryPersistence(org.eclipse.paho.client.mqttv3.persist.MemoryPersistence) IOException(java.io.IOException) InvalidParameterException(java.security.InvalidParameterException) Test(org.junit.Test)

Aggregations

Mqtt (com.microsoft.azure.sdk.iot.device.transport.mqtt.Mqtt)3 MqttException (org.eclipse.paho.client.mqttv3.MqttException)3 MemoryPersistence (org.eclipse.paho.client.mqttv3.persist.MemoryPersistence)3 Test (org.junit.Test)3 IOException (java.io.IOException)2 InvalidParameterException (java.security.InvalidParameterException)2 MqttMessage (org.eclipse.paho.client.mqttv3.MqttMessage)2 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 Timer (java.util.Timer)1 DisconnectedBufferOptions (org.eclipse.paho.client.mqttv3.DisconnectedBufferOptions)1 IMqttActionListener (org.eclipse.paho.client.mqttv3.IMqttActionListener)1 IMqttToken (org.eclipse.paho.client.mqttv3.IMqttToken)1 MqttConnectOptions (org.eclipse.paho.client.mqttv3.MqttConnectOptions)1 MqttPersistenceException (org.eclipse.paho.client.mqttv3.MqttPersistenceException)1