Search in sources :

Example 1 with ProtonSender

use of io.vertx.proton.ProtonSender 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 2 with ProtonSender

use of io.vertx.proton.ProtonSender in project hono by eclipse.

the class ForwardingTelemetryDownstreamAdapterTest method testProcessMessageForwardsDownstreamDisposition.

/**
 * Verifies that an unsettled telemetry message received from an upstream client is
 * forwarded to the downstream container and the downstream container's disposition
 * is forwarded to the upstream client.
 *
 * @param ctx The test context.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testProcessMessageForwardsDownstreamDisposition(final TestContext ctx) {
    final UpstreamReceiver client = TestSupport.newClient();
    // GIVEN an adapter with a connection to a downstream container
    final ProtonSender sender = TestSupport.newMockSender(false);
    final ForwardingTelemetryDownstreamAdapter adapter = new ForwardingTelemetryDownstreamAdapter(vertx, TestSupport.newMockSenderFactory(sender));
    adapter.setMetrics(mock(MessagingMetrics.class));
    adapter.setDownstreamConnectionFactory(connectionFactory);
    adapter.start(Future.future());
    adapter.addSender(client, sender);
    // WHEN processing an unsettled telemetry message
    final Message msg = ProtonHelper.message(TELEMETRY_MSG_CONTENT);
    MessageHelper.addDeviceId(msg, DEVICE_ID);
    final ProtonDelivery upstreamDelivery = mock(ProtonDelivery.class);
    when(upstreamDelivery.remotelySettled()).thenReturn(Boolean.FALSE);
    adapter.processMessage(client, upstreamDelivery, msg);
    // THEN the message is being delivered to the downstream container
    final ArgumentCaptor<Handler> deliveryHandler = ArgumentCaptor.forClass(Handler.class);
    verify(sender).send(eq(msg), deliveryHandler.capture());
    // and when the downstream container rejects the message
    final ProtonDelivery downstreamDelivery = mock(ProtonDelivery.class);
    when(downstreamDelivery.remotelySettled()).thenReturn(Boolean.TRUE);
    when(downstreamDelivery.getRemoteState()).thenReturn(new Rejected());
    deliveryHandler.getValue().handle(downstreamDelivery);
    // then the upstream delivery is settled with the rejected outcome
    verify(upstreamDelivery).disposition(any(Rejected.class), eq(Boolean.TRUE));
}
Also used : ProtonSender(io.vertx.proton.ProtonSender) Message(org.apache.qpid.proton.message.Message) ProtonDelivery(io.vertx.proton.ProtonDelivery) Handler(io.vertx.core.Handler) UpstreamReceiver(org.eclipse.hono.messaging.UpstreamReceiver) Rejected(org.apache.qpid.proton.amqp.messaging.Rejected) MessagingMetrics(org.eclipse.hono.messaging.MessagingMetrics) Test(org.junit.Test)

Example 3 with ProtonSender

use of io.vertx.proton.ProtonSender in project hono by eclipse.

the class ForwardingDownstreamAdapter method closeSender.

private void closeSender(final UpstreamReceiver link) {
    ProtonSender sender = activeSenders.remove(link);
    if (sender != null && sender.isOpen()) {
        logger.info("closing downstream sender [con: {}, link: {}]", link.getConnectionId(), link.getLinkId());
        metrics.decrementDownstreamSenders(link.getTargetAddress());
        metrics.submitDownstreamLinkCredits(link.getTargetAddress(), 0);
        sender.close();
    }
}
Also used : ProtonSender(io.vertx.proton.ProtonSender)

Example 4 with ProtonSender

use of io.vertx.proton.ProtonSender in project hono by eclipse.

the class TestSupport method newMockSender.

@SuppressWarnings("unchecked")
public static ProtonSender newMockSender(final boolean drainFlag) {
    @SuppressWarnings("rawtypes") final ArgumentCaptor<Handler> drainHandlerCaptor = ArgumentCaptor.forClass(Handler.class);
    Record attachments = mock(Record.class);
    ProtonSender sender = mock(ProtonSender.class);
    when(sender.attachments()).thenReturn(attachments);
    when(sender.isOpen()).thenReturn(Boolean.TRUE);
    when(sender.getCredit()).thenReturn(DEFAULT_CREDITS);
    when(sender.getQueued()).thenReturn(0);
    when(sender.getDrain()).thenReturn(drainFlag);
    when(sender.open()).then(invocation -> {
        drainHandlerCaptor.getValue().handle(sender);
        return sender;
    });
    when(sender.sendQueueDrainHandler(drainHandlerCaptor.capture())).then(invocation -> {
        return sender;
    });
    Target target = new Target();
    target.setAddress(DEFAULT_ADDRESS);
    when(sender.getTarget()).thenReturn(target);
    return sender;
}
Also used : ProtonSender(io.vertx.proton.ProtonSender) Target(org.apache.qpid.proton.amqp.messaging.Target) Handler(io.vertx.core.Handler) Record(org.apache.qpid.proton.engine.Record)

Example 5 with ProtonSender

use of io.vertx.proton.ProtonSender in project hono by eclipse.

the class ForwardingEventDownstreamAdapterTest method testProcessMessageForwardsMessageToDownstreamSender.

/**
 * Verifies that an event uploaded by an upstream client is forwarded to the
 * downstream container.
 *
 * @param ctx The test context.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testProcessMessageForwardsMessageToDownstreamSender(final TestContext ctx) {
    final UpstreamReceiver client = newClient();
    final ProtonDelivery delivery = mock(ProtonDelivery.class);
    final ProtonDelivery downstreamDelivery = mock(ProtonDelivery.class);
    when(downstreamDelivery.getRemoteState()).thenReturn(ACCEPTED);
    when(downstreamDelivery.remotelySettled()).thenReturn(true);
    // GIVEN an adapter with a connection to a downstream container
    final Async msgSent = ctx.async();
    ProtonSender sender = newMockSender(false);
    when(sender.send(any(Message.class), any(Handler.class))).then(invocation -> {
        msgSent.complete();
        final Handler handler = invocation.getArgument(1);
        handler.handle(downstreamDelivery);
        return null;
    });
    ForwardingEventDownstreamAdapter adapter = new ForwardingEventDownstreamAdapter(vertx, newMockSenderFactory(sender));
    adapter.setMetrics(mock(MessagingMetrics.class));
    adapter.setDownstreamConnectionFactory(newMockConnectionFactory(false));
    adapter.start(Future.future());
    adapter.addSender(client, sender);
    // WHEN processing an event
    Message msg = ProtonHelper.message(EVENT_MSG_CONTENT);
    MessageHelper.addDeviceId(msg, DEVICE_ID);
    adapter.processMessage(client, delivery, msg);
    // THEN the message has been delivered to the downstream container
    msgSent.await(1000);
    // and disposition was returned
    verify(delivery).disposition(any(Accepted.class), eq(Boolean.TRUE));
}
Also used : ProtonSender(io.vertx.proton.ProtonSender) ProtonDelivery(io.vertx.proton.ProtonDelivery) Message(org.apache.qpid.proton.message.Message) Async(io.vertx.ext.unit.Async) Handler(io.vertx.core.Handler) UpstreamReceiver(org.eclipse.hono.messaging.UpstreamReceiver) MessagingMetrics(org.eclipse.hono.messaging.MessagingMetrics) Accepted(org.apache.qpid.proton.amqp.messaging.Accepted) Test(org.junit.Test)

Aggregations

ProtonSender (io.vertx.proton.ProtonSender)22 Handler (io.vertx.core.Handler)11 Message (org.apache.qpid.proton.message.Message)9 Test (org.junit.Test)9 ProtonDelivery (io.vertx.proton.ProtonDelivery)8 ProtonConnection (io.vertx.proton.ProtonConnection)6 MessagingMetrics (org.eclipse.hono.messaging.MessagingMetrics)5 UpstreamReceiver (org.eclipse.hono.messaging.UpstreamReceiver)5 Vertx (io.vertx.core.Vertx)4 Future (io.vertx.core.Future)3 Async (io.vertx.ext.unit.Async)3 ProtonQoS (io.vertx.proton.ProtonQoS)3 ErrorCondition (org.apache.qpid.proton.amqp.transport.ErrorCondition)3 Record (org.apache.qpid.proton.engine.Record)3 Constants (org.eclipse.hono.util.Constants)3 ResourceIdentifier (org.eclipse.hono.util.ResourceIdentifier)3 AsyncResult (io.vertx.core.AsyncResult)2 ProtonClientOptions (io.vertx.proton.ProtonClientOptions)2 ProtonHelper (io.vertx.proton.ProtonHelper)2 ProtonSession (io.vertx.proton.ProtonSession)2