Java Code Examples for io.vertx.proton.ProtonReceiver#open()

The following examples show how to use io.vertx.proton.ProtonReceiver#open() . 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: AuthenticationServerClient.java    From hono with Eclipse Public License 2.0 5 votes vote down vote up
private static Future<ProtonReceiver> openReceiver(final ProtonConnection openConnection, final ProtonMessageHandler messageHandler) {

        final Promise<ProtonReceiver> result = Promise.promise();
        final ProtonReceiver recv = openConnection.createReceiver(AuthenticationConstants.ENDPOINT_NAME_AUTHENTICATION);
        recv.openHandler(result);
        recv.handler(messageHandler);
        recv.open();
        return result.future();
    }
 
Example 2
Source File: RequestResponseEndpoint.java    From hono with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Handles a client's request to establish a link for sending service invocation requests.
 * <p>
 * Configure and check the receiver link of the endpoint.
 * The remote link of the receiver must not demand the AT_MOST_ONCE QoS (not supported).
 * The receiver link itself is configured with the AT_LEAST_ONCE QoS and grants the configured credits
 * ({@link ServiceConfigProperties#getReceiverLinkCredit()}) with autoAcknowledge.
 * <p>
 * Handling of request messages is delegated to
 * {@link #handleRequestMessage(ProtonConnection, ProtonReceiver, ResourceIdentifier, ProtonDelivery, Message)}.
 *
 * @param con The AMQP connection that the link is part of.
 * @param receiver The ProtonReceiver that has already been created for this endpoint.
 * @param targetAddress The resource identifier for this endpoint (see {@link ResourceIdentifier} for details).
 */
@Override
public final void onLinkAttach(final ProtonConnection con, final ProtonReceiver receiver, final ResourceIdentifier targetAddress) {

    if (ProtonQoS.AT_MOST_ONCE.equals(receiver.getRemoteQoS())) {
        logger.debug("client wants to use unsupported AT MOST ONCE delivery mode for endpoint [{}], closing link ...", getName());
        receiver.setCondition(ProtonHelper.condition(AmqpError.PRECONDITION_FAILED.toString(), "endpoint requires AT_LEAST_ONCE QoS"));
        receiver.close();
    } else {

        logger.debug("establishing link for receiving request messages from client [{}]", receiver.getName());

        receiver.setQoS(ProtonQoS.AT_LEAST_ONCE);
        receiver.setAutoAccept(true); // settle received messages if the handler succeeds
        receiver.setTarget(receiver.getRemoteTarget());
        receiver.setSource(receiver.getRemoteSource());
        // We do manual flow control, credits are replenished after responses have been sent.
        receiver.setPrefetch(0);

        // set up handlers

        receiver.handler((delivery, message) -> {
            try {
                handleRequestMessage(con, receiver, targetAddress, delivery, message);
            } catch (final Exception ex) {
                logger.warn("error handling message", ex);
                ProtonHelper.released(delivery, true);
            }
        });
        HonoProtonHelper.setCloseHandler(receiver, remoteClose -> onLinkDetach(receiver));
        HonoProtonHelper.setDetachHandler(receiver, remoteDetach -> onLinkDetach(receiver));

        // acknowledge the remote open
        receiver.open();

        // send out initial credits, after opening
        logger.debug("flowing {} credits to client", config.getReceiverLinkCredit());
        receiver.flow(config.getReceiverLinkCredit());
    }
}
 
Example 3
Source File: AbstractRequestResponseEndpoint.java    From hono with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Handles a client's request to establish a link for sending service invocation requests.
 * <p>
 * Configure and check the receiver link of the endpoint.
 * The remote link of the receiver must not demand the AT_MOST_ONCE QoS (not supported).
 * The receiver link itself is configured with the AT_LEAST_ONCE QoS and grants the configured credits
 * ({@link ServiceConfigProperties#getReceiverLinkCredit()}) with autoAcknowledge.
 * <p>
 * Handling of request messages is delegated to
 * {@link #handleRequestMessage(ProtonConnection, ProtonReceiver, ResourceIdentifier, ProtonDelivery, Message)}.
 *
 * @param con The AMQP connection that the link is part of.
 * @param receiver The ProtonReceiver that has already been created for this endpoint.
 * @param targetAddress The resource identifier for this endpoint (see {@link ResourceIdentifier} for details).
 */
@Override
public final void onLinkAttach(final ProtonConnection con, final ProtonReceiver receiver, final ResourceIdentifier targetAddress) {

    if (ProtonQoS.AT_MOST_ONCE.equals(receiver.getRemoteQoS())) {
        logger.debug("client wants to use unsupported AT MOST ONCE delivery mode for endpoint [{}], closing link ...", getName());
        receiver.setCondition(ProtonHelper.condition(AmqpError.PRECONDITION_FAILED.toString(), "endpoint requires AT_LEAST_ONCE QoS"));
        receiver.close();
    } else {

        logger.debug("establishing link for receiving request messages from client [{}]", receiver.getName());

        receiver.setQoS(ProtonQoS.AT_LEAST_ONCE);
        receiver.setAutoAccept(true); // settle received messages if the handler succeeds
        receiver.setTarget(receiver.getRemoteTarget());
        receiver.setSource(receiver.getRemoteSource());
        // We do manual flow control, credits are replenished after responses have been sent.
        receiver.setPrefetch(0);

        // set up handlers

        receiver.handler((delivery, message) -> {
            try {
                handleRequestMessage(con, receiver, targetAddress, delivery, message);
            } catch (final Exception ex) {
                logger.warn("error handling message", ex);
                ProtonHelper.released(delivery, true);
            }
        });
        HonoProtonHelper.setCloseHandler(receiver, remoteClose -> onLinkDetach(receiver));
        HonoProtonHelper.setDetachHandler(receiver, remoteDetach -> onLinkDetach(receiver));

        // acknowledge the remote open
        receiver.open();

        // send out initial credits, after opening
        logger.debug("flowing {} credits to client", config.getReceiverLinkCredit());
        receiver.flow(config.getReceiverLinkCredit());
    }
}
 
Example 4
Source File: CommandAndControlClient.java    From hono with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Opens a receiver link to receive commands from the org.eclipse.hono.cli.app.adapter.
 *
 * @param msgHandler The handler to invoke when a command message is received.
 * @return A succeeded future with the created receiver link or a failed future
 *         if the receiver link cannot be created.
 *
 */
private Future<ProtonReceiver> subscribeToCommands(final ProtonMessageHandler msgHandler) {
    final Promise<ProtonReceiver> result = Promise.promise();
    final ProtonReceiver receiver = adapterConnection.createReceiver(CommandConstants.COMMAND_ENDPOINT);
    receiver.setQoS(ProtonQoS.AT_LEAST_ONCE);
    receiver.handler(msgHandler);
    receiver.openHandler(result);
    receiver.open();
    return result.future().map(recver -> {
        writer.println("Device is now ready to receive commands (Press Ctrl + c to terminate)");
        writer.flush();
        return recver;
    });
}
 
Example 5
Source File: AmqpSourceBridgeEndpoint.java    From strimzi-kafka-bridge with Apache License 2.0 4 votes vote down vote up
@Override
public void handle(Endpoint<?> endpoint) {

    ProtonLink<?> link = (ProtonLink<?>) endpoint.get();
    AmqpConfig amqpConfig = (AmqpConfig) this.bridgeConfig.getAmqpConfig();

    if (!(link instanceof ProtonReceiver)) {
        throw new IllegalArgumentException("This Proton link must be a receiver");
    }

    if (this.converter == null) {
        try {
            this.converter = (MessageConverter<K, V, Message, Collection<Message>>) AmqpBridge.instantiateConverter(amqpConfig.getMessageConverter());
        } catch (AmqpErrorConditionException e) {
            AmqpBridge.detachWithError(link, e.toCondition());
            return;
        }
    }

    ProtonReceiver receiver = (ProtonReceiver) link;
    this.name = receiver.getName();

    // the delivery state is related to the acknowledgement from Apache Kafka
    receiver.setTarget(receiver.getRemoteTarget())
            .setAutoAccept(false)
            .closeHandler(ar -> {
                if (ar.succeeded()) {
                    this.processCloseReceiver(ar.result());
                }
            })
            .detachHandler(ar -> {
                this.processCloseReceiver(receiver);
            })
            .handler((delivery, message) -> {
                this.processMessage(receiver, delivery, message);
            });

    if (receiver.getRemoteQoS() == ProtonQoS.AT_MOST_ONCE) {
        // sender settle mode is SETTLED (so AT_MOST_ONCE QoS), we assume Apache Kafka
        // no problem in throughput terms so use prefetch due to no ack from Kafka server
        receiver.setPrefetch(amqpConfig.getFlowCredit());
    } else {
        // sender settle mode is UNSETTLED (or MIXED) (so AT_LEAST_ONCE QoS).
        // Thanks to the ack from Kafka server we can modulate flow control
        receiver.setPrefetch(0)
                .flow(amqpConfig.getFlowCredit());
    }

    receiver.open();

    this.receivers.put(receiver.getName(), receiver);
}