List of usage examples for com.rabbitmq.client Channel queueBind
Queue.BindOk queueBind(String queue, String exchange, String routingKey) throws IOException;
From source file:amqp.AmqpConsumer.java
License:Apache License
/** * This method declares the exchange, queue and bindings needed by this consumer. * The method returns the queue name that will be consumed from by this class. * * @param channel channel used to issue AMQP commands * @return queue that will have messages consumed from * @throws IOException thrown if there is any communication exception *///from www.j av a2 s.c o m protected String declarationsForChannel(Channel channel) throws IOException { // setup exchange, queue and binding channel.exchangeDeclare(exchangeName, exchangeType, durableExchange); // named queue? if (queueName == null) { queueName = channel.queueDeclare().getQueue(); } else { channel.queueDeclare(queueName, durable, exclusive, autoDelete, null); } if (bindings != null) { // multiple bindings for (String binding : bindings) { channel.queueBind(queueName, exchangeName, binding); } } else { // no binding given - this could be the case if it is a fanout exchange channel.queueBind(queueName, exchangeName, SERVER_GENERATED_QUEUE_NAME); } return queueName; }
From source file:bank.OurRabbitBank.java
public static void main(String[] args) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("datdb.cphbusiness.dk"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); String queueName = channel.queueDeclare().getQueue(); channel.queueBind(queueName, ExchangeName.GLOBAL, RoutingKeys.OUR_JSON_BANK); System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(queueName, true, consumer); while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); AMQP.BasicProperties properties = delivery.getProperties(); String message = new String(delivery.getBody()); Gson g = new Gson(); Message msg = g.fromJson(message, Message.class); System.out.println(" [x] Received '" + message + "'"); sendToNormalizer(msg, properties); }//w ww . j ava2 s . c o m }
From source file:blocker.Blocker.java
/** * @param argv/*from ww w . j av a2s.c om*/ */ public static void main(String[] argv) throws Exception { seconds = Integer.parseInt(argv[7]); ConnectionFactory factory = new ConnectionFactory(); factory.setHost(argv[0]); factory.setUsername(argv[2]); factory.setPassword(argv[3]); factory.setVirtualHost(argv[1]); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(argv[4], "direct", true); String queueName = channel.queueDeclare(argv[5], true, false, false, null).getQueue(); // exchange key channel.queueBind(queueName, argv[4], argv[6]); 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"); JSONParser parser = new JSONParser(); Thread.UncaughtExceptionHandler h = new Thread.UncaughtExceptionHandler() { @Override public void uncaughtException(Thread th, Throwable ex) { System.out.println("Uncaught exception: " + ex); } }; try { Object obj = parser.parse(message); JSONObject jobj = (JSONObject) obj; String IP = (String) jobj.get("clientip"); Thread t = new Thread(new BlockerThread(IP)); t.setUncaughtExceptionHandler(h); t.start(); } catch (ParseException ex) { Logger.getLogger(Blocker.class.getName()).log(Level.SEVERE, null, ex); } } }; channel.basicConsume(argv[5], true, consumer); }
From source file:brooklyn.entity.messaging.rabbit.RabbitEc2LiveTest.java
License:Apache License
@Override protected void doTest(Location loc) throws Exception { RabbitBroker rabbit = app.createAndManageChild(EntitySpec.create(RabbitBroker.class)); rabbit.start(ImmutableList.of(loc)); EntityTestUtils.assertAttributeEqualsEventually(rabbit, RabbitBroker.SERVICE_UP, true); byte[] content = "MessageBody".getBytes(Charsets.UTF_8); String queue = "queueName"; Channel producer = null; Channel consumer = null;/*from www . ja va2 s. co m*/ try { producer = getAmqpChannel(rabbit); consumer = getAmqpChannel(rabbit); producer.queueDeclare(queue, true, false, false, Maps.<String, Object>newHashMap()); producer.queueBind(queue, AmqpExchange.DIRECT, queue); producer.basicPublish(AmqpExchange.DIRECT, queue, null, content); QueueingConsumer queueConsumer = new QueueingConsumer(consumer); consumer.basicConsume(queue, true, queueConsumer); QueueingConsumer.Delivery delivery = queueConsumer.nextDelivery(); assertEquals(delivery.getBody(), content); } finally { if (producer != null) producer.close(); if (consumer != null) consumer.close(); } }
From source file:brooklyn.entity.messaging.rabbit.RabbitIntegrationTest.java
License:Apache License
/** * Test that an AMQP client can connect to and use the broker. *//* w w w. j av a 2s . co m*/ @Test(groups = { "Integration", "WIP" }) public void testClientConnection() throws Exception { rabbit = app.createAndManageChild(EntitySpec.create(RabbitBroker.class)); rabbit.start(ImmutableList.of(testLocation)); EntityTestUtils.assertAttributeEqualsEventually(rabbit, Startable.SERVICE_UP, true); byte[] content = "MessageBody".getBytes(Charsets.UTF_8); String queue = "queueName"; Channel producer = null; Channel consumer = null; try { producer = getAmqpChannel(rabbit); consumer = getAmqpChannel(rabbit); producer.queueDeclare(queue, true, false, false, ImmutableMap.<String, Object>of()); producer.queueBind(queue, AmqpExchange.DIRECT, queue); producer.basicPublish(AmqpExchange.DIRECT, queue, null, content); QueueingConsumer queueConsumer = new QueueingConsumer(consumer); consumer.basicConsume(queue, true, queueConsumer); QueueingConsumer.Delivery delivery = queueConsumer.nextDelivery(60 * 1000l); // one minute timeout assertEquals(delivery.getBody(), content); } finally { closeSafely(producer, 10 * 1000); closeSafely(consumer, 10 * 1000); } }
From source file:com.abiquo.commons.amqp.impl.am.AMConfiguration.java
License:Open Source License
@Override public void declareQueues(Channel channel) throws IOException { channel.queueDeclare(AM_QUEUE, Durable, NonExclusive, NonAutodelete, null); channel.queueBind(AM_QUEUE, AM_EXCHANGE, AM_ROUTING_KEY); }
From source file:com.abiquo.commons.amqp.impl.datacenter.DatacenterNotificationConfiguration.java
License:Open Source License
@Override public void declareQueues(Channel channel) throws IOException { channel.queueDeclare(NOTIFICATIONS_QUEUE, Durable, NonExclusive, NonAutodelete, null); channel.queueBind(NOTIFICATIONS_QUEUE, NOTIFICATIONS_EXCHANGE, NOTIFICATIONS_ROUTING_KEY); }
From source file:com.abiquo.commons.amqp.impl.datacenter.DatacenterRequestConfiguration.java
License:Open Source License
@Override public void declareQueues(Channel channel) throws IOException { channel.queueDeclare(buildJobsQueue(datacenterId, type), Durable, NonExclusive, NonAutodelete, null); channel.queueBind(buildJobsQueue(datacenterId, type), getDatacenterExchange(), buildJobsRoutingKey(datacenterId, type)); }
From source file:com.abiquo.commons.amqp.impl.ha.HAConfiguration.java
License:Open Source License
@Override public void declareQueues(Channel channel) throws IOException { channel.queueDeclare(HA_QUEUE, Durable, NonExclusive, NonAutodelete, null); channel.queueBind(HA_QUEUE, HA_EXCHANGE, HA_ROUTING_KEY); }
From source file:com.abiquo.commons.amqp.impl.tracer.TracerConfiguration.java
License:Open Source License
@Override public void declareQueues(Channel channel) throws IOException { channel.queueDeclare(TRACER_QUEUE, Durable, NonExclusive, NonAutodelete, null); channel.queueBind(TRACER_QUEUE, TRACER_EXCHANGE, TRACER_ROUTING_KEY); }