Search in sources :

Example 16 with ProtonConnection

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

the class ProtonSubscriberIntTest method testCreateAnonymousRelaySubscriber.

@Test(timeout = 20000)
public void testCreateAnonymousRelaySubscriber(TestContext context) throws Exception {
    server.close();
    final Async serverLinkOpenAsync = context.async();
    final Async serverReceivedMessageAsync = context.async();
    final Async serverLinkCloseAsync = context.async();
    final String address1 = "testCreateAnonymousRelaySubscriber1";
    final String address2 = "testCreateAnonymousRelaySubscriber2";
    final AtomicInteger msgCounter = new AtomicInteger(0);
    final AtomicInteger acceptedMsgCounter = new AtomicInteger(0);
    ProtonServer protonServer = null;
    try {
        protonServer = createServer((serverConnection) -> {
            serverConnection.openHandler(result -> {
                serverConnection.open();
            });
            serverConnection.sessionOpenHandler(session -> session.open());
            serverConnection.receiverOpenHandler(serverReceiver -> {
                LOG.trace("Server receiver opened");
                context.assertFalse(serverLinkOpenAsync.isCompleted(), "should only be one link opened");
                context.assertNull(serverReceiver.getRemoteTarget().getAddress(), "link target should have null address for anonymous relay");
                serverReceiver.handler((delivery, msg) -> {
                    int count = msgCounter.incrementAndGet();
                    if (LOG.isTraceEnabled()) {
                        LOG.trace("Server got msg: " + getMessageBody(context, msg));
                    }
                    switch(count) {
                        case 1:
                            {
                                validateMessage(context, 1, "1", msg);
                                context.assertEquals(address1, msg.getAddress(), "Unexpected message1 'to' address");
                                break;
                            }
                        case 2:
                            {
                                validateMessage(context, 2, "2", msg);
                                context.assertEquals(address2, msg.getAddress(), "Unexpected message2 'to' address");
                                // Complete the test
                                serverReceivedMessageAsync.complete();
                                break;
                            }
                    }
                });
                serverReceiver.closeHandler(x -> {
                    serverReceiver.close();
                    serverLinkCloseAsync.complete();
                });
                // Set the local terminus details [naively]
                serverReceiver.setTarget(serverReceiver.getRemoteTarget().copy());
                serverReceiver.open();
                serverLinkOpenAsync.complete();
            });
        });
        // ===== Client Handling =====
        ProtonClient client = ProtonClient.create(vertx);
        client.connect("localhost", protonServer.actualPort(), res -> {
            context.assertTrue(res.succeeded());
            ProtonConnection connection = res.result();
            connection.open();
            // Create subscriber without an address, to send to the anonymous relay
            ProtonSubscriber<Tracker> subscriber = ProtonStreams.createTrackerProducer(connection, null);
            Tracker envelope1 = Tracker.create(message(address1, "1"), env1 -> {
                context.assertTrue(env1.isAccepted(), "msg1 should be accepted");
                context.assertTrue(env1.isRemotelySettled(), "msg1 should be remotely settled");
                context.assertTrue(acceptedMsgCounter.compareAndSet(0, 1), "unexpected acceptedMsgCounter:" + acceptedMsgCounter);
            });
            Tracker envelope2 = Tracker.create(message(address2, "2"), env2 -> {
                context.assertTrue(env2.isAccepted(), "msg1 should be accepted");
                context.assertTrue(env2.isRemotelySettled(), "msg1 should be remotely settled");
                context.assertTrue(acceptedMsgCounter.compareAndSet(1, 2), "unexpected acceptedMsgCounter:" + acceptedMsgCounter);
            });
            Publisher<Tracker> producer = Flowable.just(envelope1, envelope2);
            producer.subscribe(subscriber);
        });
        serverLinkOpenAsync.awaitSuccess();
        serverReceivedMessageAsync.awaitSuccess();
        serverLinkCloseAsync.awaitSuccess();
    } finally {
        if (protonServer != null) {
            protonServer.close();
        }
    }
}
Also used : TestContext(io.vertx.ext.unit.TestContext) ProtonConnection(io.vertx.proton.ProtonConnection) Async(io.vertx.ext.unit.Async) Arrays(java.util.Arrays) ProtonStreams(io.vertx.proton.streams.ProtonStreams) LoggerFactory(io.vertx.core.impl.logging.LoggerFactory) Rejected(org.apache.qpid.proton.amqp.messaging.Rejected) RunWith(org.junit.runner.RunWith) ProtonServer(io.vertx.proton.ProtonServer) MockServerTestBase(io.vertx.proton.MockServerTestBase) Flowable(io.reactivex.Flowable) ProtonHelper.message(io.vertx.proton.ProtonHelper.message) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Symbol(org.apache.qpid.proton.amqp.Symbol) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue) Modified(org.apache.qpid.proton.amqp.messaging.Modified) Message(org.apache.qpid.proton.message.Message) AsyncResult(io.vertx.core.AsyncResult) Logger(io.vertx.core.impl.logging.Logger) Tracker(io.vertx.proton.streams.Tracker) Publisher(org.reactivestreams.Publisher) Test(org.junit.Test) ProtonClient(io.vertx.proton.ProtonClient) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Released(org.apache.qpid.proton.amqp.messaging.Released) ExecutionException(java.util.concurrent.ExecutionException) Section(org.apache.qpid.proton.amqp.messaging.Section) FutureHandler(io.vertx.proton.FutureHandler) ProtonSubscriber(io.vertx.proton.streams.ProtonSubscriber) Handler(io.vertx.core.Handler) Accepted(org.apache.qpid.proton.amqp.messaging.Accepted) ProtonConnection(io.vertx.proton.ProtonConnection) Tracker(io.vertx.proton.streams.Tracker) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ProtonServer(io.vertx.proton.ProtonServer) Async(io.vertx.ext.unit.Async) ProtonClient(io.vertx.proton.ProtonClient) Test(org.junit.Test)

Example 17 with ProtonConnection

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

the class ProtonSubscriberIntTest method testCreateUsingCustomTarget.

@Test(timeout = 20000)
public void testCreateUsingCustomTarget(TestContext context) throws Exception {
    server.close();
    final Async serverLinkOpenAsync = context.async();
    final Async serverReceivedMessageAsync = context.async();
    final Async serverLinkCloseAsync = context.async();
    final String address = "testCreateUsingCustomTarget";
    ProtonServer protonServer = null;
    try {
        protonServer = createServer((serverConnection) -> {
            serverConnection.openHandler(result -> {
                serverConnection.open();
            });
            serverConnection.sessionOpenHandler(session -> session.open());
            serverConnection.receiverOpenHandler(serverReceiver -> {
                serverReceiver.handler((delivery, msg) -> {
                    // We got the message that was sent, complete the test
                    if (LOG.isTraceEnabled()) {
                        LOG.trace("Server got msg: " + getMessageBody(context, msg));
                    }
                    validateMessage(context, 1, "1", msg);
                    serverReceivedMessageAsync.complete();
                });
                // Verify the remote terminus details used were as expected
                context.assertNotNull(serverReceiver.getRemoteTarget(), "target should not be null");
                org.apache.qpid.proton.amqp.messaging.Target remoteTarget = (org.apache.qpid.proton.amqp.messaging.Target) serverReceiver.getRemoteTarget();
                context.assertEquals(address, remoteTarget.getAddress(), "unexpected target address");
                Symbol[] capabilities = remoteTarget.getCapabilities();
                context.assertTrue(Arrays.equals(new Symbol[] { Symbol.valueOf("custom") }, capabilities), "Unexpected capabilities: " + Arrays.toString(capabilities));
                // Set the local terminus details
                serverReceiver.setTarget(remoteTarget.copy());
                serverReceiver.closeHandler(x -> {
                    serverReceiver.close();
                    serverLinkCloseAsync.complete();
                });
                LOG.trace("Server receiver opened");
                serverReceiver.open();
                serverLinkOpenAsync.complete();
            });
        });
        // ===== Client Handling =====
        ProtonClient client = ProtonClient.create(vertx);
        client.connect("localhost", protonServer.actualPort(), res -> {
            context.assertTrue(res.succeeded());
            ProtonConnection connection = res.result();
            connection.open();
            // Create subscriber with custom target configured to be dynamic
            ProtonSubscriber<Tracker> subscriber = ProtonStreams.createTrackerProducer(connection, address);
            org.apache.qpid.proton.amqp.messaging.Target target = (org.apache.qpid.proton.amqp.messaging.Target) subscriber.getTarget();
            target.setCapabilities(new Symbol[] { Symbol.valueOf("custom") });
            Tracker envelope = Tracker.create(message("1"));
            Publisher<Tracker> publisher = Flowable.just(envelope);
            publisher.subscribe(subscriber);
        });
        serverLinkOpenAsync.awaitSuccess();
        serverReceivedMessageAsync.awaitSuccess();
        serverLinkCloseAsync.awaitSuccess();
    } finally {
        if (protonServer != null) {
            protonServer.close();
        }
    }
}
Also used : TestContext(io.vertx.ext.unit.TestContext) ProtonConnection(io.vertx.proton.ProtonConnection) Async(io.vertx.ext.unit.Async) Arrays(java.util.Arrays) ProtonStreams(io.vertx.proton.streams.ProtonStreams) LoggerFactory(io.vertx.core.impl.logging.LoggerFactory) Rejected(org.apache.qpid.proton.amqp.messaging.Rejected) RunWith(org.junit.runner.RunWith) ProtonServer(io.vertx.proton.ProtonServer) MockServerTestBase(io.vertx.proton.MockServerTestBase) Flowable(io.reactivex.Flowable) ProtonHelper.message(io.vertx.proton.ProtonHelper.message) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Symbol(org.apache.qpid.proton.amqp.Symbol) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue) Modified(org.apache.qpid.proton.amqp.messaging.Modified) Message(org.apache.qpid.proton.message.Message) AsyncResult(io.vertx.core.AsyncResult) Logger(io.vertx.core.impl.logging.Logger) Tracker(io.vertx.proton.streams.Tracker) Publisher(org.reactivestreams.Publisher) Test(org.junit.Test) ProtonClient(io.vertx.proton.ProtonClient) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Released(org.apache.qpid.proton.amqp.messaging.Released) ExecutionException(java.util.concurrent.ExecutionException) Section(org.apache.qpid.proton.amqp.messaging.Section) FutureHandler(io.vertx.proton.FutureHandler) ProtonSubscriber(io.vertx.proton.streams.ProtonSubscriber) Handler(io.vertx.core.Handler) Accepted(org.apache.qpid.proton.amqp.messaging.Accepted) Tracker(io.vertx.proton.streams.Tracker) ProtonServer(io.vertx.proton.ProtonServer) ProtonClient(io.vertx.proton.ProtonClient) ProtonConnection(io.vertx.proton.ProtonConnection) Async(io.vertx.ext.unit.Async) Test(org.junit.Test)

Example 18 with ProtonConnection

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

the class ProtonSubscriberIntTest method testConfigureProducerLinkName.

@Test(timeout = 20000)
public void testConfigureProducerLinkName(TestContext context) throws Exception {
    server.close();
    final Async serverLinkOpenAsync = context.async();
    final Async serverReceivedMessageAsync = context.async();
    final Async serverLinkCloseAsync = context.async();
    final String linkName = "testConfigureProducerLinkName";
    ProtonServer protonServer = null;
    try {
        protonServer = createServer((serverConnection) -> {
            serverConnection.openHandler(result -> {
                serverConnection.open();
            });
            serverConnection.sessionOpenHandler(session -> session.open());
            serverConnection.receiverOpenHandler(serverReceiver -> {
                LOG.trace("Server receiver opened");
                // Verify the link details used were as expected
                context.assertEquals(linkName, serverReceiver.getName(), "unexpected link name");
                serverReceiver.handler((delivery, msg) -> {
                    // We got the message that was sent, complete the test
                    if (LOG.isTraceEnabled()) {
                        LOG.trace("Server got msg: " + getMessageBody(context, msg));
                    }
                    validateMessage(context, 1, "1", msg);
                    serverReceivedMessageAsync.complete();
                });
                serverReceiver.closeHandler(x -> {
                    serverReceiver.close();
                    serverLinkCloseAsync.complete();
                });
                // Set the local terminus details [naively]
                serverReceiver.setTarget(serverReceiver.getRemoteTarget().copy());
                serverReceiver.open();
                serverLinkOpenAsync.complete();
            });
        });
        // ===== Client Handling =====
        ProtonClient client = ProtonClient.create(vertx);
        client.connect("localhost", protonServer.actualPort(), res -> {
            context.assertTrue(res.succeeded());
            ProtonConnection connection = res.result();
            connection.open();
            // Create subscriber with given link name
            ProtonSubscriberOptions options = new ProtonSubscriberOptions().setLinkName(linkName);
            ProtonSubscriber<Tracker> subscriber = ProtonStreams.createTrackerProducer(connection, "myAddress", options);
            Tracker envelope = Tracker.create(message("1"));
            Publisher<Tracker> producer = Flowable.just(envelope);
            producer.subscribe(subscriber);
        });
        serverLinkOpenAsync.awaitSuccess();
        serverReceivedMessageAsync.awaitSuccess();
        serverLinkCloseAsync.awaitSuccess();
    } finally {
        if (protonServer != null) {
            protonServer.close();
        }
    }
}
Also used : TestContext(io.vertx.ext.unit.TestContext) ProtonConnection(io.vertx.proton.ProtonConnection) Async(io.vertx.ext.unit.Async) Arrays(java.util.Arrays) ProtonStreams(io.vertx.proton.streams.ProtonStreams) LoggerFactory(io.vertx.core.impl.logging.LoggerFactory) Rejected(org.apache.qpid.proton.amqp.messaging.Rejected) RunWith(org.junit.runner.RunWith) ProtonServer(io.vertx.proton.ProtonServer) MockServerTestBase(io.vertx.proton.MockServerTestBase) Flowable(io.reactivex.Flowable) ProtonHelper.message(io.vertx.proton.ProtonHelper.message) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Symbol(org.apache.qpid.proton.amqp.Symbol) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue) Modified(org.apache.qpid.proton.amqp.messaging.Modified) Message(org.apache.qpid.proton.message.Message) AsyncResult(io.vertx.core.AsyncResult) Logger(io.vertx.core.impl.logging.Logger) Tracker(io.vertx.proton.streams.Tracker) Publisher(org.reactivestreams.Publisher) Test(org.junit.Test) ProtonClient(io.vertx.proton.ProtonClient) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Released(org.apache.qpid.proton.amqp.messaging.Released) ExecutionException(java.util.concurrent.ExecutionException) Section(org.apache.qpid.proton.amqp.messaging.Section) FutureHandler(io.vertx.proton.FutureHandler) ProtonSubscriber(io.vertx.proton.streams.ProtonSubscriber) Handler(io.vertx.core.Handler) Accepted(org.apache.qpid.proton.amqp.messaging.Accepted) ProtonConnection(io.vertx.proton.ProtonConnection) Tracker(io.vertx.proton.streams.Tracker) ProtonServer(io.vertx.proton.ProtonServer) Async(io.vertx.ext.unit.Async) ProtonClient(io.vertx.proton.ProtonClient) Test(org.junit.Test)

Example 19 with ProtonConnection

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

the class Receiver 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();
                Publisher<Message> consumerPublisher = ProtonStreams.createConsumer(connection, "queue");
                Subscriber<Message> subscriber = new Subscriber<Message>() {

                    AtomicInteger receievedCount = new AtomicInteger();

                    Subscription subscription;

                    @Override
                    public void onSubscribe(Subscription s) {
                        subscription = s;
                        // Initial request for some messages
                        subscription.request(REQ_WINDOW);
                    }

                    @Override
                    public void onNext(Message env) {
                        int msgNum = receievedCount.incrementAndGet();
                        String content = (String) ((AmqpValue) env.getBody()).getValue();
                        System.out.println("Received message: " + content);
                        if (msgNum % REQ_WINDOW == 0 && msgNum < COUNT) {
                            // Request more messages
                            subscription.request(REQ_WINDOW);
                        } else if (msgNum == COUNT) {
                            // Done, clean up.
                            subscription.cancel();
                            closeConnection(vertx, connection);
                        }
                    }

                    @Override
                    public void onError(Throwable t) {
                        System.out.println("onError(): " + t);
                        closeConnection(vertx, connection);
                    }

                    @Override
                    public void onComplete() {
                        System.out.println("onComplete()");
                        closeConnection(vertx, connection);
                    }

                    private void closeConnection(Vertx vertx, ProtonConnection connection) {
                        ctx.runOnContext(x -> {
                            System.out.println("Subscriber finished, closing connection.");
                            connection.closeHandler(y -> {
                                System.out.println("Connection closed.");
                                connection.disconnect();
                                vertx.close();
                            }).close();
                        });
                    }
                };
                consumerPublisher.subscribe(subscriber);
            } 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) ProtonConnection(io.vertx.proton.ProtonConnection) ProtonStreams(io.vertx.proton.streams.ProtonStreams) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue) Publisher(org.reactivestreams.Publisher) Subscription(org.reactivestreams.Subscription) 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) Message(org.apache.qpid.proton.message.Message) Vertx(io.vertx.core.Vertx) ProtonClient(io.vertx.proton.ProtonClient) ProtonConnection(io.vertx.proton.ProtonConnection) Subscriber(org.reactivestreams.Subscriber) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Subscription(org.reactivestreams.Subscription)

Example 20 with ProtonConnection

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

the class DeliveryPublisherVerificationTckTest method createPublisher.

@Override
public Publisher<Delivery> createPublisher(long elements) {
    int actualPort = server.actualPort();
    ProtonClient client = ProtonClient.create(vertx);
    AtomicReference<Publisher<Delivery>> ref = new AtomicReference<>();
    CountDownLatch latch = new CountDownLatch(1);
    client.connect("localhost", actualPort, result -> {
        if (result.succeeded()) {
            ProtonConnection conn = result.result();
            conn.open();
            ProtonPublisher<Delivery> stream = ProtonStreams.createDeliveryConsumer(conn, testName);
            ((ProtonPublisherImpl) 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 : ProtonPublisherImpl(io.vertx.proton.streams.impl.ProtonPublisherImpl) 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) Delivery(io.vertx.proton.streams.Delivery)

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