Example usage for io.vertx.core.eventbus MessageConsumer address

List of usage examples for io.vertx.core.eventbus MessageConsumer address

Introduction

In this page you can find the example usage for io.vertx.core.eventbus MessageConsumer address.

Prototype

String address();

Source Link

Usage

From source file:org.eclipse.hono.service.amqp.RequestResponseEndpoint.java

License:Open Source License

/**
 * Configure and check the sender link of the endpoint.
 * The sender link is used for the response to a received request and is driven by the vertx event bus.
 * It listens to the provided resource identifier of the endpoint as vertx event address and then sends the
 * constructed response./*  w  w  w  .  j  a  v  a 2  s.  co m*/
 * Since the response is endpoint specific, it is an abstract method {@link #getAmqpReply(io.vertx.core.eventbus.Message)} and needs to be implemented
 * by the subclass.
 *
 * @param con The AMQP connection that the link is part of.
 * @param sender The ProtonSender that has already been created for this endpoint.
 * @param replyToAddress The resource identifier for the responses of this endpoint (see {@link ResourceIdentifier} for details).
 *                      Note that the reply address is different for each client and is passed in during link creation.
 */
@Override
public final void onLinkAttach(final ProtonConnection con, final ProtonSender sender,
        final ResourceIdentifier replyToAddress) {
    if (replyToAddress.getResourceId() == null) {
        logger.debug(
                "link target provided in client's link ATTACH must not be null, but must match pattern \"{}/<tenant>/<reply-address>\" instead",
                getName());
        sender.setCondition(condition(AmqpError.INVALID_FIELD.toString(), String.format(
                "link target must not be null but must have the following format %s/<tenant>/<reply-address>",
                getName())));
        sender.close();
    } else {
        logger.debug("establishing sender link with client [{}]", MessageHelper.getLinkName(sender));
        final MessageConsumer<JsonObject> replyConsumer = vertx.eventBus().consumer(replyToAddress.toString(),
                message -> {
                    // TODO check for correct session here...?
                    logger.trace("forwarding reply to client: {}", message.body());
                    final Message amqpReply = getAmqpReply(message);
                    sender.send(amqpReply);
                });

        sender.closeHandler(senderClosed -> {
            replyConsumer.unregister();
            senderClosed.result().close();
            final String linkName = MessageHelper.getLinkName(sender);
            logger.debug("receiver closed link [{}], removing associated event bus consumer [{}]", linkName,
                    replyConsumer.address());
        });

        sender.setQoS(ProtonQoS.AT_LEAST_ONCE).open();
    }
}

From source file:org.eclipse.hono.service.credentials.CredentialsEndpoint.java

License:Open Source License

@Override
public void onLinkAttach(final ProtonSender sender, final ResourceIdentifier targetResource) {
    if (targetResource.getResourceId() == null) {
        logger.debug(//from  w w w . ja  v  a 2s .  co m
                "link target provided in client's link ATTACH must not be null, but must match pattern \"credentials/<tenant>/<reply-address>\" instead");
        sender.setCondition(condition(AmqpError.INVALID_FIELD.toString(),
                "link target must not be null but must have the following format credentials/<tenant>/<reply-address>"));
        sender.close();
    } else {
        logger.debug("establishing sender link with client [{}]", MessageHelper.getLinkName(sender));
        final MessageConsumer<JsonObject> replyConsumer = vertx.eventBus().consumer(targetResource.toString(),
                message -> {
                    // TODO check for correct session here...?
                    logger.trace("forwarding reply to client: {}", message.body());
                    final Message amqpReply = CredentialsConstants.getAmqpReply(message);
                    sender.send(amqpReply);
                });

        sender.closeHandler(senderClosed -> {
            replyConsumer.unregister();
            senderClosed.result().close();
            final String linkName = MessageHelper.getLinkName(sender);
            logger.debug("receiver closed link [{}], removing associated event bus consumer [{}]", linkName,
                    replyConsumer.address());
        });

        sender.setQoS(ProtonQoS.AT_LEAST_ONCE).open();
    }
}

From source file:org.eclipse.hono.service.registration.RegistrationEndpoint.java

License:Open Source License

@Override
public void onLinkAttach(final ProtonSender sender, final ResourceIdentifier targetResource) {
    /* note: we "misuse" deviceId part of the resource as reply address here */
    if (targetResource.getResourceId() == null) {
        logger.debug(//from ww w .j a v a2 s.c om
                "link target provided in client's link ATTACH does not match pattern \"registration/<tenant>/<reply-address>\"");
        sender.setCondition(condition(AmqpError.INVALID_FIELD.toString(),
                "link target must have the following format registration/<tenant>/<reply-address>"));
        sender.close();
    } else {
        logger.debug("establishing sender link with client [{}]", MessageHelper.getLinkName(sender));
        final MessageConsumer<JsonObject> replyConsumer = vertx.eventBus().consumer(targetResource.toString(),
                message -> {
                    // TODO check for correct session here...?
                    logger.trace("forwarding reply to client: {}", message.body());
                    final Message amqpReply = RegistrationConstants.getAmqpReply(message);
                    sender.send(amqpReply);
                });

        sender.closeHandler(senderClosed -> {
            replyConsumer.unregister();
            senderClosed.result().close();
            final String linkName = MessageHelper.getLinkName(sender);
            logger.debug("receiver closed link [{}], removing associated event bus consumer [{}]", linkName,
                    replyConsumer.address());
        });

        sender.setQoS(ProtonQoS.AT_LEAST_ONCE).open();
    }
}