List of usage examples for com.rabbitmq.client Connection close
@Override void close() throws IOException;
From source file:sd_aula06.Send.java
public static void main(String[] argv) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("reindeer.rmq.cloudamqp.com"); factory.setUsername("jmodzuaw"); factory.setPassword("Kwuy7kd81ED1fIj9gxEti1J4FTPBj2Jz"); factory.setVirtualHost("jmodzuaw"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(QUEUE_NAME, false, false, false, null); String message = "RafaelReis: VSF!"; channel.basicPublish("", QUEUE_NAME, null, message.getBytes("UTF-8")); System.out.println(" [x] Sent '" + message + "'"); channel.close();//from w w w.j a v a2s .co m connection.close(); }
From source file:taucalcmanager.TaucalcManager.java
/** * @param args the command line arguments * @throws java.io.IOException//from w w w . j av a 2 s . c om * @throws java.util.concurrent.TimeoutException */ public static void main(String[] args) throws IOException, TimeoutException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(WORKQUEUENAME, false, false, false, null); channel.queueDeclare(RESULTQUEUENAME, false, false, false, null); channel.basicQos(1); int calcs = Integer.parseInt(args[0]); int triesPerCalc = Integer.parseInt(args[1]); int[] results = new int[calcs]; byte[] task = { (byte) (triesPerCalc >> 24), (byte) (triesPerCalc >> 16), (byte) (triesPerCalc >> 8), (byte) (triesPerCalc) }; System.out.println("Start Publishing"); for (int i = 0; i < calcs; i++) { channel.basicPublish("", WORKQUEUENAME, null, task); } System.out.println("Done Publishing"); GetResponse response; System.out.println("Start Gathering results"); for (int i = 0; i < calcs; i++) { System.out.println("Waiting for next result: ( " + i + " )"); do { response = channel.basicGet(RESULTQUEUENAME, true); } while (response == null); results[i] = ByteBuffer.wrap(response.getBody()).getInt(); System.out.println("Received result: " + results[i]); } System.out.println("Done gathering results"); System.out.println("Calculating tau"); BigDecimal result = sumToTau(sum(results), calcs, triesPerCalc); System.out.println("Tau = " + result); BigDecimal two = new BigDecimal(2); System.out.println("pi = tau/2 = " + result.divide(two, RoundingMode.HALF_UP)); channel.close(); connection.close(); }
From source file:taucalcworker.TaucalcWorker.java
/** * @param args the command line arguments * @throws java.io.IOException// w w w .j a v a 2 s .c o m * @throws java.util.concurrent.TimeoutException */ public static void main(String[] args) throws IOException, TimeoutException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(WORKQUEUENAME, false, false, false, null); channel.queueDeclare(RESULTQUEUENAME, false, false, false, null); channel.basicQos(1); byte[] inputbyte = { 0, 0, 0, 0 }; String input = ""; do { if (System.in.available() > 0) { System.in.read(inputbyte); input = new String(inputbyte); } GetResponse response = channel.basicGet(WORKQUEUENAME, false); if (response != null) { long deliverytag = response.getEnvelope().getDeliveryTag(); byte[] body = response.getBody(); int tries = ByteBuffer.wrap(body).getInt(); System.out.println("Task received: " + tries); int success = 0; for (int i = 0; i < tries; i++) { double x = Math.random(); double y = Math.random(); if (x * x + y * y <= 1) { success++; } } System.out.println("success: " + success + " out of " + tries); double tau = ((double) success / tries) * 8; System.out.println("Tau = " + tau); byte[] resultbytes = new byte[8]; ByteBuffer.wrap(resultbytes).putDouble(tau); channel.basicPublish("", RESULTQUEUENAME, null, resultbytes); channel.basicAck(deliverytag, false); } } while (!input.equals("stop")); channel.close(); connection.close(); System.out.println("You stopped the program."); }
From source file:uk.org.openeyes.oink.hl7v2.test.Hl7ITSupport.java
License:Open Source License
protected Message sendHl7Message(Message adt, String host, int port) throws NumberFormatException, HL7Exception, LLPException, IOException { HapiContext context = new DefaultHapiContext(); ca.uhn.hl7v2.app.Connection hl7v2Conn = context.newClient(host, port, false); Initiator initiator = hl7v2Conn.getInitiator(); Message response = initiator.sendAndReceive(adt); hl7v2Conn.close(); return response; }
From source file:uk.org.openeyes.oink.proxy.test.support.RabbitClient.java
License:Open Source License
public byte[] sendAndRecieve(byte[] message, String routingKey, String exchange) throws Exception { log.debug("Sending message to direct exchange:" + exchange + " with routing key:" + routingKey); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(exchange, "direct", true, false, null); String replyQueueName = channel.queueDeclare().getQueue(); log.debug("Reply queue name is " + replyQueueName); channel.queueBind(replyQueueName, exchange, replyQueueName); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(replyQueueName, true, consumer); String corrId = java.util.UUID.randomUUID().toString(); BasicProperties props = new BasicProperties.Builder().correlationId(corrId).replyTo(replyQueueName).build(); channel.basicPublish(exchange, routingKey, props, message); log.debug("Waiting for delivery"); QueueingConsumer.Delivery delivery = consumer.nextDelivery(20000); connection.close(); if (delivery == null || !delivery.getProperties().getCorrelationId().equals(corrId)) { return null; } else {/*www . j a v a2 s . co m*/ byte[] response = delivery.getBody(); return response; } }
From source file:uniko.west.test.TwitterLogSender.java
public static void main(String[] args) { /* String strJSONFilePaths = { "/home/ubuntu/data/rawtweet-log-short.log", "/home/ubuntu/data/IITNNO-prepare-json-example.json", "/home/ubuntu/data/json/IITNNO-raw-JSON-example.json", "/home/ubuntu/data/json/ITINNO-aggregated-geoparse-example.json" "/home/ubuntu/data/json/CERTH-RSS-example.json" }; *//* w w w.j av a 2s.c o m*/ // String strJSONFilePath = "/home/ubuntu/data/rawtweet-log-short.log"; //short twitter log // String strJSONFilePath = "/home/ubuntu/data/json/IITNNO-prepare-json-example.json"; //String strJSONFilePath = "/home/ubuntu/data/json/IITNNO-raw-JSON-example.json"; //short itinno // example // String strJSONFilePath = "/home/ubuntu/data/json/ITINNO-aggregated-geoparse-example.json"; //another itinno example String strJSONFilePath = "/home/ubuntu/data/json/simplified-ITINNO-aggregated-geoparse-example.json"; String exchangeName = "ukob_test"; // for (String strJSONFilePath : strJSONFilePaths) { try (BufferedReader br = new BufferedReader(new FileReader(strJSONFilePath))) { // send a UTF-8 encoded JSON tweet to the RabbitMQ (for // stormspout // to pick up and send to bolt) // read UTF-8 JSON text from file Logger.getLogger(TwitterLogSender.class.getName()).log(Level.INFO, "ExampleRabbitmqClient started"); // connect to rabbitmq broker // first of all create connection factory ConnectionFactory factory = new ConnectionFactory(); factory.setUri("amqp://guest:guest@localhost:5672/%2F"); // initialise connection and define channel Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); String jsonLine; while ((jsonLine = br.readLine()) != null) { long timestampSinceEpoch = System.currentTimeMillis() / 1000; // initialise amqp basic peoperties object BasicProperties.Builder basicProperties = new AMQP.BasicProperties.Builder(); basicProperties.build(); basicProperties.timestamp(new Date(timestampSinceEpoch)).build(); basicProperties.contentType("text/json").build(); basicProperties.deliveryMode(1).build(); // publish message channel.basicPublish(exchangeName, "test-routing", basicProperties.build(), jsonLine.getBytes("UTF-8")); } // close connection and channel channel.close(); connection.close(); Logger.getLogger(TwitterLogSender.class.getName()).log(Level.INFO, "ExampleRabbitmqClient finished"); } catch (URISyntaxException | NoSuchAlgorithmException | KeyManagementException | IOException ex) { Logger.getLogger(TwitterLogSender.class.getName()).log(Level.SEVERE, null, ex); // } } }
From source file:v2.Student.java
public static void main(String[] argv) { Connection connection = null; Channel channel = null;/*from w w w.java 2 s.com*/ try { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("datdb.cphbusiness.dk"); connection = factory.newConnection(); channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME, "topic"); String routingKey = "AP.DAT";//getRouting(argv); String message = "Plz enroll me, breh";//getMessage(argv); String corrId = java.util.UUID.randomUUID().toString(); String callbackQueueName = channel.queueDeclare().getQueue(); String response = null; QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(callbackQueueName, true, consumer); BasicProperties props = new BasicProperties.Builder().replyTo(callbackQueueName).build(); channel.basicPublish(EXCHANGE_NAME, routingKey, props, message.getBytes("UTF-8")); System.out.println(" [x] Sent '" + routingKey + "':'" + message + "'"); while (true) { System.out.println("blocked?"); QueueingConsumer.Delivery delivery = consumer.nextDelivery(); System.out.println("unblocked"); System.out.println(delivery); response = new String(delivery.getBody()); System.out.println(response); // if (delivery.getProperties().getCorrelationId().equals(corrId)) { // response = new String(delivery.getBody()); // System.out.println(response); // break; // } } } catch (Exception e) { e.printStackTrace(); } finally { if (connection != null) { try { connection.close(); } catch (Exception ignore) { } } } }
From source file:vn.com.uet.performance.rabbitmq.MulticastSet.java
License:Open Source License
public void run(boolean announceStartup) throws IOException, InterruptedException, TimeoutException { Thread[] consumerThreads = new Thread[params.getConsumerCount()]; Connection[] consumerConnections = new Connection[consumerThreads.length]; for (int i = 0; i < consumerConnections.length; i++) { if (announceStartup) { System.out.println("id: " + testID + ", starting consumer #" + i); }/*from w w w. j a v a 2s. c om*/ Connection conn = factory.newConnection(); consumerConnections[i] = conn; Thread t = new Thread(params.createConsumer(conn, stats, id)); consumerThreads[i] = t; } if (params.shouldConfigureQueue()) { Connection conn = factory.newConnection(); params.configureQueue(conn, id); conn.close(); } Thread[] producerThreads = new Thread[params.getProducerCount()]; Connection[] producerConnections = new Connection[producerThreads.length]; for (int i = 0; i < producerThreads.length; i++) { if (announceStartup) { System.out.println("id: " + testID + ", starting producer #" + i); } Connection conn = factory.newConnection(); producerConnections[i] = conn; Thread t = new Thread(params.createProducer(conn, stats, id)); producerThreads[i] = t; } for (Thread consumerThread : consumerThreads) { consumerThread.start(); } for (Thread producerThread : producerThreads) { producerThread.start(); } for (int i = 0; i < producerThreads.length; i++) { producerThreads[i].join(); producerConnections[i].close(); } for (int i = 0; i < consumerThreads.length; i++) { consumerThreads[i].join(); consumerConnections[i].close(); } }
From source file:wiki.messaging.NewTask.java
License:Open Source License
public static void main(String[] args) throws IOException { int n = 500;/*w w w.j av a 2 s . c om*/ String url = null; String rabbitMqUrl = null; for (int i = 0; i < args.length; i++) { if (args[i].equals("ds")) { url = args[i + 1]; } else if (args[i].equals("rq")) { rabbitMqUrl = args[i + 1]; } else if (args[i].equals("n")) { n = Integer.valueOf(args[i + 1]); } } DbConnector ds = new DbConnector(url); ConnectionFactory factory = new ConnectionFactory(); factory.setHost(rabbitMqUrl); factory.setUsername("wiki"); factory.setPassword("wiki"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(QUEUE_NAME, false, false, false, null); RandomDocGetter rdg = new RandomDocGetter(ds); for (int i = 0; i < n; i++) { String message = getMessage(rdg); channel.basicPublish("", QUEUE_NAME, null, message.getBytes()); System.out.println(" [x] Sent '" + message + "'"); } channel.close(); connection.close(); }
From source file:wiki.messaging.ResultConsumer.java
License:Open Source License
public static void main(String[] args) throws IOException, InterruptedException { String rabbitMqUrl = null;// w ww. jav a 2 s. com String resultUrl = null; for (int i = 0; i < args.length; i++) { if (args[i].equals("rs")) { resultUrl = args[i + 1]; } else if (args[i].equals("rq")) { rabbitMqUrl = args[i + 1]; } } ConnectionFactory factory = new ConnectionFactory(); factory.setHost(rabbitMqUrl); factory.setUsername("wiki"); factory.setPassword("wiki"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(RESULT_QUEUE_NAME, true, false, false, null); System.out.println(" [*] Waiting for messages."); QueueingConsumer consumer = new QueueingConsumer(channel); boolean autoAck = false; channel.basicConsume(RESULT_QUEUE_NAME, autoAck, consumer); DbConnector dbc = new DbConnector(resultUrl); while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(1000); if (delivery == null) break; String message = new String(delivery.getBody()); System.out.println(" [x] Received '" + message + "'"); saveResult(message, dbc); System.out.println(" [x] Done"); channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); } channel.close(); connection.close(); }