List of usage examples for com.rabbitmq.client Connection createChannel
Channel createChannel() throws IOException;
From source file:com.es.dashboard.ConnectionBrokerJPA.java
public void requestData() throws IOException, TimeoutException, InterruptedException { System.out.println("ran"); ConnectionFactory factory = new ConnectionFactory(); factory.setHost("systembus"); // RabbitMQ IP Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare("sensors", "topic"); JSONObject json = new JSONObject(); json.put("name", "Temperatura dos Narcisos"); json.put("room", "masterbath"); json.put("start", System.currentTimeMillis() - 3600000); json.put("end", System.currentTimeMillis()); channel.basicPublish("sensors", "dashboard.request", null, json.toString().getBytes()); json = new JSONObject(); json.put("name", "Humidade da Cave"); json.put("room", "masterbath"); json.put("start", System.currentTimeMillis() - 3600000); json.put("end", System.currentTimeMillis()); channel.basicPublish("sensors", "dashboard.request", null, json.toString().getBytes()); json = new JSONObject(); json.put("name", "Luminosidade da Estufa"); json.put("room", "masterbath"); json.put("start", System.currentTimeMillis() - 3600000); json.put("end", System.currentTimeMillis()); channel.basicPublish("sensors", "dashboard.request", null, json.toString().getBytes()); json = new JSONObject(); json.put("name", "Sensor de Fumo"); json.put("room", "masterbath"); json.put("start", System.currentTimeMillis() - 3600000); json.put("end", System.currentTimeMillis()); channel.basicPublish("sensors", "dashboard.request", null, json.toString().getBytes()); //System.err.println("booting"); }
From source file:com.es.sensorgateway.Publish.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods./*from ww w . j a v a 2 s . c o m*/ * * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, UnknownHostException { response.setContentType("text/html;charset=UTF-8"); try (PrintWriter out = response.getWriter()) { /* TODO output your page here. You may use following sample code. */ logger.info("Connection with rabbit Mq stablish!"); byte[] original; String message = null; try { message = request.getParameter("data"); original = Base64.getUrlDecoder().decode(message); } catch (Exception ex) { response.setStatus(HttpServletResponse.SC_BAD_REQUEST); response.getWriter().write(HttpServletResponse.SC_BAD_REQUEST); logger.debug("Ignoring message: " + message); return; } ConnectionFactory factory = new ConnectionFactory(); factory.setHost("systembus"); // RabbitMQ IP Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); //POR aqui as cenas de autenticao response.setStatus(HttpServletResponse.SC_OK); response.getWriter().write(HttpServletResponse.SC_OK); logger.info("Received request from REST gateway!"); channel.exchangeDeclare("sensors", "topic", false); channel.basicPublish("sensors", "realtime.sensordata", null, original); logger.info("Message sent to broker: " + message); channel.close(); connection.close(); logger.info("Connection with rabbit Mq closed!"); } catch (TimeoutException ex) { logger.error(ex); } }
From source file:com.espertech.esperio.amqp.AMQPSupportReceiveRunnable.java
License:Open Source License
public void run() { try {//from ww w . j a va 2 s . co m ConnectionFactory factory = new ConnectionFactory(); factory.setHost(hostName); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); // java.lang.String queue, boolean durable, boolean exclusive, boolean autoDelete, java.util.Map<java.lang.String,java.lang.Object> arguments channel.queueDeclare(queueName, false, false, true, null); log.info("Start receiving messages"); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(queueName, true, consumer); int count = 0; while (true) { final QueueingConsumer.Delivery msg = consumer.nextDelivery(waitMSecNextMsg); if (msg == null) { continue; } final byte[] bytes = msg.getBody(); callback.handleMessage(bytes); if (isShutdown()) { break; } } log.info("Completed publishing messages: " + count + " messages"); } catch (Exception ex) { log.error("Error attaching to AMQP: " + ex.getMessage(), ex); } }
From source file:com.espertech.esperio.amqp.AMQPSupportSendRunnable.java
License:Open Source License
public void run() { try {/*from www . j ava2 s .c o m*/ ConnectionFactory factory = new ConnectionFactory(); factory.setHost(hostName); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); // java.lang.String queue, boolean durable, boolean exclusive, boolean autoDelete, java.util.Map<java.lang.String,java.lang.Object> arguments channel.queueDeclare(queueName, false, false, true, null); log.info("Start publishing messages: " + events.size() + " messages"); int count = 0; while (true) { if (events.isEmpty()) { break; } Object next = events.remove(0); byte[] bytes = SerializerUtil.objectToByteArr(next); channel.basicPublish("", queueName, null, bytes); count++; log.info("Publishing message #" + count + ": " + next); Thread.sleep(msecSleepTime); if (isShutdown()) { break; } } log.info("Completed publishing messages: " + count + " messages"); } catch (Exception ex) { log.error("Error attaching to AMQP: " + ex.getMessage(), ex); } }
From source file:com.espertech.esperio.amqp.AMQPSupportUtil.java
License:Open Source License
public static int drainQueue(String hostName, String queueName) { Connection connection = null; Channel channel = null;/*ww w. j a v a2 s . c om*/ try { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(hostName); connection = factory.newConnection(); channel = connection.createChannel(); // java.lang.String queue, boolean durable, boolean exclusive, boolean autoDelete, java.util.Map<java.lang.String,java.lang.Object> arguments channel.queueDeclare(queueName, false, false, true, null); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(queueName, true, consumer); int count = 0; while (true) { final QueueingConsumer.Delivery msg = consumer.nextDelivery(1); if (msg == null) { return count; } } } catch (Exception ex) { log.error("Error attaching to AMQP: " + ex.getMessage(), ex); } finally { if (channel != null) { try { channel.close(); } catch (IOException e) { e.printStackTrace(); } } if (connection != null) { try { connection.close(); } catch (IOException e) { e.printStackTrace(); } } } return -1; }
From source file:com.eventbook.controller.SpringbootPocController.java
@RequestMapping(value = "/rabbitMQSendTest", method = RequestMethod.GET) public String rabbitMQSendTest(@RequestParam(value = "message", defaultValue = "Hello World!") String message) { try {//from ww w . jav a 2 s . com 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); channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8")); System.out.println(" [x] Sent '" + message + "'"); channel.close(); connection.close(); return "rabbitMQSendTest Sent: " + message; } catch (IOException | TimeoutException ex) { Logger.getLogger(SpringbootPocController.class.getName()).log(Level.SEVERE, null, ex); } return "rabbitMQSendTest has been failed!!!"; }
From source file:com.eventbook.controller.SpringbootPocController.java
@RequestMapping(value = "/rabbitMQReceiveTest", method = RequestMethod.GET) public String rabbitMQReceiveTest() { try {/* w w w .j a 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 .j av a 2s . co 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.frannciscocabral.ufs.rabbitmq.Send.java
public static void main(String[] argv) throws java.io.IOException, TimeoutException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(QUEUE_NAME, false, false, false, null); String message = "Hello World!"; channel.basicPublish("", QUEUE_NAME, null, message.getBytes()); System.out.println(" [x] Sent '" + message + "'"); channel.close();/*from w ww . j a v a 2 s.c om*/ connection.close(); }
From source file:com.gdp.rabbitmq.task.consumer.ext.ManualAckConsumer.java
public ManualAckConsumer() throws IOException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("127.0.0.1"); Connection connection = factory.newConnection(); channel = connection.createChannel(); }