Search in sources :

Example 26 with ProtonConnection

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

the class MessageSubscriberWhiteboxVerificationTckTest method createSubscriber.

@Override
public Subscriber<Message> createSubscriber(WhiteboxSubscriberProbe<Message> probe) {
    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();
            // Modified stream impl, overriding methods to add test probe instrumentation
            ProtonSubscriberImpl subscriber = new ProtonSubscriberImpl("myAddress", (ProtonConnectionImpl) conn);
            ProtonSubscriber<Message> stream = new ProtonSubscriberWrapperImpl(subscriber) {

                @Override
                public void onSubscribe(final Subscription s) {
                    super.onSubscribe(s);
                    probe.registerOnSubscribe(new SubscriberPuppet() {

                        @Override
                        public void triggerRequest(long n) {
                            s.request(n);
                        }

                        @Override
                        public void signalCancel() {
                            s.cancel();
                        }
                    });
                }

                @Override
                public void onNext(Message value) {
                    probe.registerOnNext(value);
                    super.onNext(value);
                }

                @Override
                public void onError(Throwable t) {
                    probe.registerOnError(t);
                    super.onError(t);
                }

                @Override
                public void onComplete() {
                    probe.registerOnComplete();
                    super.onComplete();
                }
            };
            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) ProtonSubscriberImpl(io.vertx.proton.streams.impl.ProtonSubscriberImpl) ProtonSubscriberWrapperImpl(io.vertx.proton.streams.impl.ProtonSubscriberWrapperImpl) Subscription(org.reactivestreams.Subscription)

Example 27 with ProtonConnection

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

the class HelloWorld method helloWorldSendAndConsumeExample.

private static void helloWorldSendAndConsumeExample(ProtonConnection connection) {
    connection.open();
    // Receive messages from queue "foo" (using an ActiveMQ style address as example).
    String address = "queue://foo";
    connection.createReceiver(address).handler((delivery, msg) -> {
        Section body = msg.getBody();
        if (body instanceof AmqpValue) {
            String content = (String) ((AmqpValue) body).getValue();
            System.out.println("Received message with content: " + content);
        }
    // By default, the receiver automatically accepts (and settles) the delivery
    // when the handler returns, if no other disposition has been applied.
    // To change this and always manage dispositions yourself, use the
    // setAutoAccept method on the receiver.
    }).open();
    // Create an anonymous (no address) sender, have the message carry its destination
    ProtonSender sender = connection.createSender(null);
    // Create a message to send, have it carry its destination for use with the anonymous sender
    Message message = message(address, "Hello World from client");
    // Can optionally add an openHandler or sendQueueDrainHandler
    // to await remote sender open completing or credit to send being
    // granted. But here we will just buffer the send immediately.
    sender.open();
    System.out.println("Sending message to server");
    sender.send(message, delivery -> {
        System.out.println(String.format("The message was received by the server: remote state=%s, remotely settled=%s", delivery.getRemoteState(), delivery.remotelySettled()));
    });
}
Also used : ProtonConnection(io.vertx.proton.ProtonConnection) Section(org.apache.qpid.proton.amqp.messaging.Section) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue) ProtonHelper.message(io.vertx.proton.ProtonHelper.message) Vertx(io.vertx.core.Vertx) ProtonSender(io.vertx.proton.ProtonSender) ProtonClient(io.vertx.proton.ProtonClient) Message(org.apache.qpid.proton.message.Message) ProtonSender(io.vertx.proton.ProtonSender) Message(org.apache.qpid.proton.message.Message) Section(org.apache.qpid.proton.amqp.messaging.Section) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue)

Example 28 with ProtonConnection

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

the class HelloWorld method main.

public static void main(String[] args) {
    // Create the Vert.x instance
    Vertx vertx = Vertx.vertx();
    // Create the Vert.x AMQP client instance
    ProtonClient client = ProtonClient.create(vertx);
    // Connect, then use the event loop thread to process the connection
    client.connect("localhost", 5672, res -> {
        if (res.succeeded()) {
            System.out.println("We're connected");
            ProtonConnection connection = res.result();
            helloWorldSendAndConsumeExample(connection);
        } else {
            res.cause().printStackTrace();
        }
    });
    // Just stop main() from exiting
    try {
        System.in.read();
    } catch (Exception ignore) {
    }
}
Also used : ProtonConnection(io.vertx.proton.ProtonConnection) Vertx(io.vertx.core.Vertx) ProtonClient(io.vertx.proton.ProtonClient)

Example 29 with ProtonConnection

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

the class ProtonServerImplTest method testCustomAuthenticatorSuceedsAuthentication.

@Test(timeout = 20000)
public void testCustomAuthenticatorSuceedsAuthentication(TestContext context) {
    Async connectedAsync = context.async();
    Async authenticatedAsync = context.async();
    ProtonServer.create(vertx).saslAuthenticatorFactory(new TestPlainAuthenticatorFactory()).connectHandler(protonConnection -> {
        // Verify the expected auth detail was recorded in the connection attachments, just using a String here.
        String authValue = protonConnection.attachments().get(AUTH_KEY, String.class);
        context.assertEquals(AUTH_VALUE, authValue);
        authenticatedAsync.complete();
    }).listen(server -> ProtonClient.create(vertx).connect("localhost", server.result().actualPort(), GOOD_USER, PASSWD, protonConnectionAsyncResult -> {
        context.assertTrue(protonConnectionAsyncResult.succeeded());
        protonConnectionAsyncResult.result().disconnect();
        connectedAsync.complete();
    }));
    authenticatedAsync.awaitSuccess();
    connectedAsync.awaitSuccess();
}
Also used : TestContext(io.vertx.ext.unit.TestContext) ProtonConnection(io.vertx.proton.ProtonConnection) Async(io.vertx.ext.unit.Async) ProtonSaslAuthenticator(io.vertx.proton.sasl.ProtonSaslAuthenticator) Vertx(io.vertx.core.Vertx) RunWith(org.junit.runner.RunWith) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ProtonClient(io.vertx.proton.ProtonClient) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Sasl(org.apache.qpid.proton.engine.Sasl) SaslOutcome(org.apache.qpid.proton.engine.Sasl.SaslOutcome) Context(io.vertx.core.Context) ProtonServer(io.vertx.proton.ProtonServer) StandardCharsets(java.nio.charset.StandardCharsets) Transport(org.apache.qpid.proton.engine.Transport) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) Handler(io.vertx.core.Handler) ProtonSaslAuthenticatorFactory(io.vertx.proton.sasl.ProtonSaslAuthenticatorFactory) NetSocket(io.vertx.core.net.NetSocket) Before(org.junit.Before) Async(io.vertx.ext.unit.Async) Test(org.junit.Test)

Example 30 with ProtonConnection

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

the class TrackerSubscriberWhiteboxVerificationTckTest method createSubscriber.

@Override
public Subscriber<Tracker> createSubscriber(WhiteboxSubscriberProbe<Tracker> probe) {
    int actualPort = server.actualPort();
    ProtonClient client = ProtonClient.create(vertx);
    AtomicReference<Subscriber<Tracker>> ref = new AtomicReference<>();
    CountDownLatch latch = new CountDownLatch(1);
    client.connect("localhost", actualPort, result -> {
        if (result.succeeded()) {
            ProtonConnection conn = result.result();
            conn.open();
            // Modified stream impl, overriding methods to add test probe instrumentation
            ProtonSubscriber<Tracker> stream = new ProtonSubscriberImpl("myAddress", (ProtonConnectionImpl) conn) {

                @Override
                public void onSubscribe(final Subscription s) {
                    super.onSubscribe(s);
                    probe.registerOnSubscribe(new SubscriberPuppet() {

                        @Override
                        public void triggerRequest(long n) {
                            s.request(n);
                        }

                        @Override
                        public void signalCancel() {
                            s.cancel();
                        }
                    });
                }

                @Override
                public void onNext(Tracker value) {
                    probe.registerOnNext(value);
                    super.onNext(value);
                }

                @Override
                public void onError(Throwable t) {
                    probe.registerOnError(t);
                    super.onError(t);
                }

                @Override
                public void onComplete() {
                    probe.registerOnComplete();
                    super.onComplete();
                }
            };
            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 : Tracker(io.vertx.proton.streams.Tracker) 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) ProtonSubscriberImpl(io.vertx.proton.streams.impl.ProtonSubscriberImpl) Subscription(org.reactivestreams.Subscription)

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