List of usage examples for com.rabbitmq.client DefaultConsumer DefaultConsumer
public DefaultConsumer(Channel channel)
From source file:com.ericsson.research.IoTFrameworkEndpoint.java
License:Open Source License
public void subscribe(String hostName, String StreamId, final DataPointCallback dataPointCallback) throws IOException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(hostName); // listening on port 5672 Connection connection = factory.newConnection(); final Channel channel = connection.createChannel(); final String EXCHANGE_NAME = "streams." + StreamId; channel.exchangeDeclare(EXCHANGE_NAME, "fanout"); String queueName = channel.queueDeclare().getQueue(); channel.queueBind(queueName, EXCHANGE_NAME, ""); dataPointCallback.setConnection(connection); boolean autoAck = true; channel.basicConsume(queueName, autoAck, "myConsumerTag", new DefaultConsumer(channel) { @Override//from ww w . java2 s .c o m public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { DataPointNotification notification = gson.fromJson(new String(body), DataPointNotification.class); dataPointCallback.handleNotification(notification); } }); }
From source file:com.es.dashboard.ConnectionBroker.java
@PostConstruct public void init() { bigBaite = new LinkedList<>(); readings = new ArrayList<Reading>(); ConnectionFactory factory = new ConnectionFactory(); factory.setHost("systembus"); // RabbitMQ IP Connection connection = null; try {/*from www.ja va 2 s .co m*/ connection = factory.newConnection(); } catch (IOException ex) { logger.error(ex); } catch (TimeoutException ex) { logger.error(ex); } Channel channel = null; try { channel = connection.createChannel(); } catch (IOException ex) { logger.error(ex); } try { channel.exchangeDeclare("sensors", "topic"); // sensors is the name of the exchange } catch (IOException ex) { logger.error(ex); } AMQP.Queue.DeclareOk result = null; try { result = channel.queueDeclare("", false, true, false, null); } catch (IOException ex) { logger.error(ex); } String queuename = result.getQueue(); try { channel.queueBind(queuename, "sensors", "realtime.sensordata"); // Binding key is #, this will consume all messages channel.queueBind(queuename, "sensors", "realtime.alarms"); // Binding key is #, this will consume all messages } catch (IOException ex) { logger.error(ex); } logger.info(" [*] Waiting for messages. 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 message = new String(body, "UTF-8"); EventBus eventBus = EventBusFactory.getDefault().eventBus(); Gson gson = new Gson(); Type type = new TypeToken<Map<String, String>>() { }.getType(); Map<String, String> myMap = gson.fromJson(message, type); if (envelope.getRoutingKey().equals("realtime.alarms")) { eventBus.publish("/channel", message); return; } logger.info(envelope.getRoutingKey()); readings.add(new Reading(new Date(Long.parseLong(myMap.get("timestamp"))), myMap.get("data"), myMap.get("name"), myMap.get("room"))); eventBus.publish("/channel", message); } }; try { //RequestContext.getCurrentInstance().execute("function rpt() {$(\".qualquer\").first().trigger(\"click\")}" // + "var timeoutdummy = window.setInterval(rpt,2000);"); channel.basicConsume(queuename, true, consumer); } catch (IOException ex) { ex.printStackTrace(); logger.error(ex); } }
From source file:com.es.dashboard.ConnectionBrokerJPA.java
@PostConstruct public void init() { bigBaite = new LinkedList<>(); readings = new ArrayList<Reading>(); ConnectionFactory factory = new ConnectionFactory(); factory.setHost("systembus"); // RabbitMQ IP Connection connection = null; try {//from ww w. ja va 2s . co m connection = factory.newConnection(); } catch (IOException ex) { logger.error(ex); } catch (TimeoutException ex) { logger.error(ex); } Channel channel = null; try { channel = connection.createChannel(); } catch (IOException ex) { logger.error(ex); } try { channel.exchangeDeclare("sensors", "topic"); // sensors is the name of the exchange } catch (IOException ex) { logger.error(ex); } AMQP.Queue.DeclareOk result = null; try { result = channel.queueDeclare("", false, true, false, null); } catch (IOException ex) { logger.error(ex); } String queuename = result.getQueue(); try { channel.queueBind(queuename, "sensors", "dashboard.response"); // Binding key is #, this will consume all messages } catch (IOException ex) { logger.error(ex); } logger.info(" [*] Waiting for messages. 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 message = new String(body, "UTF-8"); EventBus eventBus = EventBusFactory.getDefault().eventBus(); Gson gson = new Gson(); Type type = new TypeToken<Map<String, String>>() { }.getType(); Map<String, String> myMap = gson.fromJson(message, type); if (envelope.getRoutingKey().equals("realtime.alarms")) { eventBus.publish("/jpa", message); return; } System.out.println(envelope.getRoutingKey()); logger.info(envelope.getRoutingKey()); //readings.add(new Reading(new Date(Long.parseLong(myMap.get("timestamp"))), myMap.get("value"), myMap.get("name"), myMap.get("room"))); //System.out.println("publishing " + msg_to_publish); eventBus.publish("/jpa", message); //System.out.println("published"); } }; try { //RequestContext.getCurrentInstance().execute("function rpt() {$(\".qualquer\").first().trigger(\"click\")}" // + "var timeoutdummy = window.setInterval(rpt,2000);"); channel.basicConsume(queuename, true, consumer); } catch (IOException ex) { ex.printStackTrace(); logger.error(ex); } }
From source file:com.eventbook.controller.SpringbootPocController.java
@RequestMapping(value = "/rabbitMQReceiveTest", method = RequestMethod.GET) public String rabbitMQReceiveTest() { try {/*from w w w .ja v a 2 s. co m*/ ConnectionFactory factory = new ConnectionFactory(); factory.setHost("rabbitmq"); factory.setPort(5672); 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 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); } catch (IOException | TimeoutException ex) { Logger.getLogger(SpringbootPocController.class.getName()).log(Level.SEVERE, null, ex); } return "Getting messages from RabbitMQ!!"; }
From source file:com.frannciscocabral.ufs.rabbitmq.Receiver.java
public static void main(String[] argv) throws java.io.IOException, java.lang.InterruptedException, TimeoutException { 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/*from 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.github.hexsmith.rabbitmq.consumer.MessageConsumer.java
License:Open Source License
public boolean consume(String queueName) { //RabbitMQ//from w w w .j a v a 2 s.c o m ConnectionFactory factory = new ConnectionFactory(); factory.setHost("127.0.0.1"); Connection connection = null; Channel channel = null; try { connection = factory.newConnection(); channel = connection.createChannel(); //queue??queue //?queueDeclare???queue channel.queueDeclare(queueName, false, false, false, null); //?DefaultConsumerhandleDelivery???getByte()???String 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"); logger.info("Received '" + message + "'"); } }; //?? channel.basicConsume(queueName, true, consumer); //???rabbitMQ } catch (IOException | TimeoutException e) { //?false logger.error("send message failed!", e); return false; } return true; }
From source file:com.github.hexsmith.rabbitmq.consumer.MultiMessageConsumer.java
License:Open Source License
public boolean consume(String queueName, String consumerName) { //RabbitMQ/* w ww .j a v a 2s. c o m*/ ConnectionFactory factory = new ConnectionFactory(); factory.setHost("127.0.0.1"); Connection connection = null; Channel channel = null; try { connection = factory.newConnection(); channel = connection.createChannel(); //queue??queue //?queueDeclare???queue channel.queueDeclare(queueName, false, false, false, null); //?DefaultConsumerhandleDelivery???getByte()???String 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"); logger.info("[{}]" + "Received '" + message + "'", consumerName); } }; //?? channel.basicConsume(queueName, true, consumer); //???rabbitMQ } catch (IOException | TimeoutException e) { //?false logger.error("send message failed!", e); return false; } return true; }
From source file:com.groupx.recipientlist.test.TranslatorMock.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.exchangeDeclare(EXCHANGE_NAME, "fanout"); String queueName = channel.queueDeclare().getQueue(); channel.queueBind(queueName, EXCHANGE_NAME, ""); System.out.println(" [*] Waiting for messages. To exit press X+ENTER"); Consumer consumer = new DefaultConsumer(channel) { @Override/*from www . ja v a 2 s . com*/ 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(queueName, true, consumer); try { Scanner in = new Scanner(System.in); String input = in.next(); if ("x".equals(input)) { System.exit(0); } } catch (Exception e) { } }
From source file:com.kurento.kmf.rabbitmq.RabbitTemplate.java
License:Apache License
protected Message doSendAndReceiveWithTemporary(final String exchange, final String routingKey, final Message message) { return this.execute(new ChannelCallback<Message>() { @Override/*from ww w . ja v a2 s. c o m*/ public Message doInRabbit(Channel channel) throws Exception { final ArrayBlockingQueue<Message> replyHandoff = new ArrayBlockingQueue<Message>(1); Assert.isNull(message.getMessageProperties().getReplyTo(), "Send-and-receive methods can only be used if the Message does not already have a replyTo property."); DeclareOk queueDeclaration = channel.queueDeclare(); String replyTo = queueDeclaration.getQueue(); message.getMessageProperties().setReplyTo(replyTo); String consumerTag = UUID.randomUUID().toString(); DefaultConsumer consumer = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { MessageProperties messageProperties = messagePropertiesConverter .toMessageProperties(properties, envelope, encoding); Message reply = new Message(body, messageProperties); if (logger.isTraceEnabled()) { logger.trace("Message received " + reply); } try { replyHandoff.put(reply); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } }; channel.basicConsume(replyTo, true, consumerTag, true, true, null, consumer); doSend(channel, exchange, routingKey, message, null); Message reply = (replyTimeout < 0) ? replyHandoff.take() : replyHandoff.poll(replyTimeout, TimeUnit.MILLISECONDS); channel.basicCancel(consumerTag); return reply; } }); }
From source file:com.lang.pat.rabbitirc.Consumer.java
public void consume() { com.rabbitmq.client.Consumer consumer = new DefaultConsumer(channel) { @Override/* www. jav a 2 s.c om*/ public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String message = new String(body, "UTF-8"); JSONParser parse = new JSONParser(); SimpleDateFormat formatDate = new SimpleDateFormat("yy-MM-dd HH:mm:ss"); try { JSONObject JSONMessage = (JSONObject) parse.parse(message); Date sendDat = new Date(); sendDat.setTime((long) JSONMessage.get("timestamp")); System.out.println("[" + envelope.getRoutingKey() + "] " + "[" + JSONMessage.get("username") + "] " + JSONMessage.get("message") + " || " + formatDate.format(sendDat)); if (JSONMessage.get("username").toString().equals(ClientMain.USERNAME)) { if (!JSONMessage.get("token").toString().equals(ClientMain.token)) { System.out.print("! Duplicate username, please change to avoid conflict!"); } } System.out.print("> "); } catch (ParseException ex) { Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex); } } }; try { channel.basicConsume(ClientMain.QUEUENAME, true, consumer); } catch (IOException ex) { Logger.getLogger(Consumer.class.getName()).log(Level.SEVERE, null, ex); } }