Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define field value of an annotation from property file

Tags:

java

spring

jms

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 ?

like image 576
eparvan Avatar asked Mar 11 '23 19:03

eparvan


1 Answers

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);
    }
like image 151
eparvan Avatar answered Mar 21 '23 18:03

eparvan