Search in sources :

Example 91 with Subscriber

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

the class TrackerSubscriberWhiteboxVerificationTckTest 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) SubscriberWhiteboxVerification(org.reactivestreams.tck.SubscriberWhiteboxVerification) LoggerFactory(io.vertx.core.impl.logging.LoggerFactory) MockServer(io.vertx.proton.MockServer) ProtonSubscriberImpl(io.vertx.proton.streams.impl.ProtonSubscriberImpl) 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) ProtonSubscriber(io.vertx.proton.streams.ProtonSubscriber) TestEnvironment(org.reactivestreams.tck.TestEnvironment) Subscription(org.reactivestreams.Subscription) ProtonConnectionImpl(io.vertx.proton.impl.ProtonConnectionImpl) 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 92 with Subscriber

use of org.reactivestreams.Subscriber 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)

Aggregations

Subscriber (org.reactivestreams.Subscriber)92 Subscription (org.reactivestreams.Subscription)56 Test (org.junit.Test)49 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)24 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)23 ProtonClient (io.vertx.proton.ProtonClient)22 ProtonConnection (io.vertx.proton.ProtonConnection)22 Message (org.apache.qpid.proton.message.Message)20 List (java.util.List)18 AmqpValue (org.apache.qpid.proton.amqp.messaging.AmqpValue)18 AtomicReference (java.util.concurrent.atomic.AtomicReference)17 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 ExecutionException (java.util.concurrent.ExecutionException)16 Symbol (org.apache.qpid.proton.amqp.Symbol)16 Section (org.apache.qpid.proton.amqp.messaging.Section)16 ArrayList (java.util.ArrayList)14 Collections (java.util.Collections)13