use of org.apache.qpid.proton.amqp.messaging.AmqpValue 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 org.apache.qpid.proton.amqp.messaging.AmqpValue in project vertx-proton by vert-x3.
the class HelloWorldServer method helloProcessConnection.
private static void helloProcessConnection(Vertx vertx, ProtonConnection connection) {
connection.openHandler(res -> {
System.out.println("Client connected: " + connection.getRemoteContainer());
connection.open();
}).closeHandler(c -> {
System.out.println("Client closing amqp connection: " + connection.getRemoteContainer());
connection.close();
connection.disconnect();
}).disconnectHandler(c -> {
System.out.println("Client socket disconnected: " + connection.getRemoteContainer());
connection.disconnect();
}).sessionOpenHandler(session -> session.open());
connection.receiverOpenHandler(receiver -> {
// This is rather naive, for example use only, proper
receiver.setTarget(receiver.getRemoteTarget()).handler((delivery, msg) -> {
String address = msg.getAddress();
if (address == null) {
address = receiver.getRemoteTarget().getAddress();
}
Section body = msg.getBody();
if (body instanceof AmqpValue) {
String content = (String) ((AmqpValue) body).getValue();
System.out.println("message to:" + address + ", body: " + content);
}
}).open();
});
connection.senderOpenHandler(sender -> {
System.out.println("Sending to client from: " + sender.getRemoteSource().getAddress());
// This is rather naive, for example use only, proper
sender.setSource(sender.getRemoteSource());
// servers should ensure that they advertise their own
// Source settings that actually reflect what is in place.
// The request may have also been for a dynamic address.
sender.open();
vertx.setPeriodic(1000, timer -> {
if (connection.isDisconnected()) {
vertx.cancelTimer(timer);
} else {
System.out.println("Sending message to client");
Message m = message("Hello World from Server!");
sender.send(m, delivery -> {
System.out.println("The message was received by the client.");
});
}
});
});
}
use of org.apache.qpid.proton.amqp.messaging.AmqpValue in project hono by eclipse.
the class RegistrationAmqpEndpointTest method testProcessMessageSendsRequestViaEventBus.
/**
* Verifies that the endpoint forwards a request message via the event bus.
*/
@Test
public void testProcessMessageSendsRequestViaEventBus() {
final Message msg = ProtonHelper.message();
msg.setMessageId("4711");
msg.setSubject(RegistrationConstants.ACTION_ASSERT);
msg.setBody(new AmqpValue(new JsonObject().put("temp", 15).encode()));
MessageHelper.annotate(msg, resource);
endpoint.processRequest(msg, resource, Constants.PRINCIPAL_ANONYMOUS);
verify(eventBus).send(eq(RegistrationConstants.EVENT_BUS_ADDRESS_REGISTRATION_IN), any(JsonObject.class));
}
use of org.apache.qpid.proton.amqp.messaging.AmqpValue in project hono by eclipse.
the class CredentialsAmqpEndpointTest method testProcessMessageSendsRequestViaEventBus.
/**
* Verifies that the endpoint forwards a request message via the event bus.
*/
@Test
public void testProcessMessageSendsRequestViaEventBus() {
final Message msg = ProtonHelper.message();
msg.setMessageId("random-id");
msg.setSubject(CredentialsConstants.CredentialsAction.add.toString());
MessageHelper.addDeviceId(msg, "4711");
MessageHelper.addTenantId(msg, Constants.DEFAULT_TENANT);
MessageHelper.annotate(msg, resource);
msg.setBody(new AmqpValue(new JsonObject().put("temp", 15).encode()));
endpoint.processRequest(msg, resource, Constants.PRINCIPAL_ANONYMOUS);
verify(eventBus).send(eq(CredentialsConstants.EVENT_BUS_ADDRESS_CREDENTIALS_IN), any(JsonObject.class));
}
use of org.apache.qpid.proton.amqp.messaging.AmqpValue in project hono by eclipse.
the class AbstractRequestResponseClient method createAndSendRequest.
/**
* Creates a request message for a payload and headers and sends it to the peer.
* <p>
* This method first checks if the sender has any credit left. If not, the result handler is failed immediately.
* Otherwise, the request message is sent and a timer is started which fails the result handler,
* if no response is received within <em>requestTimeout</em> milliseconds.
*
* @param action The operation that the request is supposed to trigger/invoke.
* @param properties The headers to include in the request message as AMQP application properties.
* @param payload The payload to include in the request message as a an AMQP Value section.
* @param resultHandler The handler to notify about the outcome of the request. The handler is failed with
* a {@link ServerErrorException} if the request cannot be sent to the remote service,
* e.g. because there is no connection to the service or there are no credits available
* for sending the request or the request timed out.
* @param cacheKey The key to use for caching the response (if the service allows caching).
* @throws NullPointerException if action or result handler are {@code null}.
* @throws IllegalArgumentException if the properties contain any non-primitive typed values.
* @see AbstractHonoClient#setApplicationProperties(Message, Map)
*/
protected final void createAndSendRequest(final String action, final Map<String, Object> properties, final JsonObject payload, final Handler<AsyncResult<R>> resultHandler, final Object cacheKey) {
Objects.requireNonNull(action);
Objects.requireNonNull(resultHandler);
if (isOpen()) {
final Message request = createMessage(action, properties);
if (payload != null) {
request.setContentType(RequestResponseApiConstants.CONTENT_TYPE_APPLICATION_JSON);
request.setBody(new AmqpValue(payload.encode()));
}
sendRequest(request, resultHandler, cacheKey);
} else {
resultHandler.handle(Future.failedFuture(new ServerErrorException(HttpURLConnection.HTTP_UNAVAILABLE, "sender and/or receiver link is not open")));
}
}
Aggregations