List of usage examples for io.vertx.core.eventbus MessageConsumer unregister
Future<Void> unregister();
From source file:com.consol.citrus.vertx.endpoint.VertxConsumer.java
License:Apache License
@Override public Message receive(TestContext context, long timeout) { if (log.isDebugEnabled()) { log.debug(//from w w w . j a v a 2s. c o m "Receiving message on Vert.x event bus address: '" + endpointConfiguration.getAddress() + "'"); } VertxSingleMessageHandler vertxMessageHandler = new VertxSingleMessageHandler(); MessageConsumer<Object> vertxConsumer = vertx.eventBus().consumer(endpointConfiguration.getAddress(), vertxMessageHandler); try { long timeLeft = timeout; Message message = endpointConfiguration.getMessageConverter() .convertInbound(vertxMessageHandler.getMessage(), endpointConfiguration, context); while (message == null && timeLeft > 0) { timeLeft -= endpointConfiguration.getPollingInterval(); if (RETRY_LOG.isDebugEnabled()) { RETRY_LOG.debug(String.format( "Waiting for message on Vert.x event bus address '%s' - retrying in %s ms", endpointConfiguration.getAddress(), (timeLeft > 0 ? endpointConfiguration.getPollingInterval() : endpointConfiguration.getPollingInterval() + timeLeft))); } try { Thread.sleep(timeLeft > 0 ? endpointConfiguration.getPollingInterval() : endpointConfiguration.getPollingInterval() + timeLeft); } catch (InterruptedException e) { RETRY_LOG.warn("Thread interrupted while waiting for message on Vert.x event bus", e); } message = endpointConfiguration.getMessageConverter() .convertInbound(vertxMessageHandler.getMessage(), endpointConfiguration, context); } if (message == null) { throw new ActionTimeoutException( "Action timed out while receiving message on Vert.x event bus address '" + endpointConfiguration.getAddress() + "'"); } log.info("Received message on Vert.x event bus address: '" + endpointConfiguration.getAddress() + "'"); context.onInboundMessage(message); return message; } finally { vertxConsumer.unregister(); } }
From source file:com.groupon.vertx.memcache.stream.MemcacheSocketHandler.java
License:Apache License
public void handle(Long time) { final Handler<Long> currentHandler = this; log.trace("handle", "establishSocket", new String[] { "eventBusAddress", "server", "delay" }, eventBusAddress, server.getServer(), currentDelay); netClient.connect(server.getPort(), server.getHost(), new Handler<AsyncResult<NetSocket>>() { public void handle(AsyncResult<NetSocket> socket) { log.trace("establishSocket", "handle", new String[] { "eventBusAddress", "server", "status" }, eventBusAddress, server.getServer(), socket.succeeded()); if (socket.succeeded()) { log.trace("establishSocket", "success"); currentDelay = delayFactor; final NetSocket netSocket = socket.result(); final MemcacheCommandHandler memcacheHandler = new MemcacheCommandHandler(netSocket); final MessageConsumer<MemcacheCommand> consumer = vertx.eventBus().consumer(eventBusAddress, memcacheHandler); netSocket.exceptionHandler(ex -> { log.error("establishSocket", "exception", "unknown", ex); consumer.unregister(); memcacheHandler.finish(); });//from w w w. j a v a 2s . c o m netSocket.closeHandler(message -> { log.warn("establishSocket", "socketClosed"); consumer.unregister(); memcacheHandler.finish(); vertx.setTimer(currentDelay, currentHandler); }); } else { if (socket.result() != null) { log.warn("establishSocket", "closeSocket"); socket.result().close(); } currentDelay = Math.min(currentDelay * 2, MAXIMUM_DELAY); log.warn("establishSocket", "failed", new String[] { "eventBusAddress", "server" }, eventBusAddress, server.getServer()); vertx.setTimer(currentDelay, currentHandler); } } }); }
From source file:com.groupon.vertx.redis.RedisSocketHandler.java
License:Apache License
public void handle(Long time) { final Handler<Long> currentHandler = this; netClient.connect(port, host, socket -> { if (socket.succeeded()) { log.trace("establishSocket", "success"); currentDelay = delayFactor;/* www .j av a2 s . c o m*/ final NetSocket netSocket = socket.result(); final RedisCommandHandler redisHandler = new RedisCommandHandler(netSocket); final MessageConsumer<JsonObject> consumer = vertx.eventBus().consumer(eventBusAddress, redisHandler); netSocket.exceptionHandler(ex -> { log.error("establishSocket", "exception", "unknown", ex); consumer.unregister(); redisHandler.finish(); }); netSocket.closeHandler(message -> { log.warn("establishSocket", "socketClosed"); consumer.unregister(); redisHandler.finish(); vertx.setTimer(currentDelay, currentHandler); }); } else { if (socket.result() != null) { log.warn("establishSocket", "closeSocket"); socket.result().close(); } currentDelay = Math.min(currentDelay * 2, MAXIMUM_DELAY); log.warn("establishSocket", "failed", new String[] { "eventBusAddress", "server" }, eventBusAddress, host); vertx.setTimer(currentDelay, currentHandler); } }); }
From source file:eu.rethink.mn.pipeline.PipeSession.java
License:Apache License
/** Detach an address listener. * @param address Any address/*from www . j av a 2 s .c om*/ */ public void removeListener(String address) { logger.info("REMOVE-LISTENER(" + runtimeSessionURL + "): " + address); final MessageConsumer<Object> value = consumers.remove(address); if (value != null) { value.unregister(); } }
From source file:eu.rethink.mn.pipeline.PipeSession.java
License:Apache License
void close() { registry.sessions.remove(runtimeSessionURL); for (String url : urls) { registry.urlSpace.remove(url);/*from ww w . j a v a2s. c om*/ } for (MessageConsumer<Object> value : consumers.values()) { value.unregister(); } // consumers.clear(); or urls.clear(); no need to do this, session will be discarded }
From source file:io.engagingspaces.graphql.events.impl.SchemaMessageConsumers.java
License:Open Source License
public void close() { for (Iterator<Map.Entry<String, MessageConsumer<JsonObject>>> it = messageConsumers.entrySet() .iterator(); it.hasNext();) { MessageConsumer consumer = it.next().getValue(); if (consumer.isRegistered()) { consumer.unregister(); }/* w w w .j a va2 s. co m*/ it.remove(); } consumerRegistrations.clear(); }
From source file:io.nonobot.core.client.impl.BotClientImpl.java
License:Apache License
@Override public void receiveMessage(ReceiveOptions options, String message, Handler<AsyncResult<String>> replyHandler) { String replyAddress = UUID.randomUUID().toString(); Future<String> reply = Future.future(); reply.setHandler(replyHandler);//from w w w.j a v a 2 s . co m MessageConsumer<String> consumer = vertx.eventBus().consumer(replyAddress); consumer.handler(msg -> { String content = msg.body(); if (content != null && !reply.isComplete()) { if (msg.replyAddress() != null) { msg.reply(null); } reply.complete(content); consumer.unregister(); } else { if (msg.replyAddress() != null) { msg.fail(0, "Already replied"); } } }); consumer.completionHandler(ar -> { if (ar.succeeded()) { Matcher botMatcher = botPattern.matcher(message); JsonObject msg = new JsonObject().put("replyAddress", replyAddress); msg.put("chatId", options.getChatId()); if (botMatcher.find()) { msg.put("respond", true); msg.put("content", botMatcher.group(1)); } else { msg.put("respond", false); msg.put("content", message); } vertx.eventBus().publish(inboundAddress, msg); vertx.setTimer(options.getTimeout(), timerID -> { if (!reply.isComplete()) { consumer.unregister(); reply.fail(new Exception("timeout")); } }); } else { replyHandler.handle(Future.failedFuture(ar.cause())); } }); }
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 av a2 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(/* w ww.j a 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(/*ww w .j a v a 2s.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(); } }