List of usage examples for com.rabbitmq.client ConnectionFactory newConnection
public Connection newConnection() throws IOException, TimeoutException
From source file:dummyloanbroker.DummyLoanBroker.java
public static void main(String[] args) throws IOException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("datdb.cphbusiness.dk"); factory.setUsername("student"); factory.setPassword("cph"); com.rabbitmq.client.Connection connection = factory.newConnection(); com.rabbitmq.client.Channel channel = connection.createChannel(); String corrId = java.util.UUID.randomUUID().toString(); LoanRequestDTO loanRequest = new LoanRequestDTO("123456-7890", 456289.0, 25, -1); Gson gson = new Gson(); String message = gson.toJson(loanRequest); AMQP.BasicProperties props = new AMQP.BasicProperties.Builder().correlationId(corrId).build(); channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null); channel.basicPublish("", TASK_QUEUE_NAME, props, message.getBytes()); channel.close();/* ww w .j a v a 2 s .c om*/ connection.close(); }
From source file:edu.iit.rabbitmq.Queue.java
/** * * @return/*from w ww. j a va 2 s . c om*/ */ protected Channel getChannel() { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(RABBITMQ); Channel channel = null; try { Connection connection = factory.newConnection(); channel = connection.createChannel(); channel.queueDeclare(QUEUENAME, false, false, false, null); } catch (Exception e) { System.out.println("Queue already exists, moving on"); } return channel; }
From source file:edu.iit.rabbitmq.Send.java
/** * * @param message// ww w . ja va2 s .co m * @throws Exception */ public void sendMessage(String message) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(RABBITMQ); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); try { channel.queueDeclare(QUEUENAME, false, false, false, null); } catch (Exception e) { System.out.println("Queue already exists, moving on"); } channel.basicPublish("", QUEUENAME, null, message.getBytes("UTF-8")); System.out.println(" [x] Sent '" + message + "'"); channel.close(); connection.close(); }
From source file:edu.iu.messaging.service.core.impl.RabbitMQPublisher.java
License:Apache License
private void connect() { try {// www. jav a2s.co m logger.info("connect() -> Connecting to server"); ConnectionFactory connectionFactory = new ConnectionFactory(); connectionFactory.setUri(properties.getBrokerUrl()); connectionFactory.setAutomaticRecoveryEnabled(properties.isAutoRecoveryEnable()); connection = connectionFactory.newConnection(); connection.addShutdownListener(new ShutdownListener() { public void shutdownCompleted(ShutdownSignalException cause) { } }); channel = connection.createChannel(); /* Not required for work queue implementation */ //channel.basicQos(properties.getPrefetchCount()); //channel.exchangeDeclare(properties.getExchangeName(), properties.getExchangeType(), true); } catch (Exception e) { logger.error("connect() -> Error connecting to server.", e); } }
From source file:edu.iu.messaging.service.core.impl.RabbitMQSubscriber.java
License:Apache License
private void createConnection() { try {//from www .j a v a 2 s .c o m logger.info("createConnection() -> Connecting to server"); ConnectionFactory connectionFactory = new ConnectionFactory(); connectionFactory.setUri(properties.getBrokerUrl()); connectionFactory.setAutomaticRecoveryEnabled(properties.isAutoRecoveryEnable()); connection = connectionFactory.newConnection(); addShutdownListener(); channel = connection.createChannel(); /* Not required for work queue implementation */ //channel.basicQos(Constants.PREFETCH_COUT); //channel.exchangeDeclare(properties.getExchangeName(), properties.getExchangeType(), true); } catch (Exception e) { logger.error("createConnection() -> Error connecting to server.", e); } }
From source file:edu.kit.dama.mdm.audit.impl.RabbitMQPublisher.java
License:Apache License
@Override public boolean initialize() { boolean result = false; ConnectionFactory factory = new ConnectionFactory(); factory.setHost(hostname);//from w w w . ja va 2 s . c o m try { connection = factory.newConnection(); channel = connection.createChannel(); LOGGER.debug("Declaring exchange of type 'topic' in publisher."); channel.exchangeDeclare(exchangeName, "topic", true, false, false, null); LOGGER.debug("Declaring queue."); String queueName = channel.queueDeclare().getQueue(); LOGGER.debug("Binding queue with name {} to exchange.", queueName); channel.queueBind(queueName, exchangeName, ""); result = true; } catch (IOException | TimeoutException ex) { LOGGER.error("Failed to initialize RabbitMQPublisher.", ex); } return result; }
From source file:edu.kit.dama.mdm.audit.servlet.RabbitMQInitializerListener.java
License:Apache License
@Override public void contextInitialized(ServletContextEvent sce) { loadConfiguration();//from ww w . j a v a 2 s .com if (!CONFIGURED) { LOGGER.warn("Skipping initialization of RabbitMQ consumer."); return; } try { LOGGER.debug("Intitializing RabbitMQ consumer."); ConnectionFactory factory = new ConnectionFactory(); factory.setHost(hostname); connection = factory.newConnection(); channel = connection.createChannel(); LOGGER.debug("Declaring topic based exchange with name '{}'", exchange); channel.exchangeDeclare(exchange, "topic", true); String queueName = channel.queueDeclare().getQueue(); LOGGER.debug("Using queue with name '{}'. Binding queue to exchange.", queueName); channel.queueBind(queueName, exchange, "audit.*"); LOGGER.debug("Queue bound to exchange with filter 'audit.*'. Starting consumer."); 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"); try { LOGGER.debug("Handling new audit event."); AuditEvent event = AuditEvent.fromJson(message); LOGGER.debug("Submitting event to attached consumers."); for (AbstractAuditConsumer consumer : AuditManager.getInstance().getConsumers()) { consumer.consume(event); } LOGGER.debug("Event submitted to all consumers."); } catch (FormatException ex) { LOGGER.error("Failed to consume audit event from message '" + message + "'", ex); } } }; channel.basicConsume(queueName, true, consumer); LOGGER.debug("RabbitMQ consumer successfully initialized."); } catch (IOException | TimeoutException ex) { LOGGER.error("Failed to initialize RabbitMQ audit distributor.", ex); } }
From source file:edu.kit.dama.util.test.RabbitMQReceiver.java
License:Apache License
public static void main(String[] args) throws IOException, TimeoutException, InterruptedException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); final Connection connection = factory.newConnection(); final Channel channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME, "topic", true); String queueName = channel.queueDeclare().getQueue(); channel.queueBind(queueName, EXCHANGE_NAME, "audit.*"); // channel.queueDeclare(QUEUE_NAME, true, false, false, null); System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); /* final Consumer consumer = new DefaultConsumer(channel) { @Override/*from w w w . j a va 2 s .co 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 + "'"); try { doWork(message); } finally { System.out.println(" [x] Done"); channel.basicAck(envelope.getDeliveryTag(), false); } } }; channel.basicConsume(TASK_QUEUE_NAME, false, consumer); }*/ /* QueueingConsumer consumer = new QueueingConsumer(channel); /*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(queueName, true, consumer); QueueingConsumer.Delivery delivery = consumer.nextDelivery(10000); if (delivery != null) { byte[] message = delivery.getBody(); System.out.println("MESS " + new String(message)); }*/ 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(queueName, true, consumer); // System.exit(0); }
From source file:edu.kit.dama.util.test.RabbitMQTest.java
License:Apache License
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.exchangeDeclare(EXCHANGE_NAME, "fanout", true, false, false, null); //channel.queueDeclare(QUEUE_NAME, true, false, false, null); /*String queueName = channel.queueDeclare().getQueue(); channel.queueBind(queueName, EXCHANGE_NAME, "");*/ String message = "Hello!"; channel.basicPublish(EXCHANGE_NAME, "", MessageProperties.MINIMAL_PERSISTENT_BASIC, message.getBytes()); System.out.println(" [x] Sent '" + message + "'"); channel.close();/* w w w . ja va2 s. c o m*/ connection.close(); }
From source file:es.devcircus.rabbitmq_examples.rabbitmq_hello_world.Recv.java
License:Open Source License
/** * /*w ww. j a v a 2s. c o m*/ * @param argv * @throws Exception */ public static void main(String[] argv) throws Exception { 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"); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(QUEUE_NAME, true, consumer); while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String message = new String(delivery.getBody()); System.out.println(" [x] Received '" + message + "'"); } }