List of usage examples for com.rabbitmq.client ConnectionFactory ConnectionFactory
ConnectionFactory
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;// w w w .ja va 2 s . c o m 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.esri.geoevent.test.performance.rabbitmq.RabbitMQEventConsumer.java
License:Apache License
@Override public void init(Config config) throws TestException { super.init(config); uri = config.getPropertyValue("uri"); exchangeName = config.getPropertyValue("exchangeName"); queueName = config.getPropertyValue("queueName"); routingKey = config.getPropertyValue("routingKey"); if (uri == null) { throw new TestException("RabbitMQ event consumer ERROR: 'uri' property must be specified"); }/*from w ww. j a v a2 s. com*/ if (exchangeName == null) { throw new TestException("RabbitMQ event consumer ERROR: 'exchangeName' property must be specified"); } if (queueName == null) { throw new TestException("RabbitMQ event consumer ERROR: 'queueName' property must be specified"); } try { ConnectionFactory factory = new ConnectionFactory(); factory.setUri(uri); connection = factory.newConnection(); channel = connection.createChannel(); channel.queueDeclare(queueName, false, false, true, null); channel.queueBind(queueName, exchangeName, routingKey); // channel.basicQos(1); consumer = new QueueingConsumer(channel); channel.basicConsume(queueName, true, consumer); } catch (Exception error) { throw new TestException( ImplMessages.getMessage("INIT_FAILURE", getClass().getName(), error.getMessage()), error); } }
From source file:com.esri.geoevent.test.performance.rabbitmq.RabbitMQEventProducer.java
License:Apache License
@Override public void init(Config config) throws TestException { super.init(config); uri = config.getPropertyValue("uri"); exchangeName = config.getPropertyValue("exchangeName"); queueName = config.getPropertyValue("queueName"); routingKey = config.getPropertyValue("routingKey"); if (uri == null) { throw new TestException("RabbitMQ event producer ERROR: 'uri' property must be specified"); }/* w ww. ja v a 2s . c om*/ if (exchangeName == null) { throw new TestException("RabbitMQ event producer ERROR: 'exchangeName' property must be specified"); } if (queueName == null) { throw new TestException("RabbitMQ event producer ERROR: 'queueName' property must be specified"); } try { ConnectionFactory factory = new ConnectionFactory(); factory.setUri(uri); connection = factory.newConnection(); channel = connection.createChannel(); channel.queueDeclare(queueName, false, false, true, null); channel.queueBind(queueName, exchangeName, routingKey); } catch (Exception error) { throw new TestException( ImplMessages.getMessage("INIT_FAILURE", getClass().getName(), error.getMessage()), error); } }
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 av a2s . 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.eventbook.controller.SpringbootPocController.java
@RequestMapping(value = "/rabbitMQReceiveTest", method = RequestMethod.GET) public String rabbitMQReceiveTest() { try {/*w ww. ja va2 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.examples.rabbit.producer.ConfirmDontLoseMessages.java
License:Mozilla Public License
public static void main(String[] args) throws IOException, InterruptedException { if (args.length > 0) { msgCount = Integer.parseInt(args[0]); }//w w w . j a v a 2s. co m connectionFactory = new ConnectionFactory(); // Consume msgCount messages. (new Thread(new Consumer())).start(); // Publish msgCount messages and wait for confirms. (new Thread(new Publisher())).start(); }
From source file:com.flipkart.bifrost.framework.Connection.java
License:Apache License
/** * Start the connection. This will try to connect to the cluster hosts provided in the contructor. * Will throw an error in case of failure. * @throws BifrostException in case of connection failure. *///ww w . j a va 2 s . c om public void start() throws BifrostException { ConnectionFactory connectionFactory = new ConnectionFactory(); Address brokers[] = new Address[hosts.size()]; int i = 0; for (String member : hosts) { brokers[i++] = new Address(member); } connectionFactory.setUsername(username); connectionFactory.setPassword(password); connectionFactory.setAutomaticRecoveryEnabled(true); connectionFactory.setTopologyRecoveryEnabled(true); try { connection = connectionFactory.newConnection(brokers); } catch (IOException e) { throw new BifrostException(BifrostException.ErrorCode.IO_ERROR, "Could not connect", e); } }
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 a v a 2 s. c o 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 w w . j a v a 2s. c o m*/ connection.close(); }
From source file:com.gdp.rabbitmq.bench.SharedConnectionPublisherBench.java
public SharedConnectionPublisherBench() { try {/*from w w w. java 2 s .c om*/ final ConnectionFactory factory = new ConnectionFactory(); factory.setHost("127.0.0.1"); connection = factory.newConnection(); } catch (IOException ex) { Logger.getLogger(SharedConnectionBench.class.getName()).log(Level.SEVERE, null, ex); } }