Search in sources :

Example 36 with Subscriber

use of org.reactivestreams.Subscriber in project vertx-proton by vert-x3.

the class MessageSubscriberBlackboxVerificationTckTest method createServer.

private TestServer createServer() throws Exception {
    return new TestServer(vertx, (connection) -> {
        connection.openHandler(res -> {
            LOG.trace("Client connected: " + connection.getRemoteContainer());
            connection.open();
        }).closeHandler(c -> {
            LOG.trace("Client closing amqp connection: " + connection.getRemoteContainer());
            connection.close();
            connection.disconnect();
        }).disconnectHandler(c -> {
            LOG.trace("Client socket disconnected: " + connection.getRemoteContainer());
            connection.disconnect();
        }).sessionOpenHandler(session -> session.open());
        connection.receiverOpenHandler(receiver -> {
            if (!server.getDetachLink()) {
                LOG.trace("Receiving from client to: " + receiver.getRemoteTarget().getAddress());
                // This is rather naive, for example use only, proper
                receiver.setTarget(receiver.getRemoteTarget());
                // servers should ensure that they advertise their own
                // Target settings that actually reflect what is in place.
                // The request may have also been for a dynamic address.
                receiver.handler((delivery, msg) -> {
                    String address = msg.getAddress();
                    if (address == null) {
                        address = receiver.getRemoteTarget().getAddress();
                    }
                    Section body = msg.getBody();
                    String content = "unknown";
                    if (body instanceof AmqpValue) {
                        content = (String) ((AmqpValue) body).getValue();
                    }
                    LOG.trace("message to:" + address + ", body: " + content);
                });
                receiver.closeHandler(s -> {
                    s.result().close();
                });
            }
            receiver.open();
            if (server.getDetachLink()) {
                receiver.setCondition(new ErrorCondition(Symbol.getSymbol("Failed Subscriber Requested"), ""));
                receiver.close();
            }
        });
    });
}
Also used : ProtonConnection(io.vertx.proton.ProtonConnection) ProtonStreams(io.vertx.proton.streams.ProtonStreams) LoggerFactory(io.vertx.core.impl.logging.LoggerFactory) MockServer(io.vertx.proton.MockServer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AfterMethod(org.testng.annotations.AfterMethod) AtomicReference(java.util.concurrent.atomic.AtomicReference) Symbol(org.apache.qpid.proton.amqp.Symbol) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue) Message(org.apache.qpid.proton.message.Message) Subscriber(org.reactivestreams.Subscriber) Logger(io.vertx.core.impl.logging.Logger) ProtonSubscriberWrapperImpl(io.vertx.proton.streams.impl.ProtonSubscriberWrapperImpl) BeforeMethod(org.testng.annotations.BeforeMethod) Vertx(io.vertx.core.Vertx) ProtonClient(io.vertx.proton.ProtonClient) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Proton(org.apache.qpid.proton.Proton) CountDownLatch(java.util.concurrent.CountDownLatch) ErrorCondition(org.apache.qpid.proton.amqp.transport.ErrorCondition) Section(org.apache.qpid.proton.amqp.messaging.Section) SubscriberBlackboxVerification(org.reactivestreams.tck.SubscriberBlackboxVerification) ProtonSubscriber(io.vertx.proton.streams.ProtonSubscriber) TestEnvironment(org.reactivestreams.tck.TestEnvironment) Handler(io.vertx.core.Handler) ErrorCondition(org.apache.qpid.proton.amqp.transport.ErrorCondition) Section(org.apache.qpid.proton.amqp.messaging.Section) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue)

Example 37 with Subscriber

use of org.reactivestreams.Subscriber in project vertx-proton by vert-x3.

the class MessageSubscriberBlackboxVerificationTckTest method createSubscriber.

@Override
public Subscriber<Message> createSubscriber() {
    int actualPort = server.actualPort();
    ProtonClient client = ProtonClient.create(vertx);
    AtomicReference<Subscriber<Message>> ref = new AtomicReference<>();
    CountDownLatch latch = new CountDownLatch(1);
    client.connect("localhost", actualPort, result -> {
        if (result.succeeded()) {
            ProtonConnection conn = result.result();
            conn.open();
            ProtonSubscriber<Message> stream = ProtonStreams.createProducer(conn, "myAddress");
            ((ProtonSubscriberWrapperImpl) 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 subscriber", e);
    }
    return ref.get();
}
Also used : Message(org.apache.qpid.proton.message.Message) AtomicReference(java.util.concurrent.atomic.AtomicReference) ProtonClient(io.vertx.proton.ProtonClient) CountDownLatch(java.util.concurrent.CountDownLatch) ProtonConnection(io.vertx.proton.ProtonConnection) Subscriber(org.reactivestreams.Subscriber) ProtonSubscriber(io.vertx.proton.streams.ProtonSubscriber) ProtonSubscriberWrapperImpl(io.vertx.proton.streams.impl.ProtonSubscriberWrapperImpl)

Example 38 with Subscriber

use of org.reactivestreams.Subscriber in project vertx-proton by vert-x3.

the class TrackerSubscriberBlackboxVerificationTckTest method createServer.

private TestServer createServer() throws Exception {
    return new TestServer(vertx, (connection) -> {
        connection.openHandler(res -> {
            LOG.trace("Client connected: " + connection.getRemoteContainer());
            connection.open();
        }).closeHandler(c -> {
            LOG.trace("Client closing amqp connection: " + connection.getRemoteContainer());
            connection.close();
            connection.disconnect();
        }).disconnectHandler(c -> {
            LOG.trace("Client socket disconnected: " + connection.getRemoteContainer());
            connection.disconnect();
        }).sessionOpenHandler(session -> session.open());
        connection.receiverOpenHandler(receiver -> {
            if (!server.getDetachLink()) {
                LOG.trace("Receiving from client to: " + receiver.getRemoteTarget().getAddress());
                // This is rather naive, for example use only, proper
                receiver.setTarget(receiver.getRemoteTarget());
                // servers should ensure that they advertise their own
                // Target settings that actually reflect what is in place.
                // The request may have also been for a dynamic address.
                receiver.handler((delivery, msg) -> {
                    String address = msg.getAddress();
                    if (address == null) {
                        address = receiver.getRemoteTarget().getAddress();
                    }
                    Section body = msg.getBody();
                    String content = "unknown";
                    if (body instanceof AmqpValue) {
                        content = (String) ((AmqpValue) body).getValue();
                    }
                    LOG.trace("message to:" + address + ", body: " + content);
                });
                receiver.closeHandler(s -> {
                    s.result().close();
                });
            }
            receiver.open();
            if (server.getDetachLink()) {
                receiver.setCondition(new ErrorCondition(Symbol.getSymbol("Failed Subscriber Requested"), ""));
                receiver.close();
            }
        });
    });
}
Also used : ProtonConnection(io.vertx.proton.ProtonConnection) ProtonStreams(io.vertx.proton.streams.ProtonStreams) LoggerFactory(io.vertx.core.impl.logging.LoggerFactory) ProtonSubscriberImpl(io.vertx.proton.streams.impl.ProtonSubscriberImpl) MockServer(io.vertx.proton.MockServer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AfterMethod(org.testng.annotations.AfterMethod) AtomicReference(java.util.concurrent.atomic.AtomicReference) Symbol(org.apache.qpid.proton.amqp.Symbol) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue) Message(org.apache.qpid.proton.message.Message) Subscriber(org.reactivestreams.Subscriber) Logger(io.vertx.core.impl.logging.Logger) Tracker(io.vertx.proton.streams.Tracker) BeforeMethod(org.testng.annotations.BeforeMethod) Vertx(io.vertx.core.Vertx) ProtonClient(io.vertx.proton.ProtonClient) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Proton(org.apache.qpid.proton.Proton) CountDownLatch(java.util.concurrent.CountDownLatch) ErrorCondition(org.apache.qpid.proton.amqp.transport.ErrorCondition) Section(org.apache.qpid.proton.amqp.messaging.Section) SubscriberBlackboxVerification(org.reactivestreams.tck.SubscriberBlackboxVerification) ProtonSubscriber(io.vertx.proton.streams.ProtonSubscriber) TestEnvironment(org.reactivestreams.tck.TestEnvironment) Handler(io.vertx.core.Handler) ErrorCondition(org.apache.qpid.proton.amqp.transport.ErrorCondition) Section(org.apache.qpid.proton.amqp.messaging.Section) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue)

Example 39 with Subscriber

use of org.reactivestreams.Subscriber in project vertx-proton by vert-x3.

the class ProtonPublisherIntTest method testCreateUsingCustomSource.

@Test(timeout = 20000)
public void testCreateUsingCustomSource(TestContext context) throws Exception {
    server.close();
    final Async clientLinkOpenAsync = context.async();
    final Async serverLinkOpenAsync = context.async();
    final Async clientLinkCloseAsync = context.async();
    final String dynamicAddress = "testCreateUsingCustomSource:" + UUID.randomUUID();
    ProtonServer protonServer = null;
    try {
        protonServer = createServer((serverConnection) -> {
            serverConnection.openHandler(result -> {
                serverConnection.open();
            });
            serverConnection.sessionOpenHandler(session -> session.open());
            serverConnection.senderOpenHandler(serverSender -> {
                serverSender.closeHandler(res -> {
                    serverSender.close();
                });
                // Verify the remote terminus details used were as expected
                context.assertNotNull(serverSender.getRemoteSource(), "source should not be null");
                org.apache.qpid.proton.amqp.messaging.Source remoteSource = (org.apache.qpid.proton.amqp.messaging.Source) serverSender.getRemoteSource();
                context.assertTrue(remoteSource.getDynamic(), "expected dynamic source");
                context.assertNull(remoteSource.getAddress(), "expected no source address");
                // Set the local terminus details
                org.apache.qpid.proton.amqp.messaging.Source source = (org.apache.qpid.proton.amqp.messaging.Source) remoteSource.copy();
                source.setAddress(dynamicAddress);
                serverSender.setSource(source);
                LOG.trace("Server sender opened");
                serverSender.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 publisher with source configured to be dynamic
            ProtonPublisher<Delivery> publisher = ProtonStreams.createDeliveryConsumer(connection, null);
            org.apache.qpid.proton.amqp.messaging.Source source = (org.apache.qpid.proton.amqp.messaging.Source) publisher.getSource();
            source.setDynamic(true);
            publisher.subscribe(new Subscriber<Delivery>() {

                Subscription sub = null;

                @Override
                public void onSubscribe(Subscription s) {
                    // Grab and verify the dynamic address details
                    org.apache.qpid.proton.amqp.messaging.Source remoteSource = (org.apache.qpid.proton.amqp.messaging.Source) publisher.getRemoteSource();
                    context.assertTrue(remoteSource.getDynamic(), "expected dynamic source");
                    context.assertEquals(dynamicAddress, remoteSource.getAddress(), "unexpected source address");
                    clientLinkOpenAsync.complete();
                    sub = s;
                    s.request(1);
                    sub.cancel();
                }

                @Override
                public void onNext(Delivery e) {
                }

                @Override
                public void onError(Throwable t) {
                    if (!clientLinkCloseAsync.isCompleted()) {
                        context.fail("onError called");
                    }
                }

                @Override
                public void onComplete() {
                    clientLinkCloseAsync.complete();
                }
            });
        });
        serverLinkOpenAsync.awaitSuccess();
        clientLinkOpenAsync.awaitSuccess();
        clientLinkCloseAsync.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) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ProtonServer(io.vertx.proton.ProtonServer) ArrayList(java.util.ArrayList) MockServerTestBase(io.vertx.proton.MockServerTestBase) Delivery(io.vertx.proton.streams.Delivery) 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) DeliveryState(org.apache.qpid.proton.amqp.transport.DeliveryState) Message(org.apache.qpid.proton.message.Message) AsyncResult(io.vertx.core.AsyncResult) Subscriber(org.reactivestreams.Subscriber) Logger(io.vertx.core.impl.logging.Logger) Test(org.junit.Test) ProtonClient(io.vertx.proton.ProtonClient) UUID(java.util.UUID) TerminusExpiryPolicy(org.apache.qpid.proton.amqp.messaging.TerminusExpiryPolicy) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Released(org.apache.qpid.proton.amqp.messaging.Released) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) Section(org.apache.qpid.proton.amqp.messaging.Section) FutureHandler(io.vertx.proton.FutureHandler) Subscription(org.reactivestreams.Subscription) TerminusDurability(org.apache.qpid.proton.amqp.messaging.TerminusDurability) ProtonPublisher(io.vertx.proton.streams.ProtonPublisher) Handler(io.vertx.core.Handler) Collections(java.util.Collections) Accepted(org.apache.qpid.proton.amqp.messaging.Accepted) ProtonServer(io.vertx.proton.ProtonServer) ProtonClient(io.vertx.proton.ProtonClient) ProtonConnection(io.vertx.proton.ProtonConnection) Async(io.vertx.ext.unit.Async) Delivery(io.vertx.proton.streams.Delivery) Subscription(org.reactivestreams.Subscription) Test(org.junit.Test)

Example 40 with Subscriber

use of org.reactivestreams.Subscriber in project vertx-proton by vert-x3.

the class ProtonPublisherIntTest method testCreateCancelSubscription.

@Test(timeout = 20000)
public void testCreateCancelSubscription(TestContext context) throws Exception {
    server.close();
    final Async clientLinkOpenAsync = context.async();
    final Async serverLinkOpenAsync = context.async();
    final Async serverLinkCloseAsync = context.async();
    final Async clientLinkCloseAsync = context.async();
    ProtonServer protonServer = null;
    try {
        protonServer = createServer((serverConnection) -> {
            serverConnection.openHandler(result -> {
                serverConnection.open();
            });
            serverConnection.sessionOpenHandler(session -> session.open());
            serverConnection.senderOpenHandler(serverSender -> {
                serverSender.closeHandler(res -> {
                    serverLinkCloseAsync.complete();
                    serverSender.close();
                });
                // Buffer a single message to send when credit arrives
                serverSender.send(message(String.valueOf(1)));
                LOG.trace("Server sender opened");
                serverSender.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();
            ProtonPublisher<Delivery> publisher = ProtonStreams.createDeliveryConsumer(connection, "myAddress");
            publisher.subscribe(new Subscriber<Delivery>() {

                Subscription sub = null;

                @Override
                public void onSubscribe(Subscription s) {
                    clientLinkOpenAsync.complete();
                    sub = s;
                    s.request(5);
                }

                @Override
                public void onNext(Delivery d) {
                    validateMessage(context, 1, String.valueOf(1), d.message());
                    sub.cancel();
                }

                @Override
                public void onError(Throwable t) {
                    if (!clientLinkCloseAsync.isCompleted()) {
                        context.fail("onError called");
                    }
                }

                @Override
                public void onComplete() {
                    clientLinkCloseAsync.complete();
                }
            });
        });
        serverLinkOpenAsync.awaitSuccess();
        clientLinkOpenAsync.awaitSuccess();
        serverLinkCloseAsync.awaitSuccess();
        clientLinkCloseAsync.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) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ProtonServer(io.vertx.proton.ProtonServer) ArrayList(java.util.ArrayList) MockServerTestBase(io.vertx.proton.MockServerTestBase) Delivery(io.vertx.proton.streams.Delivery) 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) DeliveryState(org.apache.qpid.proton.amqp.transport.DeliveryState) Message(org.apache.qpid.proton.message.Message) AsyncResult(io.vertx.core.AsyncResult) Subscriber(org.reactivestreams.Subscriber) Logger(io.vertx.core.impl.logging.Logger) Test(org.junit.Test) ProtonClient(io.vertx.proton.ProtonClient) UUID(java.util.UUID) TerminusExpiryPolicy(org.apache.qpid.proton.amqp.messaging.TerminusExpiryPolicy) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Released(org.apache.qpid.proton.amqp.messaging.Released) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) Section(org.apache.qpid.proton.amqp.messaging.Section) FutureHandler(io.vertx.proton.FutureHandler) Subscription(org.reactivestreams.Subscription) TerminusDurability(org.apache.qpid.proton.amqp.messaging.TerminusDurability) ProtonPublisher(io.vertx.proton.streams.ProtonPublisher) Handler(io.vertx.core.Handler) Collections(java.util.Collections) Accepted(org.apache.qpid.proton.amqp.messaging.Accepted) ProtonConnection(io.vertx.proton.ProtonConnection) ProtonServer(io.vertx.proton.ProtonServer) Async(io.vertx.ext.unit.Async) Delivery(io.vertx.proton.streams.Delivery) ProtonClient(io.vertx.proton.ProtonClient) Subscription(org.reactivestreams.Subscription) Test(org.junit.Test)

Aggregations

Subscriber (org.reactivestreams.Subscriber)114 Subscription (org.reactivestreams.Subscription)75 Test (org.junit.Test)55 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)41 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)38 List (java.util.List)34 Arrays (java.util.Arrays)26 AtomicReference (java.util.concurrent.atomic.AtomicReference)26 Publisher (org.reactivestreams.Publisher)26 ArrayList (java.util.ArrayList)24 ProtonClient (io.vertx.proton.ProtonClient)22 ProtonConnection (io.vertx.proton.ProtonConnection)22 Message (org.apache.qpid.proton.message.Message)20 CountDownLatch (java.util.concurrent.CountDownLatch)19 Collections (java.util.Collections)18 AmqpValue (org.apache.qpid.proton.amqp.messaging.AmqpValue)18 Handler (io.vertx.core.Handler)16 Logger (io.vertx.core.impl.logging.Logger)16 LoggerFactory (io.vertx.core.impl.logging.LoggerFactory)16 ProtonStreams (io.vertx.proton.streams.ProtonStreams)16