2015-10-06 89 views
1

我可以使用@Configuration創建Exchanges,隊列/綁定& @Bean乾淨地(如下所示),但我沒有找到類似方式創建VirtualHost的方法。我只想爲這個虛擬主機使用默認的'guest'用戶。有沒有辦法?我有1個製作人和3個聽衆在不同的應用程序中運行。我認爲用這種配置有一個課程很容易,並將其複製到所有這些應用程序。我認爲這是一個非常普遍的要求。在這種情況下創建所需配置的最佳方式是什麼?使用@Configuration創建VirtualHost類

@Configuration 
public class amqpConfiguration { 
@Autowired 
RabbitTemplate rabbitTemplate; 

@Bean 
TopicExchange testExchange() { 
    return new TopicExchange("test.exchange"); 
} 

TopicExchange errorExchange() { 
    return new TopicExchange("error.change"); 
} 

@Bean 
Queue erorQueue() { 
    return new Queue("error.q", true); 
} 

@Bean 
Binding errorQueueBinding(Queue erorQueue, TopicExchange errorExchange) { 
    return BindingBuilder.bind(erorQueue).to(errorExchange).with("error.q"); 
} 

@Bean 
Queue testQueue() { 
    Map<String, Object> args = new HashMap<String, Object>(); 
    args.put("x-dead-letter-exchange", "test.exchange"); 
    args.put("x-dead-letter-routing-key", "error.q"); 
    return new Queue("test.q", true, false, false, args); 
} 

@Bean 
Binding inQueueBinding(Queue testQueue, TopicExchange testExchange) { 
    return BindingBuilder.bind(testQueue).to(testExchange).with("test.q"); 
} 

}

+0

找到另一個[post](http://rabbitmq.1065348.n5.nabble.com/Configure-Queue-under-specific-vhost-in-Spring-Project-td35374.html)。這表明我可以在connectionfactory中指定vhost,但看起來並不像我可以創建一個新的虛擬主機。 – user3741611

回答

1

不,你能做到這一點只能從經紀人的角度,或使用Management Plugin HTTP API:

/API /虛擬主機/名稱 - 單獨的虛擬主機。由於虛擬主機通常只有一個名稱,因此在進行其中一種操作時不需要HTTP主體。

好,從大的高度,是的,你可以這樣做:使用Spring RestTemplate來執行REST API來創建所需vhost