List of usage examples for com.rabbitmq.client ConnectionFactory setHost
public void setHost(String host)
From source file:com.espertech.esperio.amqp.AMQPSink.java
License:Open Source License
public void open(DataFlowOpOpenContext openContext) { log.info("Opening AMQP, settings are: " + settings.toString()); try {//from w w w . ja v a 2 s .c om final ConnectionFactory connectionFactory = new ConnectionFactory(); connectionFactory.setHost(settings.getHost()); if (settings.getPort() > -1) { connectionFactory.setPort(settings.getPort()); } if (settings.getUsername() != null) { connectionFactory.setUsername(settings.getUsername()); } if (settings.getPassword() != null) { connectionFactory.setPassword(settings.getPassword()); } if (settings.getVhost() != null) { connectionFactory.setVirtualHost(settings.getVhost()); } connection = connectionFactory.newConnection(); channel = connection.createChannel(); if (settings.getExchange() != null) { channel.exchangeDeclarePassive(settings.getExchange()); } final AMQP.Queue.DeclareOk queue; if (settings.getQueueName() == null || settings.getQueueName().trim().length() == 0) { queue = channel.queueDeclare(); } else { // java.lang.String queue,boolean durable,boolean exclusive,boolean autoDelete,java.util.Map<java.lang.String,java.lang.Object> arguments) throws java.io.IOException queue = channel.queueDeclare(settings.getQueueName(), settings.isDeclareDurable(), settings.isDeclareExclusive(), settings.isDeclareAutoDelete(), settings.getDeclareAdditionalArgs()); } if (settings.getExchange() != null && settings.getRoutingKey() != null) { channel.queueBind(queue.getQueue(), settings.getExchange(), settings.getRoutingKey()); } final String queueName = queue.getQueue(); log.info("AMQP producing queue is " + queueName + (settings.isLogMessages() ? " with logging" : "")); } catch (IOException e) { String message = "AMQP setup failed: " + e.getMessage(); log.error(message, e); throw new RuntimeException(message, e); } }
From source file:com.espertech.esperio.amqp.AMQPSource.java
License:Open Source License
public void open(DataFlowOpOpenContext openContext) { log.info("Opening AMQP, settings are: " + settings.toString()); try {//www .ja v a 2 s .com final ConnectionFactory connectionFactory = new ConnectionFactory(); connectionFactory.setHost(settings.getHost()); if (settings.getPort() > -1) { connectionFactory.setPort(settings.getPort()); } if (settings.getUsername() != null) { connectionFactory.setUsername(settings.getUsername()); } if (settings.getPassword() != null) { connectionFactory.setPassword(settings.getPassword()); } if (settings.getVhost() != null) { connectionFactory.setVirtualHost(settings.getVhost()); } connection = connectionFactory.newConnection(); channel = connection.createChannel(); channel.basicQos(settings.getPrefetchCount()); if (settings.getExchange() != null) { channel.exchangeDeclarePassive(settings.getExchange()); } final AMQP.Queue.DeclareOk queue; if (settings.getQueueName() == null || settings.getQueueName().trim().length() == 0) { queue = channel.queueDeclare(); } else { // java.lang.String queue,boolean durable,boolean exclusive,boolean autoDelete,java.util.Map<java.lang.String,java.lang.Object> arguments) throws java.io.IOException queue = channel.queueDeclare(settings.getQueueName(), settings.isDeclareDurable(), settings.isDeclareExclusive(), settings.isDeclareAutoDelete(), settings.getDeclareAdditionalArgs()); } if (settings.getExchange() != null && settings.getRoutingKey() != null) { channel.queueBind(queue.getQueue(), settings.getExchange(), settings.getRoutingKey()); } final String queueName = queue.getQueue(); log.info("AMQP consuming queue " + queueName + (settings.isLogMessages() ? " with logging" : "")); consumer = new QueueingConsumer(channel); consumerTag = channel.basicConsume(queueName, settings.isConsumeAutoAck(), consumer); } catch (IOException e) { String message = "AMQP source setup failed: " + e.getMessage(); log.error(message, e); throw new EPException(message, e); } }
From source file:com.espertech.esperio.amqp.AMQPSupportReceiveRunnable.java
License:Open Source License
public void run() { try {/*from ww w . ja v a2 s. c o m*/ ConnectionFactory factory = new ConnectionFactory(); factory.setHost(hostName); Connection connection = factory.newConnection(); Channel 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); log.info("Start receiving messages"); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(queueName, true, consumer); int count = 0; while (true) { final QueueingConsumer.Delivery msg = consumer.nextDelivery(waitMSecNextMsg); if (msg == null) { continue; } final byte[] bytes = msg.getBody(); callback.handleMessage(bytes); if (isShutdown()) { break; } } log.info("Completed publishing messages: " + count + " messages"); } catch (Exception ex) { log.error("Error attaching to AMQP: " + ex.getMessage(), ex); } }
From source file:com.espertech.esperio.amqp.AMQPSupportSendRunnable.java
License:Open Source License
public void run() { try {//w w w . ja v a 2 s.c o m ConnectionFactory factory = new ConnectionFactory(); factory.setHost(hostName); Connection connection = factory.newConnection(); Channel 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); log.info("Start publishing messages: " + events.size() + " messages"); int count = 0; while (true) { if (events.isEmpty()) { break; } Object next = events.remove(0); byte[] bytes = SerializerUtil.objectToByteArr(next); channel.basicPublish("", queueName, null, bytes); count++; log.info("Publishing message #" + count + ": " + next); Thread.sleep(msecSleepTime); if (isShutdown()) { break; } } log.info("Completed publishing messages: " + count + " messages"); } catch (Exception ex) { log.error("Error attaching to AMQP: " + ex.getMessage(), 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;//from w w w . j a v a 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.eventbook.controller.SpringbootPocController.java
@RequestMapping(value = "/rabbitMQSendTest", method = RequestMethod.GET) public String rabbitMQSendTest(@RequestParam(value = "message", defaultValue = "Hello World!") String message) { try {//from ww w. jav a 2 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); 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 . j av a2 s . c o 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.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 ww .java 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 ww w .ja v a 2 s .c o m*/ connection.close(); }
From source file:com.gdp.rabbitmq.bench.SharedConnectionPublisherBench.java
public SharedConnectionPublisherBench() { try {// ww w . j ava 2 s . com 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); } }