Java Code Examples for org.springframework.amqp.rabbit.core.RabbitAdmin

The following examples show how to use org.springframework.amqp.rabbit.core.RabbitAdmin. These examples are extracted from open source projects. 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 Project: generic-vnfm   Source File: EMSConfiguration.java    License: 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 2
Source Project: zxl   Source File: MQClient.java    License: 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
@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 4
Source Project: rabbitmq-mock   Source File: SpringIntegrationTest.java    License: 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 Project: kkbinlog   Source File: RabbitMQClient.java    License: Apache License 2.0 5 votes vote down vote up
public synchronized static RabbitMQClient getInstance(ConnectionFactory connectionFactory) throws Exception {
    if (instance == null && connectionFactory != null) {
        instance = new RabbitMQClient();
        instance.connectionFactory = connectionFactory;
        instance.amqpAdmin = new RabbitAdmin(connectionFactory);
        instance.amqpTemplate = new RabbitTemplate(connectionFactory);
        instance.setDataExchange();
        instance.setNotifyExchange();
    }
    return instance;
}
 
Example 6
@Bean
public RabbitAdmin rabbitAdmin() {
	RabbitAdmin admin = new RabbitAdmin(connectionFactory());
	admin.declareQueue(queue());
	admin.declareExchange(exchange());
	admin.declareBinding(exchangeBinding(exchange(), queue()));
	return admin;
}
 
Example 7
Source Project: shine-mq   Source File: RabbitmqFactory.java    License: Apache License 2.0 5 votes vote down vote up
private RabbitmqFactory(MqProperties config) {
    Objects.requireNonNull(config, "The RabbitmqProperties is empty.");
    this.config = config;
    this.rabbit = config.getRabbit();
    rabbitAdmin = new RabbitAdmin(rabbitConnectionFactory);
    rabbitTemplate = new RabbitTemplate(rabbitConnectionFactory);
    rabbitTemplate.setMessageConverter(serializerMessageConverter);
    if (config.getDistributed().isTransaction()) {
        setRabbitTemplateForDis(config);
    }
    template = new RabbitmqTemplate(this, rabbitTemplate, serializerMessageConverter);
}
 
Example 8
@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 9
@After
public void tearDown() {
	if (context != null) {
		context.close();
		context = null;
	}
	RabbitAdmin admin = new RabbitAdmin(rabbitTestSupport.getResource());
	admin.deleteExchange("input");
	admin.deleteExchange("output");
}
 
Example 10
@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:[email protected]:15672/api/");
	List<?> bindings = client.getBindingsBySource("/", exchange.getName());
	assertThat(bindings.size()).isEqualTo(1);
}
 
Example 11
@Test
public void testNonDurablePubSubWithAutoBindDLQ() throws Exception {
	RabbitAdmin admin = new RabbitAdmin(this.rabbitAvailableRule.getResource());

	RabbitTestBinder binder = getBinder();
	ExtendedConsumerProperties<RabbitConsumerProperties> consumerProperties = createConsumerProperties();
	consumerProperties.getExtension().setPrefix(TEST_PREFIX);
	consumerProperties.getExtension().setAutoBindDlq(true);
	consumerProperties.getExtension().setDurableSubscription(false);
	consumerProperties.setMaxAttempts(1); // disable retry
	BindingProperties bindingProperties = createConsumerBindingProperties(
			consumerProperties);
	DirectChannel moduleInputChannel = createBindableChannel("input",
			bindingProperties);
	moduleInputChannel.setBeanName("nondurabletest");
	moduleInputChannel.subscribe(new MessageHandler() {

		@Override
		public void handleMessage(Message<?> message) throws MessagingException {
			throw new RuntimeException("foo");
		}

	});
	Binding<MessageChannel> consumerBinding = binder.bindConsumer("nondurabletest.0",
			"tgroup", moduleInputChannel, consumerProperties);

	consumerBinding.unbind();
	assertThat(admin.getQueueProperties(TEST_PREFIX + "nondurabletest.0.dlq"))
			.isNull();
}
 
Example 12
@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 13
public RabbitTestBinder(ConnectionFactory connectionFactory,
		RabbitMessageChannelBinder binder) {
	this.applicationContext = new AnnotationConfigApplicationContext(Config.class);
	binder.setApplicationContext(this.applicationContext);
	this.setPollableConsumerBinder(binder);
	this.rabbitAdmin = new RabbitAdmin(connectionFactory);
}
 
Example 14
public RabbitExchangeQueueProvisioner(ConnectionFactory connectionFactory,
		List<DeclarableCustomizer> customizers) {

	this.rabbitAdmin = new RabbitAdmin(connectionFactory);
	this.autoDeclareContext.refresh();
	this.rabbitAdmin.setApplicationContext(this.autoDeclareContext);
	this.rabbitAdmin.afterPropertiesSet();
	this.customizers = customizers;
}
 
Example 15
Source Project: haven-platform   Source File: AmqpUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * decalre specified excahnge
 * @param connectionFactory
 * @param exchangeType
 * @param exchangeName
 * @param durable
 * @param autoDelete
 */
public static void declareExchange(ConnectionFactory connectionFactory,
                                   String exchangeType,
                                   String exchangeName,
                                   boolean durable,
                                   boolean autoDelete) {
    Exchange x = createExchange(exchangeType, exchangeName, durable, autoDelete);
    RabbitAdmin admin = new RabbitAdmin(connectionFactory);
    admin.declareExchange(x);
}
 
Example 16
Source Project: haven-platform   Source File: AmqpReporter.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public RabbitTemplate call() throws Exception {
    ConnectionFactory connectionFactory = connectionFactoryProvider.getObject();
    Assert.notNull(connectionFactory, "connectionFactory is null");

    if(exchangeFactory != null) {
        Exchange exchange = exchangeFactory.call();
        if(exchange != null) {
            RabbitAdmin admin = new RabbitAdmin(connectionFactory);
            admin.declareExchange(exchange);
        }
    }

    return new RabbitTemplate(connectionFactory);
}
 
Example 17
protected RabbitAdmin resolveAdmin(RabbitInboundChannelModel channelDefinition) {
    String rabbitAdmin = resolve(channelDefinition.getAdmin());
    if (StringUtils.hasText(rabbitAdmin)) {
        Assert.state(this.beanFactory != null, "BeanFactory must be set to resolve RabbitAdmin by bean name");
        try {
            return this.beanFactory.getBean(rabbitAdmin, RabbitAdmin.class);
        } catch (NoSuchBeanDefinitionException ex) {
            throw new IllegalArgumentException("Could not register rabbit listener endpoint on [" +
                channelDefinition + "], no " + RabbitAdmin.class.getSimpleName() + " with id '" +
                rabbitAdmin + "' was found in the application context", ex);
        }
    } else {
        return null;
    }
}
 
Example 18
Source Project: zxl   Source File: MQServer.java    License: 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 19
Source Project: zxl   Source File: MqBeanParser.java    License: Apache License 2.0 5 votes vote down vote up
protected void buildRabbitAdminBeanDefinition(String beanName, String connectionFactoryBeanName, BeanDefinitionRegistry beanDefinitionRegistry) {
	AbstractBeanDefinition rabbitAdminBeanDefinition = new GenericBeanDefinition();
	rabbitAdminBeanDefinition.setBeanClass(RabbitAdmin.class);
	ConstructorArgumentValues rabbitAdminConstructorArgumentValues = new ConstructorArgumentValues();
	rabbitAdminConstructorArgumentValues.addIndexedArgumentValue(0, new RuntimeBeanReference(connectionFactoryBeanName));
	rabbitAdminBeanDefinition.setConstructorArgumentValues(rabbitAdminConstructorArgumentValues);
	beanDefinitionRegistry.registerBeanDefinition(beanName, rabbitAdminBeanDefinition);
}
 
Example 20
Source Project: sinavi-jfw   Source File: ExceptionQueueContextConfig.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * RabbitMQの管理操作を実行する{@link AmqpAdmin}のインスタンスを生成し、DIコンテナに登録します。
 * この{@link AmqpAdmin}を利用することにより、Exchange/Queueの自動生成を行うことが可能となります。
 * 自動生成する場合はSpring のBeanProfileのスコープ指定を<strong>development</strong>に指定してください。
 * @return {@link RabbitAdmin}のインスタンス
 */
@Bean
@Profile("development")
public AmqpAdmin amqpAdmin() {
    RabbitAdmin rabbitAdmin = new RabbitAdmin(factory());
    rabbitAdmin.setAutoStartup(true);
    return rabbitAdmin;
}
 
Example 21
Source Project: sinavi-jfw   Source File: TestQueueCleaner.java    License: Apache License 2.0 5 votes vote down vote up
public static void cleaner() {
    RabbitAdmin admin = new RabbitAdmin(factory());
    admin.setAutoStartup(true);
    admin.deleteExchange("error.exchange");
    admin.deleteExchange("exception.exchange");
    admin.deleteQueue("recoverable.exception.messages.queue");
    admin.deleteQueue("unrecoverable.exception.messages.queue");
}
 
Example 22
Source Project: brave   Source File: ITSpringRabbit.java    License: 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 23
Source Project: codeway_service   Source File: RabbitUtil.java    License: GNU General Public License v3.0 4 votes vote down vote up
public RabbitUtil(RabbitAdmin rabbitAdmin, RabbitTemplate rabbitTemplate){
	this.rabbitAdmin = rabbitAdmin;
	this.rabbitTemplate = rabbitTemplate;
}
 
Example 24
Source Project: WeBASE-Front   Source File: RabbitMQConfig.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * 新建yml中rabbitmq-queue默认的队列
 * @param connectionFactory spring的yml中rabbitmq项配置
 * @return
 */
@Bean(name = "rabbitAdmin")
public RabbitAdmin initRabbitAdmin(ConnectionFactory connectionFactory) {
    RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
    return rabbitAdmin;
}
 
Example 25
Source Project: WeBASE-Front   Source File: RabbitMQUtils.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * new exchange by rabbitAdmin
 * @param rabbitAdmin
 */
public static void declareExchange(RabbitAdmin rabbitAdmin, String exchangeName){
    DirectExchange directExchange = new DirectExchange(exchangeName);
    rabbitAdmin.declareExchange(directExchange);
}
 
Example 26
@Bean
public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
	return new RabbitAdmin(connectionFactory());
}
 
Example 27
@Bean
public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
    return new RabbitAdmin(connectionFactory());
}
 
Example 28
Source Project: codeway_service   Source File: RabbitUtil.java    License: GNU General Public License v3.0 4 votes vote down vote up
public RabbitUtil(RabbitAdmin rabbitAdmin, RabbitTemplate rabbitTemplate){
	this.rabbitAdmin = rabbitAdmin;
	this.rabbitTemplate = rabbitTemplate;
}
 
Example 29
Source Project: rabbitmq-mock   Source File: SpringIntegrationTest.java    License: Apache License 2.0 4 votes vote down vote up
@Bean
RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
    return new RabbitAdmin(connectionFactory);
}
 
Example 30
Source Project: java-specialagent   Source File: SpringRabbitMQTest.java    License: Apache License 2.0 4 votes vote down vote up
@Bean
public AmqpAdmin amqpAdmin() {
  return new RabbitAdmin(connectionFactory());
}