dynamically set @JmsListener destination from configuration properties
Asked Answered
B

2

10

I want to be able to set the @JMSlistener destination from an application.properties

my code looks like this

@Service
public class ListenerService {
    private Logger log = Logger.getLogger(ListenerService.class);

    @Autowired
    QueueProperties queueProperties;


    public ListenerService(QueueProperties queueProperties) {
        this.queueProperties = queueProperties;

    }

    @JmsListener(destination = queueProperties.getQueueName() )
    public void listenQueue(String requestJSON) throws JMSException {
        log.info("Received " + requestJSON);

    }
}

but when building I get

Error:(25, 60) java: element value must be a constant expression
Bumper answered 19/3, 2018 at 17:1 Comment(3)
Can u try #listenerService.queueProperties.getQueueName() in destinationPyo
I get this Error:(25, 47) java: non-static variable queueProperties cannot be referenced from a static contextBumper
Did u try with braces so “#{}” in braces specify what I mentioned above.Pyo
L
17

You can't reference a field within the current bean, but you can reference another bean in the application context using a SpEL expression...

@SpringBootApplication
public class So49368515Application {

    public static void main(String[] args) {
        SpringApplication.run(So49368515Application.class, args);
    }

    @Bean
    public ApplicationRunner runner(JmsTemplate template, Foo foo) {
        return args -> template.convertAndSend(foo.getDestination(), "test");
    }

    @JmsListener(destination = "#{@foo.destination}")
    public void listen(Message in) {
        System.out.println(in);
    }

    @Bean
    public Foo foo() {
        return new Foo();
    }

    public class Foo {

        public String getDestination() {
            return "foo";
        }
    }

}

You can also use property placeholders ${...}.

Legitimate answered 19/3, 2018 at 17:38 Comment(4)
Gary any reason why this has not been implemented yet? It would be really helpful if reference to the current bean is available in spel?Pyo
It's not a question of not implemented yet - there is no way for an Annotation to reference the bean it is used in; the value has to be static since it's stored in the Class file.Legitimate
I dont understand this code. it seems to return "Foo", not the value of foo.destinationCyrenaica
It is not clear what you mean, in SpEL, @foo.destination means call the getDestination() method on the bean named foo. If you can't figure it out, ask a new question showing your code and config.Legitimate
C
8

Using property placeholder is much easier.

@JmsListener(destination = "${mq.queue}")
public void onMessage(Message data) {

}
Comical answered 9/2, 2021 at 7:9 Comment(1)
Much better. ThanksLatashalatashia

© 2022 - 2024 — McMap. All rights reserved.