Search in sources :

Example 61 with ProtonConnection

use of io.vertx.proton.ProtonConnection in project vertx-proton by vert-x3.

the class Sender method main.

public static void main(String[] args) throws Exception {
    try {
        Vertx vertx = Vertx.vertx();
        ProtonClient client = ProtonClient.create(vertx);
        // Connect, then use the event loop thread to process the connection
        client.connect("localhost", 5672, res -> {
            Context ctx = Vertx.currentContext();
            if (res.succeeded()) {
                System.out.println("We're connected");
                ProtonConnection connection = res.result();
                connection.open();
                Subscriber<Message> producerSubscriber = ProtonStreams.createProducer(connection, "queue");
                Publisher<Message> publisher = Flowable.range(1, COUNT).map(i -> {
                    Message m = Proton.message();
                    m.setBody(new AmqpValue("Hello " + i));
                    return m;
                }).doFinally(() -> {
                    ctx.runOnContext(x -> {
                        System.out.println("Publisher finished, closing connection.");
                        connection.closeHandler(y -> {
                            System.out.println("Connection closed.");
                            connection.disconnect();
                            vertx.close();
                        }).close();
                    });
                });
                publisher.subscribe(producerSubscriber);
            } else {
                System.out.println("Failed to connect, exiting: " + res.cause());
                System.exit(1);
            }
        });
    } catch (Exception exp) {
        System.out.println("Caught exception, exiting.");
        exp.printStackTrace(System.out);
        System.exit(1);
    }
}
Also used : Context(io.vertx.core.Context) Proton(org.apache.qpid.proton.Proton) ProtonConnection(io.vertx.proton.ProtonConnection) ProtonStreams(io.vertx.proton.streams.ProtonStreams) Flowable(io.reactivex.Flowable) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue) Publisher(org.reactivestreams.Publisher) Vertx(io.vertx.core.Vertx) Message(org.apache.qpid.proton.message.Message) ProtonClient(io.vertx.proton.ProtonClient) Subscriber(org.reactivestreams.Subscriber) Context(io.vertx.core.Context) ProtonConnection(io.vertx.proton.ProtonConnection) Message(org.apache.qpid.proton.message.Message) Vertx(io.vertx.core.Vertx) ProtonClient(io.vertx.proton.ProtonClient) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue)

Example 62 with ProtonConnection

use of io.vertx.proton.ProtonConnection in project vertx-proton by vert-x3.

the class MessagePublisherVerificationTckTest method createPublisher.

@Override
public Publisher<Message> createPublisher(long elements) {
    int actualPort = server.actualPort();
    ProtonClient client = ProtonClient.create(vertx);
    AtomicReference<Publisher<Message>> ref = new AtomicReference<>();
    CountDownLatch latch = new CountDownLatch(1);
    client.connect("localhost", actualPort, result -> {
        if (result.succeeded()) {
            ProtonConnection conn = result.result();
            conn.open();
            ProtonPublisher<Message> stream = ProtonStreams.createConsumer(conn, testName);
            ((ProtonPublisherWrapperImpl) stream).setEmitOnConnectionEnd(false);
            ref.set(stream);
        } else {
            LOG.error("Connection failed");
        }
        latch.countDown();
    });
    try {
        LOG.trace("Awaiting connection");
        boolean res = latch.await(2, TimeUnit.SECONDS);
        LOG.trace("Client connected: " + res);
    } catch (InterruptedException e) {
        throw new RuntimeException("Interrupted while creating publisher", e);
    }
    return ref.get();
}
Also used : Message(org.apache.qpid.proton.message.Message) AtomicReference(java.util.concurrent.atomic.AtomicReference) Publisher(org.reactivestreams.Publisher) ProtonPublisher(io.vertx.proton.streams.ProtonPublisher) ProtonClient(io.vertx.proton.ProtonClient) CountDownLatch(java.util.concurrent.CountDownLatch) ProtonConnection(io.vertx.proton.ProtonConnection) ProtonPublisherWrapperImpl(io.vertx.proton.streams.impl.ProtonPublisherWrapperImpl)

Example 63 with ProtonConnection

use of io.vertx.proton.ProtonConnection in project vertx-proton by vert-x3.

the class VertxProtonExamples method example1.

public void example1(Vertx vertx) {
    ProtonClient client = ProtonClient.create(vertx);
    // Connect, then use the event loop thread to process things thereafter
    client.connect("hostname", 5672, "username", "password", connectResult -> {
        if (connectResult.succeeded()) {
            connectResult.result().setContainer("my-container/client-id").openHandler(openResult -> {
                if (openResult.succeeded()) {
                    ProtonConnection conn = openResult.result();
                // Create senders, receivers etc..
                }
            }).open();
        }
    });
}
Also used : ProtonConnection(io.vertx.proton.ProtonConnection) Section(org.apache.qpid.proton.amqp.messaging.Section) Source(io.vertx.docgen.Source) ProtonHelper.message(io.vertx.proton.ProtonHelper.message) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue) Vertx(io.vertx.core.Vertx) ProtonSender(io.vertx.proton.ProtonSender) Message(org.apache.qpid.proton.message.Message) ProtonClient(io.vertx.proton.ProtonClient) Context(io.vertx.core.Context) ProtonConnection(io.vertx.proton.ProtonConnection) ProtonClient(io.vertx.proton.ProtonClient)

Aggregations

ProtonConnection (io.vertx.proton.ProtonConnection)63 ProtonClient (io.vertx.proton.ProtonClient)37 Message (org.apache.qpid.proton.message.Message)36 Handler (io.vertx.core.Handler)35 Test (org.junit.Test)33 Async (io.vertx.ext.unit.Async)27 ProtonServer (io.vertx.proton.ProtonServer)25 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)25 AmqpValue (org.apache.qpid.proton.amqp.messaging.AmqpValue)25 AsyncResult (io.vertx.core.AsyncResult)24 TestContext (io.vertx.ext.unit.TestContext)24 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)24 RunWith (org.junit.runner.RunWith)24 Section (org.apache.qpid.proton.amqp.messaging.Section)23 ProtonHelper.message (io.vertx.proton.ProtonHelper.message)21 Rejected (org.apache.qpid.proton.amqp.messaging.Rejected)21 ProtonStreams (io.vertx.proton.streams.ProtonStreams)20 Vertx (io.vertx.core.Vertx)19 Symbol (org.apache.qpid.proton.amqp.Symbol)19 Accepted (org.apache.qpid.proton.amqp.messaging.Accepted)19