/** * The message consumer for the service responses. * * @return The message consumer for the service responses. * * @since 1.0 */ @Bean("name=capabilityRegistryServiceConsumer") IAmqpCapabilityRegistryServiceConsumer capabilityRegistryServiceConsumer() { final String replyTo = this.capabilityRegistryResponseQueueBinding.getRoutingKey(); return new AmqpCapabilityRegistryServiceConsumer(replyTo); }
/** * The message consumer for the service control messages. * * @return The message consumer for the service responses. * * @since 1.0 */ @Bean IAmqpCapabilityRegistryControlConsumer capabilityRegistryControlConsumer() { final String replyTo = this.capabilityRegistryControlQueueBinding.getRoutingKey(); return new AmqpCapabilityRegistryControlConsumer(replyTo, this.capabilityRegistryControlProducer); }
/** * Add a binding * */ public RabbitContextBuilder add(Binding binding) { bindings.put(Arrays.toString(new String[] {binding.getExchange(), binding.getExchange(), binding.getRoutingKey()}), binding); return this; }
/** * Add a binding * */ public RabbitContextBuilder add(Binding binding) { bindings.put(Arrays.toString(new String[] {binding.getExchange(), binding.getExchange(), binding.getRoutingKey()}), binding); return this; }
private boolean isImplicitQueueBinding(Binding binding) { return isDefaultExchange(binding.getExchange()) && binding.getDestination().equals(binding.getRoutingKey()); }
private boolean isImplicitQueueBinding(Binding binding) { return isDefaultExchange(binding.getExchange()) && binding.getDestination().equals(binding.getRoutingKey()); }
private void declareBindings(final Channel channel, final Binding... bindings) throws IOException { for (Binding binding : bindings) { if (this.logger.isDebugEnabled()) { this.logger.debug("Binding destination [" + binding.getDestination() + " (" + binding.getDestinationType() + ")] to exchange [" + binding.getExchange() + "] with routing key [" + binding.getRoutingKey() + "]"); } try { if (binding.isDestinationQueue()) { if (!isDeclaringImplicitQueueBinding(binding)) { channel.queueBind(binding.getDestination(), binding.getExchange(), binding.getRoutingKey(), binding.getArguments()); } } else { channel.exchangeBind(binding.getDestination(), binding.getExchange(), binding.getRoutingKey(), binding.getArguments()); } } catch (IOException e) { logOrRethrowDeclarationException(binding, "binding", e); } } }
private void declareBindings(final Channel channel, final Binding... bindings) throws IOException { for (Binding binding : bindings) { if (this.logger.isDebugEnabled()) { this.logger.debug("Binding destination [" + binding.getDestination() + " (" + binding.getDestinationType() + ")] to exchange [" + binding.getExchange() + "] with routing key [" + binding.getRoutingKey() + "]"); } try { if (binding.isDestinationQueue()) { if (!isDeclaringImplicitQueueBinding(binding)) { channel.queueBind(binding.getDestination(), binding.getExchange(), binding.getRoutingKey(), binding.getArguments()); } } else { channel.exchangeBind(binding.getDestination(), binding.getExchange(), binding.getRoutingKey(), binding.getArguments()); } } catch (IOException e) { logOrRethrowDeclarationException(binding, "binding", e); } } }
protected Binding declareBinding(org.springframework.amqp.core.Exchange exchange, Queue queue) { Binding binding = null; //Is this a header exchange? Bind the key/value pair(s) if(exchange instanceof HeadersExchange) { if(this.endpoint.routingKey == null) throw new IllegalStateException("Specified a header exchange without a key/value match"); if(this.endpoint.routingKey.contains("|") && this.endpoint.routingKey.contains("&")) throw new IllegalArgumentException("You cannot mix AND and OR expressions within a header binding"); Map<String, Object> keyValues = parseKeyValues(this.endpoint.routingKey); BindingBuilder.HeadersExchangeMapConfigurer mapConfig = BindingBuilder.bind(queue).to((HeadersExchange) exchange); if(this.endpoint.routingKey.contains("|")) binding = mapConfig.whereAny(keyValues).match(); else binding = mapConfig.whereAll(keyValues).match(); //Is this a fanout exchange? Just bind the queue and exchange directly } else if(exchange instanceof FanoutExchange) { binding = BindingBuilder.bind(queue).to((FanoutExchange) exchange); //Perform routing key binding for direct or topic exchanges } else { binding = BindingBuilder.bind(queue).to(exchange).with(this.endpoint.routingKey).noargs(); } if (this.endpoint.isUsingDefaultExchange()) { LOG.info("Using default exchange for endpoint {}. Default exchange is implicitly bound to every queue, with a routing key equal to the queue name.", endpoint); } else if (binding != null) { LOG.info("Declaring binding {} for endpoint {}.", binding.getRoutingKey(), endpoint); this.endpoint.getAmqpAdministration().declareBinding(binding); } return binding; } }
@Override @ManagedOperation(description = "Remove a binding from the broker (this operation is not available remotely)") public void removeBinding(final Binding binding) { this.rabbitTemplate.execute(channel -> { if (binding.isDestinationQueue()) { if (isRemovingImplicitQueueBinding(binding)) { return null; } channel.queueUnbind(binding.getDestination(), binding.getExchange(), binding.getRoutingKey(), binding.getArguments()); } else { channel.exchangeUnbind(binding.getDestination(), binding.getExchange(), binding.getRoutingKey(), binding.getArguments()); } return null; }); }
@Override @ManagedOperation(description = "Remove a binding from the broker (this operation is not available remotely)") public void removeBinding(final Binding binding) { this.rabbitTemplate.execute(channel -> { if (binding.isDestinationQueue()) { if (isRemovingImplicitQueueBinding(binding)) { return null; } channel.queueUnbind(binding.getDestination(), binding.getExchange(), binding.getRoutingKey(), binding.getArguments()); } else { channel.exchangeUnbind(binding.getDestination(), binding.getExchange(), binding.getRoutingKey(), binding.getArguments()); } return null; }); }
/** * Register a produce message class and consumme class pair. This is useful because a listener queue will be bound with the * producer routing key for replies * */ public <P> RabbitContextBuilder requestsAndReplies(Class<P> requestClass, String queueName, boolean durable, String containerAlias, Object listener, Class<?>... replyClasses) { produces(requestClass); MessageDescription<P> requestDescription = messageDescriptionFactory.createDescription(requestClass); descriptions.put(requestDescription.getType(), requestDescription); for (Class<?> replyClass : replyClasses) { MessageDescription replyDescription = messageDescriptionFactory.createDescription(replyClass); descriptions.put(replyDescription.getType(), replyDescription); addQueueData(containerAlias, queueName, listener); MessageBindingBuilder replyBindingBuilder = new MessageBindingBuilder(this, resolveRoutingKey(replyDescription.getStereotype(), requestDescription.getRoutingKey(), consumerPostfix)); Binding replyBinding = replyBindingBuilder.fromExchange(replyDescription.getExchange(), replyDescription.getExchangeType()) .toQueue(queueName, durable).bind(); replyToMap.put(new RequestReplyKey(requestClass, replyClass), replyBinding.getRoutingKey()); } return this; }
/** * Register a produce message class and consumme class pair. This is useful because a listener queue will be bound with the * producer routing key for replies * */ public <P> RabbitContextBuilder requestsAndReplies(Class<P> requestClass, String queueName, boolean durable, String containerAlias, Object listener, Class<?>... replyClasses) { produces(requestClass); MessageDescription<P> requestDescription = messageDescriptionFactory.createDescription(requestClass); descriptions.put(requestDescription.getType(), requestDescription); for (Class<?> replyClass : replyClasses) { MessageDescription replyDescription = messageDescriptionFactory.createDescription(replyClass); descriptions.put(replyDescription.getType(), replyDescription); addQueueData(containerAlias, queueName, listener); MessageBindingBuilder replyBindingBuilder = new MessageBindingBuilder(this, resolveRoutingKey(replyDescription.getStereotype(), requestDescription.getRoutingKey(), consumerPostfix)); Binding replyBinding = replyBindingBuilder.fromExchange(replyDescription.getExchange(), replyDescription.getExchangeType()) .toQueue(queueName, durable).bind(); replyToMap.put(new RequestReplyKey(requestClass, replyClass), replyBinding.getRoutingKey()); } return this; }
@Test public void customBinding() { class CustomExchange extends AbstractExchange { CustomExchange(String name) { super(name); } @Override public String getType() { return "x-custom"; } } Object argumentObject = new Object(); CustomExchange customExchange = new CustomExchange("c"); String routingKey = "r"; Binding binding = BindingBuilder.// bind(queue).// to(customExchange).// with(routingKey).// and(Collections.<String, Object>singletonMap("k", argumentObject)); assertNotNull(binding); assertEquals(argumentObject, binding.getArguments().get("k")); assertEquals(customExchange.getName(), binding.getExchange()); assertEquals(Binding.DestinationType.QUEUE, binding.getDestinationType()); assertEquals(queue.getName(), binding.getDestination()); assertEquals(routingKey, binding.getRoutingKey()); }
@Test public void fanoutBinding() { FanoutExchange fanoutExchange = new FanoutExchange("f"); Binding binding = BindingBuilder.bind(queue).to(fanoutExchange); assertNotNull(binding); assertEquals(fanoutExchange.getName(), binding.getExchange()); assertEquals("", binding.getRoutingKey()); assertEquals(Binding.DestinationType.QUEUE, binding.getDestinationType()); assertEquals(queue.getName(), binding.getDestination()); }
@Test public void directBinding() { DirectExchange directExchange = new DirectExchange("d"); String routingKey = "r"; Binding binding = BindingBuilder.bind(queue).to(directExchange).with(routingKey); assertNotNull(binding); assertEquals(directExchange.getName(), binding.getExchange()); assertEquals(Binding.DestinationType.QUEUE, binding.getDestinationType()); assertEquals(queue.getName(), binding.getDestination()); assertEquals(routingKey, binding.getRoutingKey()); }
@Test public void topicBinding() { TopicExchange topicExchange = new TopicExchange("t"); String routingKey = "r"; Binding binding = BindingBuilder.bind(queue).to(topicExchange).with(routingKey); assertNotNull(binding); assertEquals(topicExchange.getName(), binding.getExchange()); assertEquals(Binding.DestinationType.QUEUE, binding.getDestinationType()); assertEquals(queue.getName(), binding.getDestination()); assertEquals(routingKey, binding.getRoutingKey()); }
@Test public void exchangeBinding() { DirectExchange directExchange = new DirectExchange("d"); FanoutExchange fanoutExchange = new FanoutExchange("f"); Binding binding = BindingBuilder.bind(directExchange).to(fanoutExchange); assertNotNull(binding); assertEquals(fanoutExchange.getName(), binding.getExchange()); assertEquals(Binding.DestinationType.EXCHANGE, binding.getDestinationType()); assertEquals(directExchange.getName(), binding.getDestination()); assertEquals("", binding.getRoutingKey()); }
@Test public void directBindingWithQueueName() { DirectExchange directExchange = new DirectExchange("d"); Binding binding = BindingBuilder.bind(queue).to(directExchange).withQueueName(); assertNotNull(binding); assertEquals(directExchange.getName(), binding.getExchange()); assertEquals(Binding.DestinationType.QUEUE, binding.getDestinationType()); assertEquals(queue.getName(), binding.getDestination()); assertEquals(queue.getName(), binding.getRoutingKey()); }
@Test public void headerBinding() { HeadersExchange headersExchange = new HeadersExchange("h"); String headerKey = "headerKey"; Binding binding = BindingBuilder.bind(queue).to(headersExchange).where(headerKey).exists(); assertNotNull(binding); assertEquals(headersExchange.getName(), binding.getExchange()); assertEquals(Binding.DestinationType.QUEUE, binding.getDestinationType()); assertEquals(queue.getName(), binding.getDestination()); assertEquals("", binding.getRoutingKey()); }