List of usage examples for com.rabbitmq.client DefaultConsumer DefaultConsumer
public DefaultConsumer(Channel channel)
From source file:io.github.mattcarrier.metrics.transport.rabbit.RabbitMQRule.java
License:Apache License
@Override protected void before() throws Throwable { ConnectionFactory factory = new ConnectionFactory(); factory.setUri(connectionUri);/*www .j a va 2 s . c o m*/ conn = factory.newConnection(); channel = conn.createChannel(); // create the queue and register the consumer channel.queueDeclare(QUEUE_NAME, false, false, true, null); channel.basicConsume(QUEUE_NAME, true, "metrics-rabbit-consumer", new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { if (!isExpecting || !QUEUE_NAME.equals(envelope.getRoutingKey())) { return; } messages.add(body); if (messages.size() == expected) { isExpecting = false; } } }); }
From source file:io.opentracing.contrib.rabbitmq.TracingTest.java
License:Apache License
@Test public void basicConsume() throws Exception { String exchangeName = "basicConsumeExchange"; String queueName = "basicConsumeQueue"; String routingKey = "#"; channel.exchangeDeclare(exchangeName, "direct", true); channel.queueDeclare(queueName, true, false, false, null); channel.queueBind(queueName, exchangeName, routingKey); byte[] messageBodyBytes = "Hello, world!".getBytes(); channel.basicPublish(exchangeName, routingKey, null, messageBodyBytes); final CountDownLatch latch = new CountDownLatch(1); channel.basicConsume(queueName, false, new DefaultConsumer(channel) { @Override/*from ww w. j av a2 s . c o m*/ public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { long deliveryTag = envelope.getDeliveryTag(); channel.basicAck(deliveryTag, false); latch.countDown(); } }); latch.await(30, TimeUnit.SECONDS); List<MockSpan> finishedSpans = mockTracer.finishedSpans(); int tries = 10; while (tries > 0 && finishedSpans.size() < 2) { TimeUnit.SECONDS.sleep(1L); finishedSpans = mockTracer.finishedSpans(); tries--; } assertEquals(2, finishedSpans.size()); checkSpans(finishedSpans); assertNull(mockTracer.activeSpan()); }
From source file:io.qdb.server.input.RabbitMQInputHandler.java
License:Apache License
@Override public void start(final Sink sink) throws Exception { this.sink = sink; final Channel c = ensureChannel(); c.basicConsume(queue, autoAck, "qdb:" + inputPath, new DefaultConsumer(c) { @Override// w w w . j a va 2 s . co m public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { boolean ok = false; try { sink.append(envelope.getRoutingKey(), body); if (!autoAck) c.basicAck(envelope.getDeliveryTag(), false); ok = true; } catch (Exception e) { sink.error(e); } finally { if (!ok) { try { // todo should probably sit on the message for a bit before nacking to prevent storm c.basicNack(envelope.getDeliveryTag(), false, true); } catch (IOException e) { log.debug("Error nacking message: " + e, e); } } } } }); }
From source file:it.av.fac.messaging.rabbitmq.RabbitMQClient.java
/** * Instantiates a RabbitMQ connection and binds it to two queues. * * @param connWrapper The object that contains a connection to the RabbitMQ * server.//from w w w. j a v a2 s.com * @param queueOut The queue where this connection should send messages to. * @param queueIn The queue where this connection should read messages from. * @param clientKey The unique client key to be used for routing messages. * @param handler The handler to call when new messages are available in the * queueIn. Replaces the receive method. * @throws Exception If there is a connection issue to the RabbitMQ server. */ public RabbitMQClient(RabbitMQConnectionWrapper connWrapper, String queueIn, String queueOut, String clientKey, IClientHandler<byte[]> handler) throws Exception { this.queueIn = queueIn + "." + clientKey; this.queueOut = queueOut; this.routingKeyIn = this.queueIn; this.routingKeyOut = this.queueOut + "." + clientKey; this.conn = connWrapper.getConnection(); this.channel = RabbitMQChannelPool.createChannel(conn, this.queueIn, this.queueOut, this.routingKeyIn, this.routingKeyOut); this.channel.basicConsume(this.queueIn, true, new DefaultConsumer(this.channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { handler.handle(body); } }); }
From source file:it.av.fac.messaging.rabbitmq.RabbitMQServer.java
/** * Instantiates a RabbitMQ connection to receive a request only. Cannot send * messages./* w w w. j a va 2 s . co m*/ * * @param connWrapper The object that contains a connection to the RabbitMQ * server. * @param queueIn The queue where this connection should read messages from. * @param handler The handler to call when new messages are available in the * queueIn. Replaces the receive method. * @throws Exception If there is a connection issue to the RabbitMQ server. */ public RabbitMQServer(RabbitMQConnectionWrapper connWrapper, String queueIn, IServerHandler<byte[], String> handler) throws Exception { this.queueIn = queueIn; this.queueOut = null; this.routingKeyIn = queueIn + ".#"; this.routingKeyOut = null; this.canSend = false; this.conn = connWrapper.getConnection(); this.channel = RabbitMQChannelPool.createChannel(conn, this.queueIn, this.queueOut, this.routingKeyIn, this.routingKeyOut); this.channel.basicConsume(this.queueIn, true, new DefaultConsumer(this.channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String clientKey = envelope.getRoutingKey().substring(queueIn.length() + 1); handler.handle(body, clientKey); } }); }
From source file:it.polimi.tower4clouds.observers.hdb.manager.Queue.java
License:Apache License
private void internalAddSubscription(MessageParser pars) throws IOException { boolean autoAck = false; if (this.parser != null) { internalRemoveSubscription();//from ww w . j a v a2s .c o m } this.parser = pars; channel.basicConsume(queueName, autoAck, queueName + "@" + queueHost, new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { long deliveryTag = envelope.getDeliveryTag(); String message = new String(body); logger.debug("Message received:\n{}", message); parser.parseMessage(message); channel.basicAck(deliveryTag, false); } }); }
From source file:loanbroker.Aggregator.java
public void reciveFromNormalizer(Hashtable<String, Message> messageFroumBankList, Hashtable<String, Message> messagesFromNormalizer, ArrayList<Message> foundMessages) throws IOException, TimeoutException, Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(hostName);/* www.ja v a 2s. c om*/ factory.setPort(5672); factory.setUsername("student"); factory.setPassword("cph"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); // channel.exchangeDeclare(inputEXCHANGE_NAME, "direct"); String queueName = channel.queueDeclare().getQueue(); channel.queueBind(queueName, ExchangeName.GLOBAL, RoutingKeys.NormalizerToAggregator); System.out.println(" [*] Waiting for messages on " + ExchangeName.GLOBAL + RoutingKeys.NormalizerToAggregator + ". To exit press CTRL+C"); Consumer consumer = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String m = new String(body, "UTF-8"); Gson gson = new GsonBuilder().create(); LoanResponse lp = gson.fromJson(m, LoanResponse.class); Message fm = new Message("" + lp.getSsn(), (int) lp.getInterestRate(), 0, lp.getBank()); messagesFromNormalizer.put(lp.getCorrelationId(), fm); System.out.println(" [x] Received '" + envelope.getRoutingKey() + "':'" + fm.toString() + "'"); try { checkLoanMessages(messageFroumBankList, messagesFromNormalizer, foundMessages); } catch (InterruptedException ex) { Logger.getLogger(Aggregator.class.getName()).log(Level.SEVERE, null, ex); } catch (TimeoutException ex) { Logger.getLogger(Aggregator.class.getName()).log(Level.SEVERE, null, ex); } catch (Exception ex) { Logger.getLogger(Aggregator.class.getName()).log(Level.SEVERE, null, ex); } } }; channel.basicConsume(queueName, true, consumer); }
From source file:loanbroker.Aggregator.java
public void reciveFromRecieptList(Hashtable<String, Message> messagesFromBankList) throws IOException, TimeoutException, Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(hostName);// w w w. jav a 2 s .c o m factory.setPort(5672); factory.setUsername("student"); factory.setPassword("cph"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(ExchangeName.GLOBAL, "direct"); String queueName = channel.queueDeclare().getQueue(); channel.queueBind(queueName, ExchangeName.GLOBAL, RoutingKeys.RecipientListToAggregator); System.out.println(" [*] Waiting for messages on " + ExchangeName.GLOBAL + RoutingKeys.RecipientListToAggregator + ".. To exit press CTRL+C"); Consumer consumer = new DefaultConsumer(channel) { public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String m = new String(body, "UTF-8"); // System.out.println("reciveFromRecieptList" + m); String p = properties.getCorrelationId(); if (p != null) { //send to translator Gson g = new Gson(); Message fm = g.fromJson(m, Message.class); if (fm.getBanks() != null) { Message k = new Message(fm.getSsn(), fm.getCreditScore(), fm.getLoanAmount(), fm.getLoanDuration()); k.setBanks(fm.getBanks()); messagesFromBankList.put(p, k); } System.out.println(" [x] Received '" + envelope.getRoutingKey() + "':'" + fm.toString() + "'"); // System.out.println(" [x] Received '" + envelope.getRoutingKey() + "':'" + m + "'"); } else { System.out.println("No correlationId"); } } }; channel.basicConsume(queueName, true, consumer); }
From source file:loanbroker.Result.java
public void reciveResultFromAggregator() throws IOException, TimeoutException, Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(hostName);/*from w w w . jav a2 s .c om*/ factory.setPort(5672); factory.setUsername("student"); factory.setPassword("cph"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); String queueName = channel.queueDeclare().getQueue(); channel.queueBind(queueName, ExchangeName.GLOBAL, RoutingKeys.Result); System.out.println(" [*] Waiting for messages on " + ExchangeName.GLOBAL + RoutingKeys.Result + ".. To exit press CTRL+C"); Consumer consumer = new DefaultConsumer(channel) { public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String m = new String(body, "UTF-8"); // System.out.println("reciveFromRecieptList"+ m); String p = properties.getCorrelationId(); Gson gson = new GsonBuilder().create(); LoanResponse fm = gson.fromJson(m, LoanResponse.class); // System.out.println(" [x] Received '" + envelope.getRoutingKey() + "':'" + fm.toString() + "'"); // System.out.println(" [x] Received '" + envelope.getRoutingKey() + "':'" + m + "'"); System.out.println("**** [info]**** You're best interestrate is '" + fm.getInterestRate() + "' on the CPR number. '" + fm.getSsn() + "' at '" + fm.getBank() + "'"); } }; channel.basicConsume(queueName, true, consumer); }
From source file:mapas.Mapas.java
public static void main(String[] args) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); factory.setPassword("test"); factory.setUsername("test"); final Connection connection = factory.newConnection(); final Channel channel = connection.createChannel(); channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null); System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); channel.basicQos(1);/*w w w .j av a2s. c om*/ final Consumer consumer = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String message = new String(body, "UTF-8"); System.out.println(" [x] Received '" + message + "'"); try { doWork(message); } catch (Exception e) { System.out.println(e.getMessage()); } System.out.println(" [x] Done"); channel.basicAck(envelope.getDeliveryTag(), false); } }; channel.basicConsume(TASK_QUEUE_NAME, false, consumer); }