简体   繁体   中英

@SendTo not working in JMS (ActiveMQ integration) with Spring Boot

I am facing a weird issue, I am not able to send my messages to a demoQueue in ActiveMQ using only @SendTo("demoQueue") annotation, I have tried following, I managed to send a message to the queue using JmsTemplate but I also read in a blog that @SendTo will do this for you.

Below is the code that I tried, the version that isn't working.

@Component
public class ProducerTask {
private static final Logger LOG = LoggerFactory.getLogger(ProducerTask.class);
    @Scheduled(cron = "0 0/3 * * * *")
    @SendTo("demoQueue")
    public String pushToQueue() {
        String str = "Running scheduled task >> " + ZonedDateTime.now();
        LOG.info(str);
        return str;
    }
}

The version with JmsTemplate that is working fine.

@Component
public class ProducerTask {

private static final Logger LOG = LoggerFactory.getLogger(ProducerTask.class);

    @Autowired
    private JmsTemplate jmsTemplate;

    @Scheduled(cron = "0 0/3 * * * *")
    public String pushToQueue() {
        String str = "Running scheduled task >> " + ZonedDateTime.now();
        LOG.info(str);
        jmsTemplate.setDefaultDestinationName("demoQueue");
        jmsTemplate.send(new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                return session.createTextMessage(str);
            }
        });
        // TODO put the logic for consuming in different bean and inject here and call that method here.
        return str;
    }
}

Please explain, I am confused here. Thanks.

You can't use the @SendTo annotation as part of any method. It works within the context of a @JmsListener (or another listener, like @KafkaListener ), see this blog post .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM