List of usage examples for com.rabbitmq.client Channel exchangeDeclare
Exchange.DeclareOk exchangeDeclare(String exchange, BuiltinExchangeType type) throws IOException;
From source file:org.apache.beam.sdk.io.rabbitmq.RabbitMqIOTest.java
License:Apache License
@Test public void testWriteExchange() throws Exception { final int maxNumRecords = 1000; List<RabbitMqMessage> data = generateRecords(maxNumRecords).stream() .map(bytes -> new RabbitMqMessage(bytes)).collect(Collectors.toList()); p.apply(Create.of(data)).apply(/* w w w. jav a 2 s. c o m*/ RabbitMqIO.write().withUri("amqp://guest:guest@localhost:" + port).withExchange("WRITE", "fanout")); final List<String> received = new ArrayList<>(); ConnectionFactory connectionFactory = new ConnectionFactory(); connectionFactory.setUri("amqp://guest:guest@localhost:" + port); Connection connection = null; Channel channel = null; try { connection = connectionFactory.newConnection(); channel = connection.createChannel(); channel.exchangeDeclare("WRITE", "fanout"); String queueName = channel.queueDeclare().getQueue(); channel.queueBind(queueName, "WRITE", ""); Consumer consumer = new TestConsumer(channel, received); channel.basicConsume(queueName, true, consumer); p.run(); while (received.size() < maxNumRecords) { Thread.sleep(500); } assertEquals(maxNumRecords, received.size()); for (int i = 0; i < maxNumRecords; i++) { assertTrue(received.contains("Test " + i)); } } finally { if (channel != null) { channel.close(); } if (connection != null) { connection.close(); } } }
From source file:org.apache.helix.recipes.rabbitmq.ConsumerThread.java
License:Apache License
@Override public void run() { Connection connection = null; try {// w w w. j av a 2 s . c o m ConnectionFactory factory = new ConnectionFactory(); factory.setHost(_mqServer); connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME, "topic"); String queueName = channel.queueDeclare().getQueue(); String bindingKey = _partition.toString(); channel.queueBind(queueName, EXCHANGE_NAME, bindingKey); System.out.println( " [*] " + _consumerId + " Waiting for messages on " + bindingKey + ". To exit press CTRL+C"); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(queueName, true, consumer); while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String message = new String(delivery.getBody()); String routingKey = delivery.getEnvelope().getRoutingKey(); System.out.println(" [x] " + _consumerId + " Received '" + routingKey + "':'" + message + "'"); } } catch (InterruptedException e) { System.err.println(" [-] " + _consumerId + " on " + _partition + " is interrupted ..."); } catch (Exception e) { e.printStackTrace(); } finally { if (connection != null) { try { connection.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
From source file:org.apache.helix.recipes.rabbitmq.Emitter.java
License:Apache License
public static void main(String[] args) throws Exception { if (args.length < 1) { System.err.println("USAGE: java Emitter rabbitmqServer (e.g. localhost) numberOfMessage (optional)"); System.exit(1);/*from w ww. j av a 2 s . c om*/ } final String mqServer = args[0]; // "zzhang-ld"; int count = Integer.MAX_VALUE; if (args.length > 1) { try { count = Integer.parseInt(args[1]); } catch (Exception e) { // TODO: handle exception } } System.out.println("Sending " + count + " messages with random topic id"); ConnectionFactory factory = new ConnectionFactory(); factory.setHost(mqServer); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME, "topic"); for (int i = 0; i < count; i++) { int rand = ((int) (Math.random() * 10000) % SetupConsumerCluster.DEFAULT_PARTITION_NUMBER); String routingKey = "topic_" + rand; String message = "message_" + rand; channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes()); System.out.println(" [x] Sent '" + routingKey + "':'" + message + "'"); Thread.sleep(1000); } connection.close(); }
From source file:org.apache.niolex.rabbit.log.EmitLog.java
License:Apache License
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.exchangeDeclare(EXCHANGE_NAME, "fanout"); for (int i = 0; i < 100; ++i) { String message = String.format("%02d %s", i, MockUtil.randString(5)); ThreadUtil.sleepAtLeast(1000);/*from www. jav a 2 s .com*/ channel.basicPublish(EXCHANGE_NAME, "", null, message.getBytes()); System.out.println(" [x] Sent '" + message + "'"); } channel.close(); connection.close(); }
From source file:org.apache.niolex.rabbit.log.EmitLogDirect.java
License:Apache License
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.exchangeDeclare(EXCHANGE_NAME, "direct"); for (int i = 0; i < 100; ++i) { String message = String.format("%02d %s", i, MockUtil.randString(5)); String severity = MockUtil.randInt(3) == 0 ? "error" : "info"; ThreadUtil.sleepAtLeast(1000);/*from w ww . j ava 2 s . c o m*/ channel.basicPublish(EXCHANGE_NAME, severity, null, message.getBytes()); System.out.println(" [x] Sent '" + severity + "': '" + message + "'"); } channel.close(); connection.close(); }
From source file:org.apache.niolex.rabbit.log.EmitLogTopic.java
License:Apache License
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, "topic"); for (int i = 0; i < 100; ++i) { String message = String.format("%02d %s", i, MockUtil.randString(5)); ThreadUtil.sleepAtLeast(1000);/*from w ww. j ava 2 s. com*/ String routingKey = getSeverity() + "." + getFacility(); channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes()); System.out.println(" [x] Sent '" + routingKey + "':'" + message + "'"); } channel.close(); connection.close(); }
From source file:org.opennaas.extensions.genericnetwork.capability.nclprovisioner.components.NetworkObservationsPusher.java
License:Apache License
private void sendStatistics(String topicName, String csvMessage) throws KeyManagementException, NoSuchAlgorithmException, URISyntaxException, IOException { Connection conn = null;/*from www . ja va2 s .c o m*/ Channel channel = null; try { log.debug("Establishing RabbitMQ connection with sla manager with URI: " + slaManagerUri); ConnectionFactory factory = new ConnectionFactory(); factory.setHost(slaManagerUri.getHost()); conn = factory.newConnection(); channel = conn.createChannel(); channel.exchangeDeclare(topicName, "direct"); log.debug("Publishing message in RabbitMQ channel."); channel.basicPublish(topicName, RABBIT_MQ_ROUTING_KEY, new AMQP.BasicProperties.Builder().contentType(OBSERVATIONS_CONTENT_TYPE).build(), csvMessage.getBytes()); log.debug("Message successfully sent to SLA manager."); } finally { if (channel != null && channel.isOpen()) channel.close(); if (conn != null && conn.isOpen()) conn.close(); log.debug("Connection to RabbitMQ server closed."); } }
From source file:org.springframework.amqp.rabbit.MulticastMain.java
License:Mozilla Public License
public static void main(String[] args) { Options options = getOptions();/*from ww w . j ava 2s .co m*/ CommandLineParser parser = new GnuParser(); try { CommandLine cmd = parser.parse(options, args); if (cmd.hasOption('?')) { usage(options); System.exit(0); } String hostName = strArg(cmd, 'h', "localhost"); int portNumber = intArg(cmd, 'p', AMQP.PROTOCOL.PORT); String exchangeType = strArg(cmd, 't', "direct"); String exchangeName = strArg(cmd, 'e', exchangeType); int samplingInterval = intArg(cmd, 'i', 1); int rateLimit = intArg(cmd, 'r', 0); int producerCount = intArg(cmd, 'x', 1); int messageCount = intArg(cmd, 'N', 0); int consumerCount = intArg(cmd, 'y', 1); int connectionCount = cmd.hasOption('c') ? 1 : consumerCount; int producerTxSize = intArg(cmd, 'm', 0); int consumerTxSize = intArg(cmd, 'n', 0); boolean autoAck = cmd.hasOption('a'); int prefetchCount = intArg(cmd, 'q', 0); int minMsgSize = intArg(cmd, 's', 0); int timeLimit = intArg(cmd, 'z', 0); List<String> flags = lstArg(cmd, 'f'); int frameMax = intArg(cmd, 'M', 0); int heartbeat = intArg(cmd, 'b', 0); // setup String id = UUID.randomUUID().toString(); Stats stats = new Stats(1000L * samplingInterval); ConnectionFactory factory = new ConnectionFactory(); factory.setHost(hostName); factory.setPort(portNumber); factory.setRequestedFrameMax(frameMax); factory.setRequestedHeartbeat(heartbeat); Connection[] consumerConnections = new Connection[connectionCount]; for (int i = 0; i < connectionCount; i++) { Connection conn = factory.newConnection(); consumerConnections[i] = conn; } Thread[] consumerThreads = new Thread[consumerCount]; for (int i = 0; i < consumerCount; i++) { System.out.println("starting consumer #" + i); Connection conn = consumerConnections[i % connectionCount]; Channel channel = conn.createChannel(); if (consumerTxSize > 0) channel.txSelect(); channel.exchangeDeclare(exchangeName, exchangeType); String queueName = channel.queueDeclare("", flags.contains("persistent"), true, false, null) .getQueue(); QueueingConsumer consumer = new QueueingConsumer(channel); if (prefetchCount > 0) channel.basicQos(prefetchCount); channel.basicConsume(queueName, autoAck, consumer); channel.queueBind(queueName, exchangeName, id); Thread t = new Thread(new Consumer(consumer, id, consumerTxSize, autoAck, stats, timeLimit)); consumerThreads[i] = t; t.start(); } Thread[] producerThreads = new Thread[producerCount]; Connection[] producerConnections = new Connection[producerCount]; for (int i = 0; i < producerCount; i++) { System.out.println("starting producer #" + i); Connection conn = factory.newConnection(); producerConnections[i] = conn; Channel channel = conn.createChannel(); if (producerTxSize > 0) channel.txSelect(); channel.exchangeDeclare(exchangeName, exchangeType); final Producer p = new Producer(channel, exchangeName, id, flags, producerTxSize, 1000L * samplingInterval, rateLimit, minMsgSize, timeLimit, messageCount); channel.addReturnListener(p); Thread t = new Thread(p); producerThreads[i] = t; t.start(); } for (int i = 0; i < producerCount; i++) { producerThreads[i].join(); producerConnections[i].close(); } for (int i = 0; i < consumerCount; i++) { consumerThreads[i].join(); } for (int i = 0; i < connectionCount; i++) { consumerConnections[i].close(); } } catch (ParseException exp) { System.err.println("Parsing failed. Reason: " + exp.getMessage()); usage(options); } catch (Exception e) { System.err.println("Main thread caught exception: " + e); e.printStackTrace(); System.exit(1); } }
From source file:org.voltdb.bulkloader.RMQCSVReceive.java
License:Open Source License
public static void receiveMessages(RMQOptions rmqOpts, TestOptions testOpts, String[] args) throws IOException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(rmqOpts.host);//from w ww. j a v a 2s .co m Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); if (rmqOpts.exchange != null) { if (rmqOpts.extype != null) { channel.exchangeDeclare(rmqOpts.exchange, rmqOpts.extype); } for (String bindingKey : rmqOpts.bindings) { channel.queueBind(rmqOpts.queue, rmqOpts.exchange, bindingKey); } } try { channel.queueDeclare(rmqOpts.queue, rmqOpts.persistent, false, false, null); System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); channel.basicQos(1); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(rmqOpts.queue, false, consumer); while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String message = new String(delivery.getBody()); channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false); // Sleep 1 second for every trailing '.'. int dotCount = 0; for (int i = message.length() - 1; i >= 0; --i) { if (message.charAt(i) == '.') { dotCount++; } else { break; } } if (dotCount > 0) { message = message.substring(0, message.length() - dotCount); } System.out.printf(" [x] Received '%s'\n", message); Thread.sleep(dotCount * 1000); } } catch (ShutdownSignalException | ConsumerCancelledException | InterruptedException e) { e.printStackTrace(); } finally { channel.close(); connection.close(); } }
From source file:org.voltdb.bulkloader.RMQCSVSend.java
License:Open Source License
private static void sendMessages(RMQOptions rmqOpts, RandomSleeper.RSOptions sleeperOpts, TestOptions testOpts) throws InterruptedException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(rmqOpts.host);//from w w w .j a v a 2 s .com Connection connection = null; Channel channel = null; String exchangeName = ""; // Use the queue name if the routing key is not specified. String routingKey = rmqOpts.routing != null ? rmqOpts.routing : rmqOpts.queue; try { connection = factory.newConnection(); channel = connection.createChannel(); if (rmqOpts.exchange != null) { exchangeName = rmqOpts.exchange; channel.exchangeDeclare(exchangeName, rmqOpts.extype); } } catch (IOException e1) { e1.printStackTrace(); System.exit(255); } try { channel.queueDeclare(rmqOpts.queue, rmqOpts.persistent, false, false, null); try { while (testOpts.lineIter.hasNext()) { String message = testOpts.lineIter.next(); channel.basicPublish(exchangeName, routingKey, MessageProperties.TEXT_PLAIN, message.getBytes()); System.out.printf(" [x] Sent '%s'\n", message); sleeperOpts.sleeper.sleep(); } } finally { testOpts.lineIter.close(); } } catch (IOException e) { e.printStackTrace(); } finally { try { channel.close(); connection.close(); } catch (IOException e) { e.printStackTrace(); } } }