List of usage examples for com.rabbitmq.client DefaultConsumer DefaultConsumer
public DefaultConsumer(Channel channel)
From source file:org.atmosphere.plugin.rabbitmq.RabbitMQBroadcaster.java
License:Apache License
void restartConsumer() { try {//from w ww. java 2 s . c om final String id = getID(); if (consumerTag != null) { logger.debug("Delete consumer {}", consumerTag); channel.basicCancel(consumerTag); consumerTag = null; } if (queueName != null) { logger.debug("Delete queue {}", queueName); channel.queueUnbind(queueName, exchangeName, id); channel.queueDelete(queueName); queueName = null; } queueName = channel.queueDeclare().getQueue(); channel.queueBind(queueName, exchangeName, id); logger.info("Create AMQP consumer on queue {}, for routing key {}", queueName, id); DefaultConsumer queueConsumer = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { // Not for us. if (!envelope.getRoutingKey().equalsIgnoreCase(id)) return; String message = new String(body); try { Object newMsg = filter(message); // if newSgw == null, that means the message has been filtered. if (newMsg != null) { deliverPush(new Entry(newMsg, new BroadcasterFuture<Object>(newMsg, RabbitMQBroadcaster.this), message), true); } } catch (Throwable t) { logger.error("failed to push message: " + message, t); } } }; consumerTag = channel.basicConsume(queueName, true, queueConsumer); logger.info("Consumer " + consumerTag + " for queue {}, on routing key {}", queueName, id); } catch (Throwable ex) { String msg = "Unable to initialize RabbitMQBroadcaster"; logger.error(msg, ex); throw new IllegalStateException(msg, ex); } }
From source file:org.atmosphere.samples.pubsub.rabbitmq.RabbitMQBroadcaster.java
License:Apache License
void restartConsumer() { if (!isInitialized) { initialize(getID(), config);//ww w. jav a 2 s . c o m } try { final String id = getID(); if (consumerTag != null) { logger.debug("Delete consumer {}", consumerTag); channel.basicCancel(consumerTag); consumerTag = null; } if (queueName != null) { logger.debug("Delete queue {}", queueName); channel.queueUnbind(queueName, exchangeName, id); channel.queueDelete(queueName); queueName = null; } queueName = channel.queueDeclare().getQueue(); channel.queueBind(queueName, exchangeName, id); logger.info("Create AMQP consumer on queue {}, for routing key {}", queueName, id); DefaultConsumer queueConsumer = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { // Not for us. if (!envelope.getRoutingKey().equalsIgnoreCase(id)) return; String message = new String(body); try { Object newMsg = filter(message); // if newSgw == null, that means the message has been filtered. if (newMsg != null) { deliverPush(new Deliver(newMsg, new BroadcasterFuture<Object>(newMsg), message), true); } } catch (Throwable t) { logger.error("failed to push message: " + message, t); } } }; consumerTag = channel.basicConsume(queueName, true, queueConsumer); logger.info("Consumer " + consumerTag + " for queue {}, on routing key {}", queueName, id); } catch (Throwable ex) { String msg = "Unable to initialize RabbitMQBroadcaster"; logger.error(msg, ex); throw new IllegalStateException(msg, ex); } }
From source file:org.atmosphere.samples.websockethub.RabbitMQRouter.java
License:Apache License
private void routeIn() { try {/*from ww w . j av a2 s .c o m*/ if (consumerTag != null) { logger.debug("Delete consumer {}", consumerTag); channel.basicCancel(consumerTag); consumerTag = null; } if (queueName != null) { logger.debug("Delete queue {}", queueName); for (String k : amqRoutingKey) { channel.queueUnbind(queueName, exchangeName, k); } channel.queueDelete(queueName); queueName = null; } queueName = channel.queueDeclare().getQueue(); for (String k : amqRoutingKey) { channel.queueBind(queueName, exchangeName, k); } logger.info("Create AMQP consumer on queue {}, for routing key {}", queueName, amqRoutingKey); DefaultConsumer queueConsumer = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { // Not for us. // if (!amqRoutingKey.contains(envelope.getRoutingKey())) { // logger.debug("Invalid RoutingKey {}. Available one {}", envelope.getRoutingKey(), amqRoutingKey); // return; // } try { JsonNode rootNode = oMapper.readValue(body, JsonNode.class); String key = rootNode.get("routingKey").asText(); if (key == null) { logger.error("Missing key {}", new String(body)); return; } String message = rootNode.get("message").asText(); if (message == null) { logger.error("Missing message {}", new String(body)); return; } if (message.isEmpty()) { message = rootNode.get("message").toString(); } // Retrieve the Broadcaster associated with this message Broadcaster b = mapper.map(key, broadcasters); if (b == null) { logger.warn("No Broadcaster Found for Message {}", message); } else { b.broadcast(message); } } catch (Exception ex) { logger.error("", ex); logger.error("Unable to decode {}", new String(body)); } } }; consumerTag = channel.basicConsume(queueName, true, queueConsumer); logger.info("Consumer " + consumerTag + " for queue {}, on routing key {}", queueName, amqRoutingKey); } catch (Throwable ex) { String msg = "Unable to initialize RabbitMQBroadcaster"; logger.error(msg, ex); throw new IllegalStateException(msg, ex); } }
From source file:org.axonframework.eventhandling.amqp.spring.ExtendedMessageListenerContainerTest.java
License:Apache License
@Test public void testExclusiveByDefault() throws IOException { testSubject.setConnectionFactory(connectionFactory); testSubject.getConnectionFactory().createConnection().createChannel(true).basicConsume("test", new DefaultConsumer(channel)); verify(channel, never()).basicConsume(isA(String.class), isA(Consumer.class)); verify(channel, never()).basicConsume(isA(String.class), anyBoolean(), isA(Consumer.class)); verify(channel, never()).basicConsume(isA(String.class), anyBoolean(), anyString(), isA(Consumer.class)); verify(channel, never()).basicConsume(isA(String.class), anyBoolean(), anyString(), anyBoolean(), eq(false), anyMap(), isA(Consumer.class)); verify(channel).basicConsume(isA(String.class), anyBoolean(), anyString(), anyBoolean(), eq(true), anyMap(), isA(Consumer.class)); }
From source file:org.axonframework.eventhandling.amqp.spring.ExtendedMessageListenerContainerTest.java
License:Apache License
@Test public void testNonExclusive() throws IOException { testSubject.setConnectionFactory(connectionFactory); testSubject.setExclusive(false);//from ww w . j av a 2 s .c o m testSubject.getConnectionFactory().createConnection().createChannel(true).basicConsume("test", new DefaultConsumer(channel)); verify(channel, never()).basicConsume(isA(String.class), anyBoolean(), anyString(), anyBoolean(), eq(true), anyMap(), isA(Consumer.class)); }
From source file:org.ballerinalang.messaging.rabbitmq.nativeimpl.channel.listener.Start.java
License:Open Source License
/** * Receive messages from the RabbitMQ server. * * @param resource Ballerina resource function. * @param channel RabbitMQ Channel object. * @param queueName Name of the queue messages are consumed from. * @param autoAck True if the server should consider messages acknowledged once delivered; * false if the server should expect explicit acknowledgements. *///from w w w .j a v a 2 s . c o m private void receiveMessages(Resource resource, Channel channel, String queueName, boolean autoAck) { try { channel.basicConsume(queueName, autoAck, new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { dispatchMessage(resource, body, channel, envelope.getDeliveryTag(), autoAck); } }); } catch (IOException exception) { throw new BallerinaException(exception); } }
From source file:org.eclipse.flux.client.impl.RabbitMQMessageConnector.java
License:Open Source License
private String createInbox() throws IOException { DeclareOk ok = this.channel.queueDeclare("", /*durable*/ false, /*exclusive*/false, /*autoDelete*/true, null);//ww w . j a va2 s .co m final String inbox = ok.getQueue(); console.log("Inbox created: " + inbox); channel.basicConsume(inbox, new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body) throws IOException { try { JSONObject obj = JSON.parse(body); if (!isSelfOriginated(obj)) { handleIncomingMessage(obj.getString("type"), obj.getJSONObject("data")); } } catch (Exception e) { console.log(e); } } /** * Tests whether an incoming message originated from the same MessageConnector that * is receiving it. (Such messages are skipped in keeping with how socketio does the same * thing). */ private boolean isSelfOriginated(JSONObject obj) { try { String origin = obj.getString("origin"); return inbox.equals(origin); } catch (Exception e) { console.log(e); } return false; } }); return inbox; }
From source file:org.eclipse.hono.client.AmqpConnectorClient.java
License:Open Source License
private void declareBindQueue() throws IOException { channel = connectionManager.getChannel(); channel.queueDeclare(clientId, true, false, false, emptyMap()); LOGGER.info("declared a queue with subject " + clientId); channel.queueBind(clientId, OUT, clientId); LOGGER.info("bind queue to routingkey " + clientId); consumerTag = channel.basicConsume(clientId, new DefaultConsumer(channel) { @Override//from w w w .j a v a 2s . c om public void handleDelivery(final String consumerTag, final Envelope envelope, final AMQP.BasicProperties properties, final byte[] body) throws IOException { final String routingKey = envelope.getRoutingKey(); getChannel().basicAck(envelope.getDeliveryTag(), false); final String topic = properties.getHeaders().get(TOPIC_HEADER).toString(); LOGGER.debug("{} received message for topic '{}'", routingKey, topic); delegateToTopicConsumer(properties, body, topic); } }); }
From source file:org.eclipse.hono.dispatcher.amqp.DefaultAmqpHelper.java
License:Open Source License
@Override public String consume(final String queue, final Consumer<QueueingConsumer.Delivery> consumer) { try {// ww w .j av a2 s . c o m DefaultAmqpHelper.LOGGER.debug("Subscribing to {} on channel {}.", queue, channel.getChannelNumber()); return channel.basicConsume(queue, false, new DefaultConsumer(channel) { @Override public void handleDelivery(final String consumerTag, final Envelope envelope, final AMQP.BasicProperties properties, final byte[] body) throws IOException { try { DefaultAmqpHelper.LOGGER.trace("Received delivery {} on channel {}.", envelope, channel.getChannelNumber()); final QueueingConsumer.Delivery delivery = new QueueingConsumer.Delivery(envelope, properties, body); consumer.accept(delivery); } catch (final Exception e) { DefaultAmqpHelper.LOGGER.warn("Exception in consumer: {}", e); } } }); } catch (final IOException e) { throw new AmqpException("Could not subscribe to queue " + queue, e); } }
From source file:org.geoserver.notification.support.Receiver.java
License:Open Source License
private DefaultConsumer newConsumer(Channel channel) { return new DefaultConsumer(channel) { @Override// ww w .j a v a 2s. c om public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { service.manage(body); } }; }