I'm playing with spring and activemq and use the following method to receive messages from message broker:
@JmsListener(destination = "sample.queue")
public void receiveQueue(String text) {
System.out.println(text);
}
I just thought it would be nice to be able to configure destination
from my application.properties
. Is there any way to do that ?
Ok, I found the way. Suppose message-consumer.destination
property from application.properties
defines the desired destination, then it would be as simple as this:
@JmsListener(destination = "${message-consumer.destination}")
public void receiveQueue(String text) {
System.out.println(text);
}
Below are my old thoughts on how to externalize queue destination:
Here is the message consumer.
@Component
public class Consumer implements MessageListener {
@Override
public void onMessage(Message message) {
}
}
Here is the jms configuration:
@Configuration
@EnableJms
public class JmsConfiguration implements JmsListenerConfigurer {
@Value("${message-consumer.destination}")
private String destination;
@Inject
private MessageListener messageListener;
@Override
public void configureJmsListeners(JmsListenerEndpointRegistrar registrar) {
SimpleJmsListenerEndpoint endpoint = new SimpleJmsListenerEndpoint();
endpoint.setId("audit.logging");
endpoint.setDestination(destination);
endpoint.setMessageListener(messageListener);
registrar.registerEndpoint(endpoint);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With