channel.queueDeclare(queueName, true, false, false, null); if (exchangeBind) { channel.queueBind(queueName, exchange, routingKey);
@Test public void simpleRabbitMqTest() throws IOException, TimeoutException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost(rabbitMq.getContainerIpAddress()); factory.setPort(rabbitMq.getMappedPort(RABBITMQ_PORT)); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(RABBIQMQ_TEST_EXCHANGE, "direct", true); String queueName = channel.queueDeclare().getQueue(); channel.queueBind(queueName, RABBIQMQ_TEST_EXCHANGE, RABBITMQ_TEST_ROUTING_KEY); // Set up a consumer on the queue final boolean[] messageWasReceived = new boolean[1]; channel.basicConsume(queueName, false, new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { messageWasReceived[0] = Arrays.equals(body, RABBITMQ_TEST_MESSAGE.getBytes()); } }); // post a message channel.basicPublish(RABBIQMQ_TEST_EXCHANGE, RABBITMQ_TEST_ROUTING_KEY, null, RABBITMQ_TEST_MESSAGE.getBytes()); // check the message was received assertTrue("The message was received", Unreliables.retryUntilSuccess(5, TimeUnit.SECONDS, () -> { if (!messageWasReceived[0]) { throw new IllegalStateException("Message not received yet"); } return true; })); }
channel.queueBind(queue, exchange, routingKey); channel.addShutdownListener( new ShutdownListener()
public void listen(Event evt, EventListenerWrapper l) { String type = evt.getType().toString(); try { synchronized (listeners) { List<EventListenerWrapper> lst = listeners.get(type); if (lst == null) { lst = new CopyOnWriteArrayList<EventListenerWrapper>(); listeners.put(type, lst); eventChan.queueBind(queueName, BusExchange.BROADCAST.toString(), type); logger.debug(String.format("[listening event]: %s", type)); } if (!lst.contains(l)) { lst.add(l); } } } catch (IOException e) { throw new CloudRuntimeException(e); } }
void trackService(String servId) { if (trackerClose) { return; } String[] pairs = servId.split("\\."); if (pairs.length < 2) { // don't track services not using management node id in service id return; } pairs[pairs.length-1] = "*"; String bindingKey = makeMessageQueueName(StringUtils.join(pairs, ".")); bindingKeys.add(bindingKey); if (logger.isTraceEnabled()) { logger.trace(String.format("message tracker binds to key[%s], tracking service[%s]", bindingKey, pairs[0])); } try { Channel chan = channelPool.acquire(); try { chan.queueBind(name, BusExchange.P2P.toString(), bindingKey); } finally { channelPool.returnChannel(chan); } } catch (IOException e) { throw new CloudRuntimeException(e); } }
void init() { try { ConnectionFactory connFactory = new ConnectionFactory(); List<Address> addresses = CollectionUtils.transformToList(bus.getServerIps(), new Function<Address, String>() { @Override public Address call(String arg) { return Address.parseAddress(arg); } }); conn = connFactory.newConnection(addresses.toArray(new Address[]{})); chan = conn.createChannel(); String name = MessageTracker.class.getName(); chan.queueDeclare(name, true, false, true, null); chan.basicConsume(name, true, this); chan.queueBind(name, BusExchange.P2P.toString(), "#"); chan.queueBind(name, BusExchange.BROADCAST.toString(), "#"); } catch (Exception e) { throw new CloudRuntimeException(e); } }
public void construct() { try { nrouteChan = conn.createChannel(); nrouteChan.queueDeclare(nrouteName, false, false, true, null); nrouteChan.queueBind(nrouteName, BusExchange.NO_ROUTE.toString(), ""); nrouteChan.basicConsume(nrouteName, true, this); } catch (IOException e) { throw new CloudRuntimeException(e); } }
@Override public com.rabbitmq.client.AMQP.Queue.BindOk queueBind(String queue, String exchange, String routingKey) throws IOException { return this.delegate.queueBind(queue, exchange, routingKey); }
@Override public com.rabbitmq.client.AMQP.Queue.BindOk queueBind(String queue, String exchange, String routingKey, Map<String, Object> arguments) throws IOException { return this.delegate.queueBind(queue, exchange, routingKey, arguments); }
protected void addQueueBinding(Channel on, String binding) throws IOException { on.queueBind(this.queue, "bergamot.update", binding); } };
@Override protected void addQueueBinding(Channel on, String binding) throws IOException { on.queueBind(this.queue, "bergamot.scheduler", binding); }
chan.queueDeclare(name, false, false, true, null); chan.basicConsume(name, true, tracker); chan.queueBind(name, BusExchange.BROADCAST.toString(), "#"); } finally { channelPool.returnChannel(chan);
chan.queueDeclare(outboundQueue.getName(), false, false, true, queueArguments()); chan.basicConsume(outboundQueue.getName(), true, consumer); chan.queueBind(outboundQueue.getName(), outboundQueue.getBusExchange().toString(), outboundQueue.getBindingKey()); channelPool.returnChannel(chan); maid.construct();
@Override protected void removeQueueBinding(Channel on, String binding) throws IOException { // seems odd, but unbind is non-idempotent, binding then unbinding is a poor workaround on.queueBind(this.queue, "bergamot.result", binding); on.queueUnbind(this.queue, "bergamot.result", binding); } };
protected String setupQueue(Channel on) throws IOException { String queueName = on.queueDeclare().getQueue(); on.exchangeDeclare("bergamot.update", "topic", true); on.queueBind(queueName, "bergamot.update", (site == null ? "*" : site.toString()) + "." + (check == null ? "*" : check.toString())); return queueName; }
public String setupQueue(Channel on) throws IOException { on.queueDeclare("bergamot.reading.fallback_queue", true, false, false, null); on.exchangeDeclare("bergamot.reading.fallback", "fanout", true, false, null); on.queueBind("bergamot.reading.fallback_queue", "bergamot.reading.fallback", ""); return "bergamot.reading.fallback_queue"; } };
public String setupQueue(Channel on) throws IOException { // exchange on.exchangeDeclare("bergamot.notification", "topic", true); // queue String queueName = on.queueDeclare().getQueue(); // bind using the site on.queueBind(queueName, "bergamot.notification", site == null ? "#" : site.toString()); return queueName; } };
protected String setupExchange(Channel on) throws IOException { // the fallback queue on.queueDeclare("bergamot.reading.fallback_queue", true, false, false, null); on.exchangeDeclare("bergamot.reading.fallback", "fanout", true, false, null); on.queueBind("bergamot.reading.fallback_queue", "bergamot.reading.fallback", ""); // the result exchange on.exchangeDeclare("bergamot.reading", "topic", true, false, args("alternate-exchange", "bergamot.reading.fallback")); return "bergamot.reading"; } };
static void declareTestQueue(RabbitTemplate template, final String routingKey) { // declare and bind queue template.execute(channel -> { Queue.DeclareOk res = channel.queueDeclarePassive(TestConstants.QUEUE_NAME); String queueName = res.getQueue(); System .out .println("Queue Name = " + queueName); channel.queueBind(queueName, TestConstants.EXCHANGE_NAME, routingKey); return queueName; }); }
@Override public void open() throws IOException { getChannel().exchangeDeclare(exchange, "direct"); getChannel().queueDeclare(queueName, true, false, false, null); for (String routingKey : routingKeys) { getChannel().queueBind(queueName, exchange, routingKey); } getChannel().basicConsume(queueName, false, this); }