List of usage examples for com.rabbitmq.client Channel basicConsume
String basicConsume(String queue, boolean autoAck, Consumer callback) throws IOException;
From source file:com.nesscomputing.amqp.QueueConsumer.java
License:Apache License
@Override protected void connectCallback(final Channel channel) throws IOException { super.connectCallback(channel); if (getConfig().isDeclaring()) { channel.queueDeclare(getName(), getConfig().isDurable(), getConfig().isExclusive(), getConfig().isAutoDelete(), null); }// w ww . ja v a 2s . c om channel.basicConsume(getName(), false, getConsumer()); }
From source file:com.netcore.hsmart.dlrconsumers.DlrConsumer1000.java
/** * @param args the command line arguments * @throws java.lang.Exception// w w w . ja v a2s . co m */ public static void main(String[] args) throws Exception { /** * Set properties at runtime */ System.setProperty("dlrconsumer_logfile", "557_dlr_consumer.log"); AppConstants.loadAppConfig(); final String queueName = AppConstants.getDlrReceiverQueuePrefix() + GATEWAY_ID; final String exchangeName = AppConstants.getDlrReceiverExchangeName(); final String routingKey = GATEWAY_ID; Logger logger = LoggerFactory.getLogger(DlrConsumer1000.class); try { Connection connection = AppConstants.getRabbitMqObject().newConnection(); connection.addShutdownListener(new ShutdownListener() { @Override public void shutdownCompleted(ShutdownSignalException cause) { //throw new UnsupportedOperationException("Not supported yet."); if (cause.isHardError()) { Connection conn; conn = (Connection) cause.getReference(); if (!cause.isInitiatedByApplication()) { Method reason = cause.getReason(); logger.info("REASON:" + reason.toString()); } } else { Channel ch = (Channel) cause.getReference(); logger.info("NO HARDSHUTDOWN"); } } }); Channel channel = connection.createChannel(); channel.exchangeDeclare(exchangeName, "direct"); channel.queueDeclare(queueName, true, false, false, null); channel.queueBind(queueName, exchangeName, routingKey); channel.basicQos(1000); //channel.addShutdownListener(listener); logger.info(" [*] Waiting for messages from gateway " + GATEWAY_ID + ". To exit press CTRL+C"); Consumer consumer; consumer = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { Map<String, String> dataToPost = new HashMap<>(); String payload = new String(body, "UTF-8"); logger.info("REF_ID:" + dataToPost.get("ref_id") + "|START+++++++++++++++"); logger.info("REF_ID:" + dataToPost.get("ref_id") + "|PAYLOAD:" + payload); String payloadParts[] = payload.split(AppConstants.getPayloadGroupSeparator()); for (String payloadPart : payloadParts) { String s[] = payloadPart.split(AppConstants.getPayloadKVSeparator()); //check if any parameter is empty if (s.length == 2) { dataToPost.put(s[0], s[1]); } else { logger.info("REF_ID:" + dataToPost.get("ref_id") + "|EMPTY_PARAM:" + s[0]); dataToPost.put(s[0], null); } } long deliveryTag = envelope.getDeliveryTag(); if (invokeApiCall(dataToPost)) { channel.basicAck(deliveryTag, false); } else { channel.basicNack(deliveryTag, false, true); } /** * release memory */ logger.info("REF_ID:" + dataToPost.get("ref_id") + "|END-----------------"); dataToPost.clear(); payloadParts = null; } }; String cTag = channel.basicConsume(queueName, false, consumer); logger.info("CONSUMER TAG : " + cTag); } catch (IOException | TimeoutException e) { logger.error("RMQ_ERROR:" + e.getMessage()); } }
From source file:com.netcore.hsmart.smsconsumers.SmsConsumer1000.java
/** * @param args the command line arguments * @throws java.lang.Exception//from w w w . jav a 2 s. c om */ public static void main(String[] args) throws Exception { /** * Set properties at runtime */ System.setProperty("smsconsumer_logfile", GATEWAY_ID + "_sms_consumer.log"); AppConstants.loadAppConfig(); final String queueName = AppConstants.getSmsReceiverQueuePrefix() + GATEWAY_ID; final String exchangeName = AppConstants.getSmsReceiverExchangeName(); final String routingKey = GATEWAY_ID; Logger logger = LoggerFactory.getLogger(SmsConsumer1000.class); try { Connection connection = AppConstants.getRabbitMqObject().newConnection(); connection.addShutdownListener(new ShutdownListener() { @Override public void shutdownCompleted(ShutdownSignalException cause) { //throw new UnsupportedOperationException("Not supported yet."); if (cause.isHardError()) { Connection conn; conn = (Connection) cause.getReference(); if (!cause.isInitiatedByApplication()) { Method reason = cause.getReason(); logger.info("REASON:" + reason.toString()); } } else { Channel ch = (Channel) cause.getReference(); logger.info("NO HARDSHUTDOWN"); } } }); Channel channel = connection.createChannel(); channel.exchangeDeclare(exchangeName, "direct"); channel.queueDeclare(queueName, true, false, false, null); channel.queueBind(queueName, exchangeName, routingKey); channel.basicQos(1000); //channel.addShutdownListener(listener); logger.info(" [*] Waiting for messages from gateway " + GATEWAY_ID + ". To exit press CTRL+C"); Consumer consumer; consumer = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { Map<String, String> dataToPost = new HashMap<>(); String payload = new String(body, "UTF-8"); String payloadParts[] = payload.split(AppConstants.getPayloadGroupSeparator()); for (String payloadPart : payloadParts) { String s[] = payloadPart.split(AppConstants.getPayloadKVSeparator()); dataToPost.put(s[0], s[1]); } logger.info("REF_ID:" + dataToPost.get("ref_id") + "|START+++++++++++++++"); logger.info("REF_ID:" + dataToPost.get("ref_id") + "|PAYLOAD:" + payload); long deliveryTag = envelope.getDeliveryTag(); if (invokeApiCall(dataToPost)) { if (saveRequestData()) { channel.basicAck(deliveryTag, false); } else { channel.basicNack(deliveryTag, false, true); } } else { channel.basicNack(deliveryTag, false, true); } /** * release memory */ logger.info("REF_ID:" + dataToPost.get("ref_id") + "|END-----------------"); API_CALL_STATS.clear(); dataToPost.clear(); payloadParts = null; } }; String cTag = channel.basicConsume(queueName, false, consumer); logger.info("CONSUMER TAG : " + cTag); } catch (IOException | TimeoutException e) { logger.error("RMQ_ERROR:" + e.getMessage()); } }
From source file:com.pcs.test.amqp.Consumer.java
License:Open Source License
public static void main(String[] args) throws IOException, TimeoutException, ShutdownSignalException, ConsumerCancelledException, InterruptedException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("pcss-hdop04"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(QUEUE_NAME, false, false, false, null); System.out.println("listen for messages"); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(QUEUE_NAME, true, consumer); while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String msg = new String(delivery.getBody(), "UTF-8"); System.out.println("Msg received " + msg); }// w w w. ja va2 s. c om }
From source file:com.project.finalproject.Recv.java
public static void main(String[] argv) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(QUEUE_NAME, false, false, false, null); System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); Consumer consumer = new DefaultConsumer(channel) { @Override/* w w w . java 2 s .c o m*/ 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 + "'"); } }; channel.basicConsume(QUEUE_NAME, true, consumer); }
From source file:com.qt.core.util.MqConnectionUtil.java
License:Open Source License
public QueueingConsumer newConsumer(String key, int rowCount, String queueName) throws IOException { Channel channel = getChannel(key, queueName, true); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(queueName, false, consumer); return consumer; }
From source file:com.saasovation.common.port.adapter.messaging.rabbitmq.MessageConsumer.java
License:Apache License
/** * Registers aMessageListener with the channel indirectly using * a DispatchingConsumer./*from w w w .j a va 2s .com*/ * @param aMessageListener the MessageListener */ private void receiveFor(MessageListener aMessageListener) { Queue queue = this.queue(); Channel channel = queue.channel(); try { String tag = channel.basicConsume(queue.name(), this.isAutoAcknowledged(), new DispatchingConsumer(channel, aMessageListener)); this.setTag(tag); } catch (IOException e) { throw new MessageException("Failed to initiate consumer.", e); } }
From source file:com.shopwiki.roger.rpc.RequestConsumer.java
License:Apache License
public static <I> void start(RequestHandler<I, ?> handler, List<Channel> channels, String queueName, PostProcessors postProcessors) throws IOException { for (Channel channel : channels) { Consumer consumer = new RequestConsumer<I>(handler, channel, queueName, postProcessors); channel.basicConsume(queueName, false, consumer); }/*from w w w.j a va 2 s . co m*/ }
From source file:com.shopwiki.roger.rpc.RpcClient.java
License:Apache License
private RpcClient(Channel channel, Route requestRoute, Map<String, Object> responseQueueArgs, TypeReference<O> responseType, boolean exceptionsAsJson) throws IOException { this.channel = channel; this.requestRoute = requestRoute; this.responseType = responseType; this.exceptionsAsJson = exceptionsAsJson; if (exceptionsAsJson && !responseType.getType().equals(MapMessage.TYPE_REF.getType())) { throw new IllegalArgumentException( "Can't have exceptionsAsJson unless your responseType is MapMessage!"); }//from w ww. j a va2 s. c om responseQueueName = QueueUtil.declareAnonymousQueue(channel, "Roger-RpcClient", responseQueueArgs) .getQueue(); Consumer responseConsumer = new ResponseConsumer(channel); channel.basicConsume(responseQueueName, true, responseConsumer); }
From source file:com.trivago.mail.pigeon.daemon.Daemon.java
License:Apache License
/** * Main Daemon method containing the event loop. * * @param args command line args/* w w w . ja v a2s. c o m*/ * @throws java.io.IOException */ public static void main(String[] args) throws IOException { Connection conn = null; Channel channel = null; try { conn = ConnectionPool.getConnection(); channel = conn.createChannel(); boolean autoAck = false; QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(channelName, autoAck, consumer); MailFacade mailFacade = new MailFacade(); while (true) { QueueingConsumer.Delivery delivery; try { delivery = consumer.nextDelivery(); } catch (InterruptedException ie) { continue; } String jsonContent = new String(delivery.getBody()); MailTransport mailTransport = JSONParser.defaultJSONParser().parse(MailTransport.class, jsonContent); mailFacade.sendMail(mailTransport); channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); } } finally { if (channel != null) { channel.close(); } if (conn != null) { conn.close(); } } }