Search in sources :

Example 6 with MqttClientOptions

use of io.vertx.mqtt.MqttClientOptions in project vertx-examples by vert-x3.

the class Client method start.

@Override
public void start() throws Exception {
    MqttClientOptions options = new MqttClientOptions();
    options.setSsl(true);
    options.setTrustAll(true);
    MqttClient mqttClient = MqttClient.create(vertx, options);
    mqttClient.connect(BROKER_PORT, BROKER_HOST, ch -> {
        if (ch.succeeded()) {
            System.out.println("Connected to a server");
            mqttClient.publish(MQTT_TOPIC, Buffer.buffer(MQTT_MESSAGE), MqttQoS.AT_MOST_ONCE, false, false, s -> mqttClient.disconnect(d -> System.out.println("Disconnected from server")));
        } else {
            System.out.println("Failed to connect to a server");
            System.out.println(ch.cause());
        }
    });
}
Also used : MqttClient(io.vertx.mqtt.MqttClient) MqttClientOptions(io.vertx.mqtt.MqttClientOptions) MqttQoS(io.netty.handler.codec.mqtt.MqttQoS) Buffer(io.vertx.core.buffer.Buffer) MqttClient(io.vertx.mqtt.MqttClient) AbstractVerticle(io.vertx.core.AbstractVerticle) Runner(io.vertx.example.mqtt.util.Runner) MqttClientOptions(io.vertx.mqtt.MqttClientOptions)

Example 7 with MqttClientOptions

use of io.vertx.mqtt.MqttClientOptions in project hono by eclipse.

the class MqttTestBase method connectToAdapter.

/**
 * Opens a connection to the MQTT adapter using an X.509 client certificate.
 *
 * @param cert The client certificate to use for authentication.
 * @param hostname The name of the host to connect to.
 * @return A future that will be completed with the CONNACK packet received
 *         from the adapter or failed with a {@link io.vertx.mqtt.MqttConnectionException}
 *         if the connection could not be established.
 * @throws NullPointerException if any of the parameters are {@code null}.
 */
protected final Future<MqttConnAckMessage> connectToAdapter(final SelfSignedCertificate cert, final String hostname) {
    Objects.requireNonNull(cert);
    Objects.requireNonNull(hostname);
    final Promise<MqttConnAckMessage> result = Promise.promise();
    vertx.runOnContext(connect -> {
        final MqttClientOptions options = new MqttClientOptions(defaultOptions);
        options.setKeyCertOptions(cert.keyCertOptions());
        mqttClient = MqttClient.create(vertx, options);
        mqttClient.connect(IntegrationTestSupport.MQTTS_PORT, hostname, result);
    });
    return result.future().map(conAck -> {
        LOGGER.info("MQTTS connection to adapter [host: {}, port: {}] established", hostname, IntegrationTestSupport.MQTTS_PORT);
        this.context = Vertx.currentContext();
        return conAck;
    });
}
Also used : MqttClientOptions(io.vertx.mqtt.MqttClientOptions) MqttConnAckMessage(io.vertx.mqtt.messages.MqttConnAckMessage)

Example 8 with MqttClientOptions

use of io.vertx.mqtt.MqttClientOptions in project hono by eclipse.

the class MqttTestBase method connectToAdapter.

/**
 * Opens a connection to the MQTT adapter using given credentials.
 *
 * @param tlsVersion The TLS protocol version to use for connecting to the adapter.
 * @param username The username to use for authentication.
 * @param password The password to use for authentication.
 * @return A future that will be completed with the CONNACK packet received
 *         from the adapter or failed with a {@link io.vertx.mqtt.MqttConnectionException}
 *         if the connection could not be established.
 */
protected final Future<MqttConnAckMessage> connectToAdapter(final String tlsVersion, final String username, final String password) {
    final Promise<MqttConnAckMessage> result = Promise.promise();
    vertx.runOnContext(connect -> {
        final MqttClientOptions options = new MqttClientOptions(defaultOptions).setUsername(username).setPassword(password);
        options.setEnabledSecureTransportProtocols(Set.of(tlsVersion));
        mqttClient = MqttClient.create(vertx, options);
        mqttClient.connect(IntegrationTestSupport.MQTTS_PORT, IntegrationTestSupport.MQTT_HOST, result);
    });
    return result.future().map(conAck -> {
        LOGGER.info("MQTTS connection to adapter [host: {}, port: {}] established", IntegrationTestSupport.MQTT_HOST, IntegrationTestSupport.MQTTS_PORT);
        this.context = Vertx.currentContext();
        return conAck;
    });
}
Also used : MqttClientOptions(io.vertx.mqtt.MqttClientOptions) MqttConnAckMessage(io.vertx.mqtt.messages.MqttConnAckMessage)

Aggregations

MqttClientOptions (io.vertx.mqtt.MqttClientOptions)8 Buffer (io.vertx.core.buffer.Buffer)4 MqttClient (io.vertx.mqtt.MqttClient)4 MqttConnAckMessage (io.vertx.mqtt.messages.MqttConnAckMessage)4 MqttQoS (io.netty.handler.codec.mqtt.MqttQoS)3 AbstractVerticle (io.vertx.core.AbstractVerticle)2 AsyncResult (io.vertx.core.AsyncResult)2 Future (io.vertx.core.Future)2 Handler (io.vertx.core.Handler)2 Vertx (io.vertx.core.Vertx)2 PemTrustOptions (io.vertx.core.net.PemTrustOptions)2 Runner (io.vertx.example.mqtt.util.Runner)2 TestContext (io.vertx.ext.unit.TestContext)2 Consumer (java.util.function.Consumer)2 Message (org.apache.qpid.proton.message.Message)2 MessageConsumer (org.eclipse.hono.client.MessageConsumer)2 IntegrationTestSupport (org.eclipse.hono.tests.IntegrationTestSupport)2 TenantObject (org.eclipse.hono.util.TenantObject)2 Test (org.junit.Test)2 JsonObject (io.vertx.core.json.JsonObject)1