Java Code Examples for org.springframework.amqp.rabbit.core.RabbitAdmin#declareBinding()

The following examples show how to use org.springframework.amqp.rabbit.core.RabbitAdmin#declareBinding() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RabbitWithoutRabbitTemplateConfig.java    From java-spring-rabbitmq with Apache License 2.0 6 votes vote down vote up
@Bean
public RabbitAdmin rabbitAdmin(Queue queue, ConnectionFactory connectionFactory) {
  final TopicExchange exchange = new TopicExchange("myExchange", true, false);

  final RabbitAdmin admin = new RabbitAdmin(connectionFactory);
  admin.declareQueue(queue);
  admin.declareExchange(exchange);
  admin.declareBinding(BindingBuilder.bind(queue).to(exchange).with("#"));

  return admin;
}
 
Example 2
Source File: MQClient.java    From zxl with Apache License 2.0 6 votes vote down vote up
@Override
public void afterPropertiesSet() throws Exception {
	List<RabbitAdmin> rabbitAdmins = getMqAdmins();
	for (int i = 0; i < rabbitAdmins.size(); i++) {
		RabbitAdmin rabbitAdmin = rabbitAdmins.get(i);
		while (true) {
			if (!DEFAULT_EXCHANGE.equals(exchange)) {
				break;
			}
			try {
				rabbitAdmin.declareQueue(new Queue(routingKey, queueDurable, queueExclusive, queueAutoDelete, queueArguments));
				rabbitAdmin.declareBinding(new Binding(routingKey, DestinationType.QUEUE, MQClient.DEFAULT_EXCHANGE, routingKey, bindingArguments));
				break;
			} catch (Exception e) {
				LogUtil.warn(logger, "�������У�" + routingKey + "��ʧ��", e);
			}
		}
	}
}
 
Example 3
Source File: SpringIntegrationTest.java    From rabbitmq-mock with Apache License 2.0 5 votes vote down vote up
private RabbitTemplate queueAndExchangeSetup(BeanFactory context) {
    RabbitAdmin rabbitAdmin = context.getBean(RabbitAdmin.class);

    Queue queue = new Queue(QUEUE_NAME, false);
    rabbitAdmin.declareQueue(queue);
    TopicExchange exchange = new TopicExchange(EXCHANGE_NAME);
    rabbitAdmin.declareExchange(exchange);
    rabbitAdmin.declareBinding(BindingBuilder.bind(queue).to(exchange).with("test.*"));


    return context.getBean(RabbitTemplate.class);
}
 
Example 4
Source File: RabbitMqConfiguration.java    From Hands-On-High-Performance-with-Spring-5 with MIT License 5 votes vote down vote up
@Bean
public RabbitAdmin rabbitAdmin() {
	RabbitAdmin admin = new RabbitAdmin(connectionFactory());
	admin.declareQueue(queue());
	admin.declareExchange(exchange());
	admin.declareBinding(exchangeBinding(exchange(), queue()));
	return admin;
}
 
Example 5
Source File: RabbitBinderTests.java    From spring-cloud-stream-binder-rabbit with Apache License 2.0 5 votes vote down vote up
@Test
public void testConsumerPropertiesWithUserInfrastructureNoBind() throws Exception {
	RabbitAdmin admin = new RabbitAdmin(this.rabbitAvailableRule.getResource());
	Queue queue = new Queue("propsUser1.infra");
	admin.declareQueue(queue);
	DirectExchange exchange = new DirectExchange("propsUser1");
	admin.declareExchange(exchange);
	admin.declareBinding(BindingBuilder.bind(queue).to(exchange).with("foo"));

	RabbitTestBinder binder = getBinder();
	ExtendedConsumerProperties<RabbitConsumerProperties> properties = createConsumerProperties();
	properties.getExtension().setDeclareExchange(false);
	properties.getExtension().setBindQueue(false);

	Binding<MessageChannel> consumerBinding = binder.bindConsumer("propsUser1",
			"infra", createBindableChannel("input", new BindingProperties()),
			properties);
	Lifecycle endpoint = extractEndpoint(consumerBinding);
	SimpleMessageListenerContainer container = TestUtils.getPropertyValue(endpoint,
			"messageListenerContainer", SimpleMessageListenerContainer.class);
	assertThat(TestUtils.getPropertyValue(container, "missingQueuesFatal",
			Boolean.class)).isFalse();
	assertThat(container.isRunning()).isTrue();
	consumerBinding.unbind();
	assertThat(container.isRunning()).isFalse();
	Client client = new Client("http://guest:guest@localhost:15672/api/");
	List<?> bindings = client.getBindingsBySource("/", exchange.getName());
	assertThat(bindings.size()).isEqualTo(1);
}
 
Example 6
Source File: MQServer.java    From zxl with Apache License 2.0 5 votes vote down vote up
protected void handleReceiveError(Exception warnException, RabbitAdmin mqAdmin) {
	Throwable throwable = warnException;
	while ((throwable = throwable.getCause()) != null) {
		if (throwable instanceof ShutdownSignalException && throwable.getMessage().contains(NOT_FOUND_MESSAGE)) {
			try {
				mqAdmin.declareQueue(new Queue(queueName, queueDurable, queueExclusive, queueAutoDelete, queueArguments));
				mqAdmin.declareBinding(new Binding(queueName, DestinationType.QUEUE, MQClient.DEFAULT_EXCHANGE, queueName, bindingArguments));
			} catch (Exception e) {
			}
			break;
		}
	}
}
 
Example 7
Source File: ITSpringRabbit.java    From brave with Apache License 2.0 5 votes vote down vote up
@BeforeClass public static void startRabbit() {
  rabbit.start();
  CachingConnectionFactory connectionFactory =
    new CachingConnectionFactory(rabbit.getContainerIpAddress(), rabbit.getAmqpPort());
  try {
    RabbitAdmin amqpAdmin = new RabbitAdmin(connectionFactory);
    amqpAdmin.declareExchange(exchange);
    amqpAdmin.declareQueue(queue);
    amqpAdmin.declareBinding(binding);
  } finally {
    connectionFactory.destroy();
  }
}
 
Example 8
Source File: RabbitBinderTests.java    From spring-cloud-stream-binder-rabbit with Apache License 2.0 4 votes vote down vote up
@Test
public void testRoutingKeyExpression() throws Exception {
	RabbitTestBinder binder = getBinder();
	ExtendedProducerProperties<RabbitProducerProperties> producerProperties = createProducerProperties();
	producerProperties.getExtension().setRoutingKeyExpression(
			spelExpressionParser.parseExpression("payload.field"));

	DirectChannel output = createBindableChannel("output",
			createProducerBindingProperties(producerProperties));
	output.setBeanName("rkeProducer");
	Binding<MessageChannel> producerBinding = binder.bindProducer("rke", output,
			producerProperties);

	RabbitAdmin admin = new RabbitAdmin(this.rabbitAvailableRule.getResource());
	Queue queue = new AnonymousQueue();
	TopicExchange exchange = new TopicExchange("rke");
	org.springframework.amqp.core.Binding binding = BindingBuilder.bind(queue)
			.to(exchange).with("rkeTest");
	admin.declareQueue(queue);
	admin.declareBinding(binding);

	output.addInterceptor(new ChannelInterceptor() {

		@Override
		public Message<?> preSend(Message<?> message, MessageChannel channel) {
			assertThat(message.getHeaders()
					.get(RabbitExpressionEvaluatingInterceptor.ROUTING_KEY_HEADER))
							.isEqualTo("rkeTest");
			return message;
		}

	});

	output.send(new GenericMessage<>(new Pojo("rkeTest")));

	Object out = spyOn(queue.getName()).receive(false);
	assertThat(out).isInstanceOf(byte[].class);
	assertThat(new String((byte[]) out, StandardCharsets.UTF_8))
			.isEqualTo("{\"field\":\"rkeTest\"}");

	producerBinding.unbind();
}
 
Example 9
Source File: RabbitBinderTests.java    From spring-cloud-stream-binder-rabbit with Apache License 2.0 4 votes vote down vote up
@Test
public void testRoutingKeyExpressionPartitionedAndDelay() throws Exception {
	RabbitTestBinder binder = getBinder();
	ExtendedProducerProperties<RabbitProducerProperties> producerProperties = createProducerProperties();
	producerProperties.getExtension().setRoutingKeyExpression(
			spelExpressionParser.parseExpression("#root.getPayload().field"));
	// requires delayed message exchange plugin; tested locally
	// producerProperties.getExtension().setDelayedExchange(true);
	producerProperties.getExtension()
			.setDelayExpression(spelExpressionParser.parseExpression("1000"));
	producerProperties.setPartitionKeyExpression(new ValueExpression<>(0));

	DirectChannel output = createBindableChannel("output",
			createProducerBindingProperties(producerProperties));
	output.setBeanName("rkeProducer");
	Binding<MessageChannel> producerBinding = binder.bindProducer("rkep", output,
			producerProperties);

	RabbitAdmin admin = new RabbitAdmin(this.rabbitAvailableRule.getResource());
	Queue queue = new AnonymousQueue();
	TopicExchange exchange = new TopicExchange("rkep");
	org.springframework.amqp.core.Binding binding = BindingBuilder.bind(queue)
			.to(exchange).with("rkepTest-0");
	admin.declareQueue(queue);
	admin.declareBinding(binding);

	output.addInterceptor(new ChannelInterceptor() {

		@Override
		public Message<?> preSend(Message<?> message, MessageChannel channel) {
			assertThat(message.getHeaders()
					.get(RabbitExpressionEvaluatingInterceptor.ROUTING_KEY_HEADER))
							.isEqualTo("rkepTest");
			assertThat(message.getHeaders()
					.get(RabbitExpressionEvaluatingInterceptor.DELAY_HEADER))
							.isEqualTo(1000);
			return message;
		}

	});

	output.send(new GenericMessage<>(new Pojo("rkepTest")));

	Object out = spyOn(queue.getName()).receive(false);
	assertThat(out).isInstanceOf(byte[].class);
	assertThat(new String((byte[]) out, StandardCharsets.UTF_8))
			.isEqualTo("{\"field\":\"rkepTest\"}");

	producerBinding.unbind();
}
 
Example 10
Source File: RabbitMQUtils.java    From WeBASE-Front with Apache License 2.0 3 votes vote down vote up
/**
 * bind queue with routing key to existed exchange
 * @param rabbitAdmin
 * @param targetExchange
 * @param queueName
 * @param routingKey
 */
public static void bindQueue(RabbitAdmin rabbitAdmin,
                             DirectExchange targetExchange, String queueName, String routingKey){
    rabbitAdmin.declareQueue(new Queue(queueName));
    Binding binding = BindingBuilder.bind(new Queue(queueName)).to(targetExchange).with(routingKey);
    rabbitAdmin.declareBinding(binding);
}