List of usage examples for com.rabbitmq.client Connection close
@Override void close() throws IOException;
From source file:pl.nask.hsn2.bus.rabbitmq.RbtUtils.java
License:Open Source License
/** * Closing quietly RabbitMQ connection./*w w w .j a va 2 s .c om*/ * * @param connection <code>com.rabbitmq.client.Connection</code> to close. */ public static void closeConnection(Connection connection) { if (connection != null) { try { if (connection.isOpen()) { connection.close(); } } catch (IOException ex) { LOGGER.debug("Ignoring Connection state exception - assuming already closed: " + ex); } catch (Exception ex) { LOGGER.debug("Unexpected exception on closing RabbitMQ Connection", ex); } } }
From source file:pro.foundev.examples.spark_streaming.java.interactive.querybasedconsumer.ResponseWriter.java
License:Apache License
private Messenger initRabbitMqResponseQueue() { ConnectionFactory factory = new ConnectionFactory(); final Connection connection; final Channel channel; factory.setHost("localhost"); try {/* w ww . jav a2s . c om*/ connection = factory.newConnection(); channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME, "fanout"); } catch (IOException e) { throw new RuntimeException(e); } Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { ResponseWriter.mainThread.interrupt(); try { channel.close(); connection.close(); } catch (IOException e) { e.printStackTrace(); } } }); return new MessengerImpl(channel); }
From source file:pro.foundev.examples.spark_streaming.java.messaging.RabbitMQEmitWarnings.java
License:Apache License
public static void main(String[] argv) throws java.io.IOException { String master = Args.parseMaster(argv); mainThread = Thread.currentThread(); ConnectionFactory factory = new ConnectionFactory(); factory.setHost(master);/*from ww w . j a va 2 s .c o m*/ final Connection connection = factory.newConnection(); final Channel channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME, "fanout"); Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { RabbitMQEmitWarnings.mainThread.interrupt(); try { channel.close(); connection.close(); } catch (IOException e) { e.printStackTrace(); } } }); while (true) { String message = buildMessage(); channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes()); System.out.println(" [x] Sent '" + message + "'"); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } }
From source file:pt.ua.ies.Main.java
public static void main(String[] args) { try {// w ww . j a va2 s .co m ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME, "fanout"); String message = "Random Message " + new Random().nextInt(1000); channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes()); System.out.println(" [x] Sent '" + message + "'"); /** * Close the channel and the connection */ channel.close(); connection.close(); } catch (Exception ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:rabbitirc.RabbitIRC.java
public static void main(String[] argv) throws java.io.IOException, TimeoutException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Random rnd = new Random(); int indeks = rnd.nextInt(usernamelist.length); String Randusername = usernamelist[indeks] + rnd.nextInt(100); user = Randusername;// ww w .ja v a 2 s . com channellist.add("lounge"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); String queueName = channel.queueDeclare().getQueue(); channel.exchangeDeclare(EXCHANGE_NAME, "direct"); Scanner sc = new Scanner(System.in); channel.queueBind(queueName, EXCHANGE_NAME, "lounge"); channellist.add("lounge"); 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 + "'"); } }; System.out.println(" Welcome " + user + " to Channel lounge"); System.out.println(" Queries: "); System.out.println(" 1. /NICK <Username> : Change username "); System.out.println(" 2. /JOIN <Channel Name> : Join Channel"); System.out.println(" 3. @<Channel Name> <Message> : Send message to Spesific Channel"); System.out.println(" 4. /LEAVE <Channel Name> : Leave Channel"); System.out.println(" 5. <Random text> : BroadCast"); System.out.println(" 6. /EXIT : Exit App"); while (true) { channel.basicConsume(queueName, true, consumer); String input = sc.nextLine(); String[] query; if ((query = CommandRegexes.NICK.match(input)) != null) { String Nickname = query[0]; user = Nickname; System.out.println(" [x] Your Nickname '" + Nickname + "'"); } else if ((query = CommandRegexes.JOIN.match(input)) != null) { String channel_name = query[0]; channel.queueBind(queueName, EXCHANGE_NAME, channel_name); channellist.add(channel_name); System.out.println(" [x] you had Join '" + channel_name + "'"); } else if ((query = CommandRegexes.LEAVE.match(input)) != null) { String channel_name = query[0]; if (channellist.contains(channel_name)) { channellist.remove(channellist.indexOf(channel_name)); channel.queueUnbind(queueName, EXCHANGE_NAME, channel_name); System.out.println(" [x] you had leave '" + channel_name); } else { System.out.println(" [x] you're not in the channel " + channel_name); } System.out.println(" [x] you had leave '" + channel_name + "'"); } else if (CommandRegexes.EXIT.match(input) != null) { channel.close(); connection.close(); break; } else if ((query = CommandRegexes.MESSAGE_CHANNEL.match(input)) != null) { String channel_name = query[0]; if (channellist.contains(channel_name)) { String message = "[" + channel_name + "] (" + user + ") " + query[1]; channel.basicPublish(EXCHANGE_NAME, channel_name, null, message.getBytes()); System.out.println(" [x] Sent '" + message + "'"); System.out.println(" : to '" + channel_name + "'"); } else { System.out.println(" [x] No Channel " + channel_name + " on your list"); } } else { System.out.println(" [x] Broadcasting '" + input + "'"); for (String channellist1 : channellist) { String messages = "[" + channellist1 + "] (" + user + ") " + input; channel.basicPublish(EXCHANGE_NAME, channellist1, null, messages.getBytes()); } System.out.println(" [x] OK ;D"); } } }
From source file:rabbitmq.Producer.java
public void run() { try {//from w w w .j a v a2 s .c o m ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(QUEUE_NAME, false, false, false, null); for (int i = 0; i < num; i++) { Mensaje persona = new Mensaje("RabbitMQ_Java", "Persona" + i, "ID" + i); Gson gson = new Gson(); String message = gson.toJson(persona); channel.basicPublish("", QUEUE_NAME, null, message.getBytes()); System.out.println(" [" + id + "] Sent '" + message + "'"); } channel.close(); connection.close(); } catch (IOException ex) { Logger.getLogger(Producer.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:rabbitmqapp.EmitLog.java
public static void main(String... args) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setUri(Cons.RABBIT_MQ_URL); factory.setConnectionTimeout(3000);/*w w w.java 2 s. c o m*/ factory.setRequestedHeartbeat(30); //Create a connection Connection connection = factory.newConnection(); //create a channel from connection Channel channel = connection.createChannel(); channel.exchangeDeclare(Cons.EXCHANGE_NAME, EXCHANGE_TYPE); String message = "Hello Dolly"; channel.basicPublish(Cons.EXCHANGE_NAME, "", null, message.getBytes()); System.out.println(" [x] Sent '" + message + "'"); channel.close(); connection.close(); }
From source file:rabbitmqapp.RabbitMQApp.java
/** * @param args the command line arguments *//*from ww w . j a v a 2 s . c om*/ public static void main(String[] args) throws Exception { ProductOrder order = new ProductOrder("Thando Mlauzi", 34.50); ConnectionFactory factory = new ConnectionFactory(); String uri = "amqp://ytsoedex:Qu2LCiBJ5x9fhRUyLYkMhJqsURJ9dkSP@chicken.rmq.cloudamqp.com/ytsoedex"; factory.setUri(uri); //Recommended settings factory.setRequestedHeartbeat(30); factory.setConnectionTimeout(30000); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(QUENAME, false, false, false, null); String message = "Hello World!"; channel.basicPublish("", QUENAME, null, RabbitUtility.convertToByteArray(order)); System.out.println(" [x] Sent '" + order + "'"); channel.close(); connection.close(); }
From source file:ru.kinomir.queue.QueueSender.java
public synchronized void sendToQueue(Object data, String queueName, String queueHost, String userName, String password, String port, String virtualHost) { Channel channel = null;/*from w w w . j a v a 2 s . c om*/ Connection connection = null; try { logger.info("Send message to queue '" + queueName + "'"); ConnectionFactory factory = new ConnectionFactory(); if (!StringTools.isEmpty(userName)) { factory.setUsername(userName); } if (!StringTools.isEmpty(password)) { factory.setPassword(password); } if (!StringTools.isEmpty(port)) { try { factory.setPort(Integer.parseInt(port)); } catch (NumberFormatException ignore) { } } if (!StringTools.isEmpty(virtualHost)) { factory.setVirtualHost(virtualHost); } factory.setHost(queueHost); connection = factory.newConnection(); channel = connection.createChannel(); channel.queueDeclare(queueName, true, false, false, null); String message = convertToString(data); logger.info("Message text: " + message); channel.basicPublish("", queueName, MessageProperties.MINIMAL_PERSISTENT_BASIC, message.getBytes()); logger.info("Message was sent"); } catch (Exception ex) { logger.error("Uneble send message: " + convertToString(data)); logger.debug(ex.getMessage(), ex); } finally { try { if (channel != null) { channel.close(); } if (connection != null) { connection.close(); } } catch (Exception ignore) { } } }
From source file:samples.userguide.RabbitMQAMQPClient.java
License:Apache License
public static void main(String[] args) { String queueName = System.getProperty("queueName"); String mode = System.getProperty("mode"); String routingKey = System.getProperty("routingKey"); String exchangeName = System.getProperty("exchangeName"); String quote = System.getProperty("payLoad"); if (quote == null) { quote = "IBM"; }/*from w ww . j a va2 s.c o m*/ String msg = "<m:placeOrder xmlns:m=\"http://services.samples\">\n" + " <m:order>\n" + " <m:price>" + getRandom(100, 0.9, true) + "</m:price>\n" + " <m:quantity>" + (int) getRandom(10000, 1.0, true) + "</m:quantity>\n" + " <m:symbol>" + quote + "</m:symbol>\n" + " </m:order>\n" + "</m:placeOrder>"; ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = null; Channel channel = null; try { connection = factory.newConnection(); channel = connection.createChannel(); if (mode == null) { mode = "producer"; } if ("producer".equals(mode)) { if (queueName != null) { channel.basicPublish("", queueName, null, msg.getBytes()); } else { if (routingKey != null) { if (exchangeName == null) { exchangeName = "topic-exchange"; } channel.basicPublish(exchangeName, routingKey, null, msg.getBytes()); } else { if (exchangeName == null) { exchangeName = "subscriber-exchange"; } channel.basicPublish(exchangeName, "", null, msg.getBytes()); } } } else { if (queueName == null) { queueName = "ConsumerProxy"; } QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(queueName, true, consumer); QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String message = new String(delivery.getBody()); System.out.println("[x] received '" + message + "'"); } channel.close(); connection.close(); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } finally { if (channel != null && channel.isOpen()) { try { channel.close(); } catch (IOException e) { System.err.println("Error occurred while closing the channel:" + e.getMessage()); } } if (connection != null && connection.isOpen()) { try { connection.close(); } catch (IOException e) { System.err.println("Error occurred while closing the connection:" + e.getMessage()); } } } }