Java Code Examples for io.vertx.proton.ProtonSender#send()

The following examples show how to use io.vertx.proton.ProtonSender#send() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AuthenticationEndpoint.java    From hono with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public final void onLinkAttach(final ProtonConnection con, final ProtonSender sender, final ResourceIdentifier targetResource) {

    if (ProtonQoS.AT_LEAST_ONCE.equals(sender.getRemoteQoS())) {
        final HonoUser user = Constants.getClientPrincipal(con);
        sender.setQoS(ProtonQoS.AT_LEAST_ONCE).open();
        logger.debug("transferring token to client...");
        final Message tokenMsg = ProtonHelper.message(user.getToken());
        MessageHelper.addProperty(tokenMsg, AuthenticationConstants.APPLICATION_PROPERTY_TYPE, AuthenticationConstants.TYPE_AMQP_JWT);
        sender.send(tokenMsg, disposition -> {
            if (disposition.remotelySettled()) {
                logger.debug("successfully transferred auth token to client");
            } else {
                logger.debug("failed to transfer auth token to client");
            }
            sender.close();
        });
    } else {
        onLinkDetach(sender, ProtonHelper.condition(AmqpError.INVALID_FIELD, "supports AT_LEAST_ONCE delivery mode only"));
    }
}
 
Example 2
Source File: SingleSender.java    From enmasse with Apache License 2.0 6 votes vote down vote up
@Override
protected void sendMessages(final ProtonConnection connection, final ProtonSender sender) {
    this.state = "ready to send";

    switch (sender.getQoS()) {
        case AT_MOST_ONCE:
            sender.send(this.message);
            this.state = "message sent";
            this.resultPromise.complete(Collections.emptyList());
            break;
        case AT_LEAST_ONCE:
            sender.send(this.message, del -> {
                this.state = "message disposition received";
                this.deliveries.add(del);
                if (del.remotelySettled()) {
                    this.state = "message settled";
                    // complete, sending a copy
                    this.resultPromise.complete(new ArrayList<>(this.deliveries));
                }
            });
            this.state = "message sent";
            break;
    }
}
 
Example 3
Source File: DeliveryPredicateSender.java    From enmasse with Apache License 2.0 6 votes vote down vote up
private void sendNext(ProtonConnection connection, ProtonSender sender) {

        Message message;
        if (messageQueue.hasNext() && (message = messageQueue.next()) != null) {
            sender.send(message, protonDelivery -> {
                if (predicate.test(protonDelivery)) {
                    resultPromise.complete(numSent.get());
                    connection.close();
                } else {
                    numSent.incrementAndGet();
                    vertx.runOnContext(id -> sendNext(connection, sender));
                }
            });
        } else {
            if (predicate.test(null)) {
                resultPromise.complete(numSent.get());
            } else {
                resultPromise.completeExceptionally(new RuntimeException("No more messages to send after + " + numSent.get() + " messages sent"));
                connectPromise.completeExceptionally(new RuntimeException("No more messages to send after + " + numSent.get() + " messages sent"));
            }
            connection.close();
        }
    }
 
Example 4
Source File: CommandAndControlAmqpIT.java    From hono with Eclipse Public License 2.0 5 votes vote down vote up
private ProtonMessageHandler createCommandConsumer(final VertxTestContext ctx, final ProtonReceiver cmdReceiver,
        final ProtonSender cmdResponseSender) {

    return (delivery, msg) -> {
        ctx.verify(() -> {
            assertThat(msg.getReplyTo()).isNotNull();
            assertThat(msg.getSubject()).isNotNull();
            assertThat(msg.getCorrelationId()).isNotNull();
        });
        final String command = msg.getSubject();
        final Object correlationId = msg.getCorrelationId();
        log.debug("received command [name: {}, reply-to: {}, correlation-id: {}]", command, msg.getReplyTo(), correlationId);
        ProtonHelper.accepted(delivery, true);
        cmdReceiver.flow(1);
        // send response
        final Message commandResponse = ProtonHelper.message(command + " ok");
        commandResponse.setAddress(msg.getReplyTo());
        commandResponse.setCorrelationId(correlationId);
        commandResponse.setContentType("text/plain");
        MessageHelper.addProperty(commandResponse, MessageHelper.APP_PROPERTY_STATUS, HttpURLConnection.HTTP_OK);
        log.debug("sending response [to: {}, correlation-id: {}]", commandResponse.getAddress(), commandResponse.getCorrelationId());
        cmdResponseSender.send(commandResponse, updatedDelivery -> {
            if (!Accepted.class.isInstance(updatedDelivery.getRemoteState())) {
                log.error("AMQP adapter did not accept command response [remote state: {}]",
                        updatedDelivery.getRemoteState().getClass().getSimpleName());
            }
        });
    };
}
 
Example 5
Source File: Sender.java    From enmasse with Apache License 2.0 5 votes vote down vote up
private void sendNext(ProtonConnection connection, ProtonSender sender) {

        Message message;
        if (messageQueue.hasNext() && (message = messageQueue.next()) != null) {
            if (sender.getQoS().equals(ProtonQoS.AT_MOST_ONCE)) {
                sender.send(message);
                numSent.incrementAndGet();
                if (predicate.test(message)) {
                    resultPromise.complete(numSent.get());
                } else {
                    vertx.runOnContext(id -> sendNext(connection, sender));
                }
            } else {
                sender.send(message, protonDelivery -> {
                    if (protonDelivery.getRemoteState().equals(Accepted.getInstance())) {
                        numSent.incrementAndGet();
                        if (predicate.test(message)) {
                            resultPromise.complete(numSent.get());
                            connection.close();
                        } else {
                            sendNext(connection, sender);
                        }
                    } else {
                        resultPromise.completeExceptionally(new IllegalStateException("Message not accepted (remote state: " + protonDelivery.getRemoteState() + ") after " + numSent.get() + " messages sent"));
                        connectPromise.completeExceptionally(new IllegalStateException("Message not accepted (remote state: " + protonDelivery.getRemoteState() + ") after " + numSent.get() + " messages sent"));
                        connection.close();
                    }
                });
            }
        } else {
            if (predicate.test(null)) {
                resultPromise.complete(numSent.get());
            } else {
                resultPromise.completeExceptionally(new RuntimeException("No more messages to send after + " + numSent.get() + " messages sent"));
                connectPromise.completeExceptionally(new RuntimeException("No more messages to send after + " + numSent.get() + " messages sent"));
            }
            connection.close();
        }
    }
 
Example 6
Source File: BlockingClient.java    From enmasse with Apache License 2.0 5 votes vote down vote up
private void sendNext(ProtonConnection connection, ProtonSender sender, Queue<Message> messageQueue, CountDownLatch latch) {
    Message message = messageQueue.poll();

    if (message == null) {
        connection.close();
        latch.countDown();
    } else {
        sender.send(message, protonDelivery -> sendNext(connection, sender, messageQueue, latch));
    }
}
 
Example 7
Source File: HelloWorld.java    From vertx-proton with Apache License 2.0 5 votes vote down vote up
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()));
  });
}