Search code examples
javaspringamazon-web-servicesspring-bootjmstemplate

Should I manually reset state-data of Autowired Bean in Spring (since it is Singleton)?


I am using org.springframework.jms.core.JmsTemplate; in my project to send messages to SQS queues.

I autowired JmsTemplate in a @Service

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

@Service
public class AmazonSQSPublisherImpl implements AmazonSQSPublisher {
    @Autowired
    private JmsTemplate jmsTemplate;
    @Override
    public boolean sendMessageToFifo(String queueName, String message,
                                     String messageGroupId, String messageDedupeId, long delayTime) throws EventBrokerException {
        jmsTemplate.setDeliveryDelay(delayTime);
        jmsTemplate.send(queueName, session -> {/* somemessage here*/});
    }

    @Override
    public boolean sendMessage(String queueName, String message) throws EventBrokerException {
        jmsTemplate.convertAndSend(queueName, message);
    }
}

Here I use setDeliveryDelay inside the jmsTemplate. So my question is: As I known, bean is singleton by default. if I used the setter of the jmsTemplate bean, should I reset it? In my example, should I add this at the end of method sendMessageToFifo:

jmsTemplate.setDeliveryDelay(0);

If I don't add this, when I call sendMessage rather than sendMessageToFifo, the delivery delay will not be zero, right?

Also, I want to ask if any best practices for call setter of autowired bean??


Solution

  • All beans are singleton by default, so yes one way is to reset it.

    But imho thats an ugly solution.

    What you should do is to define a dedicated JmsTemplate @Bean that you custom configure and then autowire where you need.

    If you need another JmsTemplate for any other place you define a second @Bean and you separate these two by using @Qualifiers.