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

The following examples show how to use org.springframework.amqp.rabbit.core.RabbitAdmin#declareQueue() . 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: EMSConfiguration.java    From generic-vnfm with Apache License 2.0 6 votes vote down vote up
@PostConstruct
private void init() {
  log.info("Initialization of RabbitConfiguration");

  emsConnectionFactory = new CachingConnectionFactory();
  ((CachingConnectionFactory) emsConnectionFactory).setHost(brokerIp);
  ((CachingConnectionFactory) emsConnectionFactory).setPort(rabbitPort);
  ((CachingConnectionFactory) emsConnectionFactory).setUsername(emsRabbitUsername);
  ((CachingConnectionFactory) emsConnectionFactory).setPassword(emsRabbitPassword);

  rabbitAdmin = new RabbitAdmin(emsConnectionFactory);
  TopicExchange topicExchange = new TopicExchange("openbaton-exchange");
  rabbitAdmin.declareExchange(topicExchange);
  log.info("exchange declared");

  queueName_emsRegistrator = "ems." + vnfmHelper.getVnfmEndpoint() + ".register";
  rabbitAdmin.declareQueue(new Queue(queueName_emsRegistrator, durable, exclusive, autodelete));
}
 
Example 3
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 4
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 5
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 6
Source File: LocalizedQueueConnectionFactoryIntegrationTests.java    From spring-cloud-stream-binder-rabbit with Apache License 2.0 5 votes vote down vote up
@Test
public void testConnect() {
	RabbitAdmin admin = new RabbitAdmin(this.lqcf);
	Queue queue = new Queue(UUID.randomUUID().toString(), false, false, true);
	admin.declareQueue(queue);
	ConnectionFactory targetConnectionFactory = this.lqcf
			.getTargetConnectionFactory("[" + queue.getName() + "]");
	RabbitTemplate template = new RabbitTemplate(targetConnectionFactory);
	template.convertAndSend("", queue.getName(), "foo");
	assertThat(template.receiveAndConvert(queue.getName())).isEqualTo("foo");
	((CachingConnectionFactory) targetConnectionFactory).destroy();
	admin.deleteQueue(queue.getName());
}
 
Example 7
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 8
Source File: RabbitBinderTests.java    From spring-cloud-stream-binder-rabbit with Apache License 2.0 5 votes vote down vote up
@Test
public void testBadUserDeclarationsFatal() throws Exception {
	RabbitTestBinder binder = getBinder();
	ConfigurableApplicationContext context = binder.getApplicationContext();
	ConfigurableListableBeanFactory bf = context.getBeanFactory();
	bf.registerSingleton("testBadUserDeclarationsFatal",
			new Queue("testBadUserDeclarationsFatal", false));
	bf.registerSingleton("binder", binder);
	RabbitExchangeQueueProvisioner provisioner = TestUtils.getPropertyValue(binder,
			"binder.provisioningProvider", RabbitExchangeQueueProvisioner.class);
	bf.initializeBean(provisioner, "provisioner");
	bf.registerSingleton("provisioner", provisioner);
	context.addApplicationListener(provisioner);
	RabbitAdmin admin = new RabbitAdmin(rabbitAvailableRule.getResource());
	admin.declareQueue(new Queue("testBadUserDeclarationsFatal"));
	// reset the connection and configure the "user" admin to auto declare queues...
	rabbitAvailableRule.getResource().resetConnection();
	bf.initializeBean(admin, "rabbitAdmin");
	bf.registerSingleton("rabbitAdmin", admin);
	admin.afterPropertiesSet();
	// the mis-configured queue should be fatal
	Binding<?> binding = null;
	try {
		binding = binder.bindConsumer("input", "baddecls",
				this.createBindableChannel("input", new BindingProperties()),
				createConsumerProperties());
		fail("Expected exception");
	}
	catch (BinderException e) {
		assertThat(e.getCause()).isInstanceOf(AmqpIOException.class);
	}
	finally {
		admin.deleteQueue("testBadUserDeclarationsFatal");
		if (binding != null) {
			binding.unbind();
		}
	}
}
 
Example 9
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 10
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 11
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 12
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 13
Source File: RabbitMQUtils.java    From WeBASE-Front with Apache License 2.0 3 votes vote down vote up
/**
 * new exchange and bind queue by rabbitAdmin
 * @param rabbitAdmin
 * @param exchangeName
 * @param queueName
 * @param routingKey
 */
public static void declareExchangeAndQueue(RabbitAdmin rabbitAdmin,
                                            String exchangeName, String queueName, String routingKey){
    DirectExchange directExchange = new DirectExchange(exchangeName);
    rabbitAdmin.declareExchange(directExchange);
    rabbitAdmin.declareQueue(new Queue(queueName));
}
 
Example 14
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);
}