I need some help.
I'm developing a spring boot application, and I want wo publish messages to a rabbitMQ. I want to send it to a queue, that is named in the message itself. This way i want to create queues dynamicly.
I only found examples that use a "static" queue.
I have reserched some things but didn't find anything.
I'm new to RabbitMQ and learned the basic concepts.
I'm also fairly new to spring.
RabbotMQ Config
#Configuration
public class RabbitMQConfig {
#Value("amq.direct")
String exchange;
#Value("queue-name") // Don't want to do this
String queueName;
#Value("routing-key") // Or this
String routingkey;
#Bean
Queue queue() {
return new Queue(queueName, true);
}
#Bean
DirectExchange exchange() {
return new DirectExchange(exchange);
}
#Bean
Binding binding(Queue queue, DirectExchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with(routingkey);
}
#Bean
public MessageConverter jsonMessageConverter() {
return new Jackson2JsonMessageConverter();
}
#Bean
public AmqpTemplate template(ConnectionFactory connectionFactory) {
final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
rabbitTemplate.setMessageConverter(jsonMessageConverter());
return rabbitTemplate;
}
}
MessageSender
#Service
public class RabbitMQSender {
#Autowired
private AmqpTemplate template;
#Value("amq.direct")
private String exchange;
public void send(MessageDTO message) {
template.convertAndSend(exchange, message);
}
}
I came to a solution:
You need to create a AmqpAdmin in your config:
#Bean
public AmqpAdmin amqpAdmin() {
return new RabbitAdmin(connectionFactory);
}
Then you add it to your service:
#Autowired
private AmqpAdmin admin;
Finally you can use it to create queues and bindings.
Queue queue = new Queue(queueName, durable, false, false);
Binding binding = new Binding(queueName, Binding.DestinationType.QUEUE, EXCHANGE, routingKey, null);
admin.declareQueue(queue);
admin.declareBinding(binding);
I found the solution here
Not sure which version of RabbitMQ you were on but, your original code was close. This works, too.
#Bean
Queue fanoutQueue() {
// empty name, durable false, exclusive false, autoDelete false
return new Queue("", false, false, true);
}
#Bean
FanoutExchange fanoutExchange() {
return new FanoutExchange("fanout-exchange", true, false);
}
#Bean
Binding fanoutBinding(Queue fanoutQueue, FanoutExchange fanoutExchange) {
return BindingBuilder.bind(fanoutQueue).to(fanoutExchange);
}
Related
My Application currently uses IBM MQ and has queue config setup and working fine with JMS. e.g.
#EnableJms
#Configuration
public class IBMQueueConfig {
#Bean("defaultContainer")
public JmsListenerContainerFactory containerFactory(final ConnectionFactory connectionFactory,
final ErrorHandler errorHandler) {
final DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
factory.setErrorHandler(errorHandler);
return factory;
}
}
I can receive message and process as follows:
#Service
public class ProcessMessageReceive {
#JmsListener(destination = "${queue}", concurrency = "${threads}", containerFactory = "defaultContainer")
public Message processMessage(#Payload final String message) {
//do stuff
}
}
I need to use RabbitMQ for testing and require additional configuration. I have the following the class:
#Configuration
#ConfigurationProperties(prefix = "spring.rabbitmq")
#EnableRabbit
public class RabbitMQConfiguration {
private String host;
private int port;
private String username;
private String password;
private String virtualHost;
#Bean
public DirectExchange exchange() {
return new DirectExchange(exchange);
}
#Bean("defaultContainer")
public JmsListenerContainerFactory containerFactory(#Qualifier("rabbit-connection-factory") final ConnectionFactory connectionFactory) {
final DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setConnectionFactory(); //ERROR
return factory;
}
#Bean
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(#Qualifier("rabbit-connection-factory") final ConnectionFactory connectionFactory,
#Value("spring.rabbitmq.listener.simple.concurrency") final int concurrency,
#Value("spring.rabbitmq.listener.simple.max-concurrency") final int maxConcurrency) {
final SimpleRabbitListenerContainerFactory containerFactory = new SimpleRabbitListenerContainerFactory();
containerFactory.setConnectionFactory(connectionFactory);
containerFactory.setConcurrentConsumers(concurrency);
containerFactory.setMaxConcurrentConsumers(maxConcurrency);
containerFactory.setDefaultRequeueRejected(false);
return containerFactory;
}
#Bean(name = "rabbit-connection-factory")
public ConnectionFactory connectionFactory() {
final CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
connectionFactory.setHost(host);
connectionFactory.setPort(port);
connectionFactory.setUsername(username);
connectionFactory.setPassword(password);
connectionFactory.setVirtualHost(virtualHost);
return connectionFactory;
}
#Bean
public Queue inboundQueue() {
return new Queue(fixInboundQueue, true);
}
#Bean
public Binding inboundQueueBinding() {
return bind(inboundQueue())
.to(exchange())
.with(routingKey);
}
}
I get an error on line factory.setConnectionFactory(connectionFactory); as it expects a javax.jms.ConnectionFactory but provided is Rabbit MQ One.
Is there a way I can wire in the Rabbit MQ ConnectionFactory ? I know it is possible if I use RMQConnectionFactory, but I am looking to see If I can achieve it with Spring Rabbit dependency.
The objective is to avoid writing another processMessage() specifically for the Rabbit MQ and re-use what I already have.
Alternatively, can I use both annotations? In which case I would use spring profile to enable the one I need depending on prod or test?
#RabbitListener(queues = "${app.rabbitmq.queue}")
#JmsListener(destination = "${queue}", concurrency = "${threads}", containerFactory = "defaultContainer")
public Message processMessage(#Payload final String message) {
//do stuff
}
You have to use #RabbitListener instead of #JmsListener if you want to talk to RabbitMQ over AMQP.
You can add both annotations if you want to use JMS in production and RabbitMQ in tests.
i'm implementing a project where i have to send messages across different vhosts in rabbitmq. using SimpleRoutingConnectionFactory but get java.lang.IllegalStateException: Cannot determine target ConnectionFactory for lookup key [null].
Anyone who has an idea how to implement such below is my configuration class code.
#Configuration
#EnableRabbit
public class RabbitMQConfiguration {
#Autowired
ConnectionProperties connect;
// client1 exchanges
#Bean
public TopicExchange client1Exchange() {
TopicExchange ex = new TopicExchange("ex_client1");
ex.setAdminsThatShouldDeclare(client1());
return ex;
}
// client2 exchange
#Bean
public TopicExchange client2Exchange() {
TopicExchange ex = new TopicExchange("ex_client2");
ex.setAdminsThatShouldDeclare(client2Admin());
return ex;
}
#Bean
public Queue client1Queue() {
Queue queue = new Queue("client1_queue");
queue.setAdminsThatShouldDeclare(client1());
return queue;
}
#Bean
public Binding client1Binding() {
Binding binding = BindingBuilder.bind(client1Queue())
.to(client1Exchange())
.with("client1_key");
binding.setAdminsThatShouldDeclare(client1());
return binding;
}
#Bean
public Queue client2Queue() {
Queue queue = new Queue("client2_queue");
queue.setAdminsThatShouldDeclare(client2());
return queue;
}
#Bean
public Binding client2Binding() {
Binding binding = BindingBuilder.bind(client2Queue())
.to(client2Exchange())
.with("client2_key");
binding.setAdminsThatShouldDeclare(client2());
return binding;
}
#Bean
#Primary
public ConnectionFactory connectionFactory() {
SimpleRoutingConnectionFactory connectionFactory = new SimpleRoutingConnectionFactory();
Map<Object, ConnectionFactory> targetConnectionFactories = new HashMap<>();
targetConnectionFactories.put("client1", client1ConnectionFactory());
targetConnectionFactories.put("client2", client2ConnectionFactory());
connectionFactory.setTargetConnectionFactories(targetConnectionFactories);
return connectionFactory;
}
#Bean
public ConnectionFactory client1ConnectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory(connect.getRabbitMQHost());
connectionFactory.setVirtualHost(connect.getRabbitMQClient1VHost());
connectionFactory.setUsername(connect.getRabbitMQClient1User());
connectionFactory.setPassword(connect.getRabbitMQClient1Pass());
return connectionFactory;
}
#Bean
public ConnectionFactory client2ConnectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory(connect.getRabbitMQHost());
connectionFactory.setVirtualHost(connect.getRabbitMQClient2VHost());
connectionFactory.setUsername(connect.getRabbitClient2User());
connectionFactory.setPassword(connect.getRabbitClient2Pass());
return connectionFactory;
}
// You can comment all methods below and remove interface's implementation to use the default serialization / deserialization
#Bean
public RabbitTemplate rabbitTemplate() {
final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory());
rabbitTemplate.setMessageConverter(producerJackson2MessageConverter());
return rabbitTemplate;
}
#Bean
public Jackson2JsonMessageConverter producerJackson2MessageConverter() {
return new Jackson2JsonMessageConverter();
}
#Bean
public MappingJackson2MessageConverter consumerJackson2MessageConverter() {
return new MappingJackson2MessageConverter();
}
#Bean
public DefaultMessageHandlerMethodFactory messageHandlerMethodFactory() {
DefaultMessageHandlerMethodFactory factory = new DefaultMessageHandlerMethodFactory();
factory.setMessageConverter(consumerJackson2MessageConverter());
return factory;
}
#Bean
public TaskExecutor rabbitListenerExecutor() {
int threads = Integer.valueOf(connect.getMinConsumers()) * 2; // threads = min consumers* no of queues
final ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(threads);
executor.setMaxPoolSize(threads);
executor.setThreadNamePrefix("RabbitThreadListener");
executor.afterPropertiesSet();
return executor;
}
#Bean
public SimpleRabbitListenerContainerFactory myRabbitListenerContainerFactory() {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
factory.setConnectionFactory(connectionFactory());
factory.setConcurrentConsumers(Integer.valueOf(connect.getMinConsumers()));
factory.setPrefetchCount(Integer.valueOf(connect.getPrefetchCount()));
factory.setTaskExecutor(rabbitListenerExecutor());
factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
return factory;
}
#Bean
public RabbitAdmin client1() {
RabbitAdmin rabbitAdmin = new RabbitAdmin(client1ConnectionFactory());
rabbitAdmin.afterPropertiesSet();
return rabbitAdmin;
}
#Bean
public RabbitAdmin client2() {
RabbitAdmin rabbitAdmin = new RabbitAdmin(client2ConnectionFactory());
rabbitAdmin.afterPropertiesSet();
return rabbitAdmin;
}
}
i'm getting this stacktrace
o.s.a.r.l.SimpleMessageListenerContainer - Consumer raised exception,
processing can restart if the connection factory supports it
java.lang.IllegalStateException: Cannot determine target ConnectionFactory for lookup key [null]
at org.springframework.amqp.rabbit.connection.AbstractRoutingConnectionFactory.determineTargetConnectionFactory(AbstractRoutingConnectionFactory.java:119)
at org.springframework.amqp.rabbit.connection.AbstractRoutingConnectionFactory.createConnection(AbstractRoutingConnectionFactory.java:97)
at org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils$1.createConnection(ConnectionFactoryUtils.java:90)
at org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils.doGetTransactionalResourceHolder(ConnectionFactoryUtils.java:140)
at org.springframework.amqp.rabbit.connection.ConnectionFactoryUtils.getTransactionalResourceHolder(ConnectionFactoryUtils.java:76)
at org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.start(BlockingQueueConsumer.java:505)
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1335)
at java.lang.Thread.run(Thread.java:748)
The RoutingConnectionFactory is generally used for publishing messages.
When using a routing factory in a listener container you must configure the lookup key to match the queue name(s) configured in the container.
From the documentation:
Also starting with version 1.4, you can configure a routing connection factory in a listener container. In that case, the list of queue names is used as the lookup key. For example, if you configure the container with setQueueNames("foo", "bar"), the lookup key will be "[foo,bar]" (no spaces).
So; if a RabbitListener listens to queue foo the routing lookup key must be [foo]. (You can add the same CF multiple times with different keys).
Or you can simply create multiple container factories, with each getting a concrete CF instead of the routing CF.
EDIT
Let's say you have
#RabbitListener(queues = "myQueue", connectionFactory = "myRabbitListenerContainerFactory")
public void listen(...) {
...
}
If myQueue is in client1's vhost, then you need an entry in the router CF map thus...
targetConnectionFactories.put("[myQueue]", client1ConnectionFactory());
...because the listener container generated for the listener will use the queue name in its lookup key.
Alternatively, create 2 container factories; each wired directly with client1 and client2 CFs instead of the routing CF...
#Bean
public SimpleRabbitListenerContainerFactory client1ListenerContainerFactory() {
#Bean
public SimpleRabbitListenerContainerFactory client2ListenerContainerFactory() {
and
#RabbitListener(queues = "myQueue", connectionFactory = "client1ListenerContainerFactory")
public void listen(...) {
...
}
i.e. don't use the routing CF at all for listeners - containers only have one connection.
I have implemented a basic asynchronous RPC call using spring boot 1.4 and rabbit mq.
My intention is to use this example as a basis of communication
among micro services.For example, Publisher.java and Subscriber.java could be two micro services talking to each other.
The code shown works fine, but I am curious to know if there are any better ways
of doing this?
My queries as follows:
For subscriber to listen to request queue using #RabbitListener annotation , I did not had to declare directExchange() and binding() beans in configuration. But for asyncRabbitTemplate to read response from reply queue, I had to declare directExchange() and binding() beans in configuration.
Is there any way I can avoid it, because I feel it is code duplication as I am declaring these beans twice.
In real world application, there would be many such calls between micro services.And as per my understanding , I would need to declare similar rpcReplyMessageListenerContainer() and asyncRabbitTemplate() for each request-reply call.Is that correct?
Code as follows.
Link to Github
Config.java
#Configuration("asyncRPCConfig")
#Profile("async_rpc")
#EnableScheduling
#EnableRabbit
#ComponentScan(basePackages = {"in.rabbitmq.async_rpc"})
public class Config {
#Value("${queue.reply}")
private String replyQueue;
#Value("${exchange.direct}")
private String directExchange;
#Value("${routingKey.reply}")
private String replyRoutingKey;
#Bean
public Publisher publisher() {
return new Publisher();
}
#Bean
public SimpleRabbitListenerContainerFactory simpleMessageListenerContainerFactory(ConnectionFactory connectionFactory,
SimpleRabbitListenerContainerFactoryConfigurer configurer) {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
configurer.configure(factory, connectionFactory);
return factory;
}
#Bean
public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
RabbitTemplate template = new RabbitTemplate(connectionFactory);
template.setMessageConverter(jsonMessageConverter());
return template;
}
#Bean
public MessageConverter jsonMessageConverter() {
return new Jackson2JsonMessageConverter();
}
#Bean
public Queue replyQueueRPC() {
return new Queue(replyQueue);
}
#Bean
public SimpleMessageListenerContainer rpcReplyMessageListenerContainer(ConnectionFactory connectionFactory) {
SimpleMessageListenerContainer simpleMessageListenerContainer = new SimpleMessageListenerContainer(connectionFactory);
simpleMessageListenerContainer.setQueues(replyQueueRPC());
simpleMessageListenerContainer.setReceiveTimeout(2000);
simpleMessageListenerContainer.setTaskExecutor(Executors.newCachedThreadPool());
return simpleMessageListenerContainer;
}
#Bean
public AsyncRabbitTemplate asyncRabbitTemplate(ConnectionFactory connectionFactory) {
return new AsyncRabbitTemplate(rabbitTemplate(connectionFactory),
rpcReplyMessageListenerContainer(connectionFactory),
directExchange + "/" + replyRoutingKey);
}
#Bean
public DirectExchange directExchange() {
return new DirectExchange(directExchange);
}
#Bean
public Binding binding() {
return BindingBuilder.bind(replyQueueRPC()).to(directExchange()).with(replyRoutingKey);
}
#Bean
public Subscriber subscriber() {
return new Subscriber();
}
}
Publisher.java
public class Publisher {
#Value("${routingKey.request}")
private String requestRoutingKey;
#Autowired
private DirectExchange directExchange;
private static SecureRandom SECURE_RANDOM;
static {
try {
SECURE_RANDOM = SecureRandom.getInstanceStrong();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
#Autowired
private AsyncRabbitTemplate asyncRabbitTemplate;
#Scheduled(fixedDelay = 100 * 1)
public void publishToDirectExchangeRPCStyle() {
Integer integer = SECURE_RANDOM.nextInt();
SampleRequestMessage sampleRequestMessage = new SampleRequestMessage(String.valueOf(integer));
System.out.println("Sending out message on direct directExchange:" + sampleRequestMessage);
AsyncRabbitTemplate.RabbitConverterFuture<SampleResponseMessage> sampleResponseMessageRabbitConverterFuture = asyncRabbitTemplate
.convertSendAndReceive(directExchange.getName(), requestRoutingKey, sampleRequestMessage);
sampleResponseMessageRabbitConverterFuture.addCallback(
sampleResponseMessage ->
System.out.println("Response for request message:" + sampleRequestMessage + " is:" + sampleResponseMessage)
, failure ->
System.out.println(failure.getMessage())
);
}
}
Subscriber.java
public class Subscriber {
#RabbitHandler
#RabbitListener(
bindings = {
#QueueBinding(value = #Queue("${queue.request}"),
key = "${routingKey.request}",
exchange = #Exchange(value = "${exchange.direct}", type = ExchangeTypes.DIRECT, durable = "true"))})
public SampleResponseMessage subscribeToRequestQueue(#Payload SampleRequestMessage sampleRequestMessage, Message message) {
System.out.println("Received message :" + message);
return new SampleResponseMessage(sampleRequestMessage.getMessage());
}
}
Your solution is fine.
It is not clear what you are asking...
I had to declare directExchange() and binding() beans in configuration.
Is there any way I can avoid it, because I feel it is code duplication as I am declaring these beans twice.
#QueueBinding is simply a convenience on #RabbitListener and an alternative to declaring the queue, exchange and binding as #Beans.
If you are using a common #Config class you can simply omit the bindings attribute on the listener and use queues = "${queue.reply}" to avoid the duplication.
I would need to declare similar rpcReplyMessageListenerContainer() and asyncRabbitTemplate() for each request-reply call.
Is that correct?
Yes; although with the upcoming 2.0 release, you can use a DirectReplyToMessageListenerContainer which avoids the need for a separate reply queue for each service; when you send a message.
See the documentation here and here.
Starting with version 2.0, the async template now supports Direct reply-to instead of a configured reply queue.
(Should read "as an alternative to " rather than "instead of").
So you can use the same template to talk to multiple services.
I've the following two Configuration classes:
#Configuration
#EnableRabbit
#Import({ LocalRabbitConfigA.class, CloudRabbitConfigA.class })
public class RabbitConfigA {
#Autowired
#Qualifier("rabbitConnectionFactory_A")
private ConnectionFactory rabbitConnectionFactory;
#Bean(name = "admin_A")
AmqpAdmin amqpAdmin() {
return new RabbitAdmin(rabbitConnectionFactory);
}
#Bean(name = "Exchange_A")
DirectExchange receiverExchange() {
return new DirectExchange("Exchange_A", true, false);
}
}
And
#Configuration
#EnableRabbit
#Import({ LocalRabbitConfigB.class, CloudRabbitConfigB.class })
public class RabbitConfigB {
#Autowired
#Qualifier("rabbitConnectionFactory_B")
private ConnectionFactory rabbitConnectionFactory;
#Bean(name = "admin_B")
AmqpAdmin amqpAdmin() {
return new RabbitAdmin(rabbitConnectionFactory);
}
#Bean(name = "Exchange_B")
DirectExchange receiverExchange() {
return new DirectExchange("Exchange_B", true, false);
}
}
Note that the LocalRabbitConfigA and LocalRabbitConfigB classes define the connectionFactory which connects to a different VHost.
When starting the application (within Tomcat), all the Exchanges are created in both VHosts.
The question is how to define that a certain Exchange/Queue is created by a specific ConnectionFactiory ?
So that VHost A contains only the Exchange_A, and VHost B only Exchange_B ?
See conditional declaration.
Specifically:
#Bean(name = "Exchange_B")
DirectExchange receiverExchange() {
DirectExchange exchange = new DirectExchange("Exchange_B", true, false);
exchange.setAdminsThatShouldDeclare(amqpAdmin());
return exchange;
}
We can achieve this using SimpleRoutingConnectionFactory, where we create multiple connection factories each for a vhost and configure it to SimpleRoutingConnectionFactory.
From the spring documentation: spring doc
public class MyService {
#Autowired
private RabbitTemplate rabbitTemplate;
public void service(String vHost, String payload) {
SimpleResourceHolder.bind(rabbitTemplate.getConnectionFactory(), vHost);
rabbitTemplate.convertAndSend(payload);
SimpleResourceHolder.unbind(rabbitTemplate.getConnectionFactory());
}
}
I have created a git repo showing how to do this: spring-boot-amqp-multiple-vhosts
I'm trying to create a RabbitMQ configuration class using Spring Framework. The documentation does not say anything on how to setup multiple topics in a TopicExchange. How do I do that? So far, I have this Java code but I'm not clear on how to setup multiple topics in the binding method below since it only returns one binding. Would I not need multiple bindings if I need multiple topics?
#Configuration
#EnableRabbit
public class MessageReceiverConfiguration {
final static String queueName = "identity";
final static String topic1 = "NewUserSignedUp";
final static String topic2 = "AccountCreated";
#Autowired
RabbitTemplate rabbitTemplate;
#Bean
Queue queue() {
return new Queue(queueName, false);
}
#Bean
TopicExchange exchange() {
return new TopicExchange("DomainEvents");
}
#Bean
Binding binding(Queue queue, TopicExchange exchange) {
// How to setup multiple topics?
return BindingBuilder.bind(queue).to(exchange).with(topic1);
}
#Bean
SimpleMessageListenerContainer container(ConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.setQueueNames(queueName);
container.setMessageListener(listenerAdapter);
container.setAcknowledgeMode(AcknowledgeMode.AUTO);
return container;
}
#Bean
MessageReceiver receiver() {
return new MessageReceiver();
}
#Bean
MessageListenerAdapter listenerAdapter(MessageReceiver receiver) {
return new MessageListenerAdapter(receiver, "receiveMessage");
}
}
You can define multiple binding by changing Binding function to return a list instead of a single Binding object.
#Bean
List<Binding> bindings() {
return Arrays.AsList(BindingBuilder.bind(queue()).to(exchange()).with(topic1),
BindingBuilder.bind(queue()).to(exchange()).with(topic2));
}
Tip: You do not need to pass queue and exchange as method params. You can directly refer the bean methods to pass the information of exchange and queue.
Refer documentation for more details.
List<Binding> bindings() not supported by spring boot 2.+ versions.
This one works;
#Bean
public Declarables bindings() {
return new Declarables(
BindingBuilder
.bind(bookingAddQueue())
.to(bookingExchange())
.with("add")
.noargs(),
BindingBuilder
.bind(bookingEditQueue())
.to(bookingExchange())
.with("edit")
.noargs());
}