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()));
});
}
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));
}
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();
}
}
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;
}
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));
}
Aggregations