Home > Blockchain >  Using Spring Boot, how can I inject a JMS destination in @JmsListener annotation?
Using Spring Boot, how can I inject a JMS destination in @JmsListener annotation?

Time:03-12

Using Spring Boot, how can I inject my JMS topic from a config property? I want to define the topic/queue name in application.properties to be injected at runtime.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Service;

@Service
public class DispatcherService {

    @Autowired ConfigProperties cp;
    private final String dest = "my-topic";
    private final String dest2 = cp.getTopic();

    // WORKS
    @JmsListener(destination = "my-topic")
    public void receive1(String message) {
        ...
    }

    // WORKS
    @JmsListener(destination = dest)
    public void receive2(String message) {
        ...
    }

    // DOES NOT WORK - "Attribute must be constant"
    @JmsListener(destination = dest2)
    public void receive3(String message) {
        ...
    }

    // DOES NOT WORK - can I inject this here, somehow?
    @JmsListener(destination = @Value("${topic}"))
    public void receive4(String message) {
        ...
    }
}

CodePudding user response:

Try this:

@JmsListener(destination = "${my-topic}")

CodePudding user response:

@JmsListener(destination = "\${config.property.key}"), This should take the topic/queue name from configs.

  • Related