List of usage examples for com.rabbitmq.client Channel close
@Override void close() throws IOException, TimeoutException;
From source file:com.ericsson.eiffel.remrem.publish.helper.RabbitMqProperties.java
License:Apache License
/** * This method is used to check for checking exchange availability, if * exchange is not available creates a new exchange based on isCreateExchangeIfNotExisting true boolean property . * @throws RemRemPublishException/*from w ww . ja v a 2 s .c o m*/ * @throws TimeoutException * @throws IOException */ public void checkAndCreateExchangeIfNeeded() throws RemRemPublishException { final boolean exchangeAlreadyExist = hasExchange(); if (!exchangeAlreadyExist) { if (isCreateExchangeIfNotExisting()) { Connection connection = null; try { connection = factory.newConnection(); } catch (final IOException | TimeoutException e) { throw new RemRemPublishException("Exception occurred while creating Rabbitmq connection ::" + factory.getHost() + ":" + factory.getPort() + e.getMessage()); } Channel channel = null; try { channel = connection.createChannel(); } catch (final IOException e) { throw new RemRemPublishException( "Exception occurred while creating Channel with Rabbitmq connection ::" + factory.getHost() + ":" + factory.getPort() + e.getMessage()); } try { channel.exchangeDeclare(exchangeName, "topic", true); } catch (final IOException e) { log.info(exchangeName + "failed to create an exchange"); throw new RemRemPublishException("Unable to create Exchange with Rabbitmq connection " + exchangeName + factory.getHost() + ":" + factory.getPort() + e.getMessage()); } finally { if (channel == null || channel.isOpen()) { try { channel.close(); connection.close(); } catch (IOException | TimeoutException e) { log.warn("Exception occurred while closing the channel" + e.getMessage()); } } } } else { if (!Boolean.getBoolean(PropertiesConfig.CLI_MODE)) { throw new RemRemPublishException( exchangeName + PropertiesConfig.INVALID_EXCHANGE_MESSAGE_SERVICE); } else { throw new RemRemPublishException( "Exchange " + exchangeName + PropertiesConfig.INVALID_EXCHANGE_MESSAGE_CLI); } } } }
From source file:com.ericsson.eiffel.remrem.publish.helper.RabbitMqProperties.java
License:Apache License
/** * This method is used to check exchange exists or not * @return Boolean//from w w w. ja v a2s.co m * @throws RemRemPublishException * @throws TimeoutException * @throws IOException */ private boolean hasExchange() throws RemRemPublishException { log.info("Exchange is: " + exchangeName); Connection connection; try { connection = factory.newConnection(); } catch (final IOException | TimeoutException e) { throw new RemRemPublishException("Exception occurred while creating Rabbitmq connection ::" + factory.getHost() + factory.getPort() + e.getMessage()); } Channel channel = null; try { channel = connection.createChannel(); } catch (final IOException e) { log.info("Exchange " + exchangeName + " does not Exist"); throw new RemRemPublishException("Exception occurred while creating Channel with Rabbitmq connection ::" + factory.getHost() + factory.getPort() + e.getMessage()); } try { channel.exchangeDeclarePassive(exchangeName); return true; } catch (final IOException e) { log.info("Exchange " + exchangeName + " does not Exist"); return false; } finally { if (channel != null && channel.isOpen()) { try { channel.close(); connection.close(); } catch (IOException | TimeoutException e) { log.warn("Exception occurred while closing the channel" + e.getMessage()); } } } }
From source file:com.es.sensorgateway.Publish.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods./* ww w.ja v a 2 s .co 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.AMQPSupportUtil.java
License:Open Source License
public static int drainQueue(String hostName, String queueName) { Connection connection = null; Channel channel = null; try {//from w w w. j a va 2 s . com 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 w w w . j a v a 2 s . c om 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.flipkart.bifrost.framework.impl.RabbitMQBifrostExecutor.java
License:Apache License
@Override public void shutdown() throws BifrostException { try {//from w w w . jav a 2 s .c om publishChannel.close(); for (ReplyListener<T> listener : listeners) { Channel channel = listener.getChannel(); channel.basicCancel(listener.getConsumerTag()); channel.close(); } //TODO::Save the futures and shut them down } catch (Exception e) { logger.error("Error publishing: ", e); } }
From source file:com.flipkart.bifrost.framework.impl.RabbitMQBifrostRemoteCallExecutionServer.java
License:Apache License
@Override public void stop() throws BifrostException { try {//from w ww . j a va 2 s . com for (RabbitMQExecutionServerListener<T> listener : listeners) { if (!Strings.isNullOrEmpty(listener.getConsumerTag())) { Channel channel = listener.getChannel(); channel.basicCancel(listener.getConsumerTag()); channel.close(); } } } catch (IOException e) { throw new BifrostException(BifrostException.ErrorCode.IO_ERROR, "Error unregistering listener", e); } }
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(); connection.close();// ww w. ja v a 2 s . co m }
From source file:com.github.dann.wspusher.common.util.RabbitMQResourceUtils.java
License:Apache License
public static void closeQuietly(Channel channel) { if (channel != null) { try {//ww w. java 2 s.c om channel.close(); } catch (IOException e) { // ignore } } }
From source file:com.github.hexsmith.rabbitmq.producer.MessageProducer.java
License:Open Source License
public boolean sendMessage(String message) { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("127.0.0.7"); Connection connection = null; Channel channel = null; try {/*from w w w . j a v a 2 s. com*/ connection = factory.newConnection(); channel = connection.createChannel(); channel.queueDeclare(QUEUE_NAME, false, false, false, null); channel.basicPublish("", QUEUE_NAME, null, message.getBytes()); logger.info("send message = {}", message); channel.close(); connection.close(); } catch (IOException | TimeoutException e) { logger.error("send message failed!,exception message is {}", e); return false; } return true; }