1

I am trying to implement the annotations @RefreshScope and @Retryable in my code. I want them to work simultaneously but I am not sure if it is possible. Currently, @Retryable works as it is suppose to. If the method throws an exception, it will retry max:5 times with 6 second down-times. The problem: I need @Value property to be refreshed, and @RefreshScope should take care of this BUT a different method takes @Value property as an argument. So really, if methodB that has @Retryable fails, I need methodA that takes the @Value property to refresh. If not, methodB retries with the same incorrect values. You're probably confused so here is an example:

@RefreshScope
@Service
public class JMSTopicClient {

  @Value("#{'${jms.newTopic.topic}'}")
  private String newTopicDestination;

  private static int COUNTER = 0;


  public boolean methodA(String message, ConcurrentHashMap<String, String> jmsHeaderDetails){
    InitialContext ctx = new InitialContext(newTopicEnv);
    ConnectionInfo conInfo = connectionInfo.get();
    conInfo.topic = (javax.jms.Topic) ctx.lookup(destinationTopic);
    //rest of the code is irrelevant.
  }

Notice, ctx.lookup(destinationTopic) takes the value of destinationTopic. Ultimately, this is the method that needs to be refreshed in case the value is changed in config repo (if methodB fails/retries)

   @Retryable(value = { Exception.class }, maxAttempts = 5, backoff = @Backoff(delay = 6000))
  public boolean methodB(Document doc, ConcurrentHashMap<String, String> jmsHeadersDetails) {
    COUNTER++;
    String xmlEventNotify = DocumentHelperFactory.get().convertDocumentToString(doc);
    boolean result = methodA(xmlEventNotify, jmsHeadersDetails);
    if(result == false) {
        if(COUNTER<5) {
            throw new RuntimeException();
        } else {
            COUNTER = 0;
            return result;
        }
    } else {
        return result;
    }
}

Here, methodB is retrying, but it is retrying with the same incorrect values that methodA takes in. I need the whole process methodA-methodB to retry if methodB throws exception. I'm confident there is a workaround but I cannot find any documentation where @RefreshScope and @Retryable work simultaneously.

Thank you in advance!

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
JCP
  • 169
  • 1
  • 7
  • Works for me. It might be sensitive to the order that the interceptors are added to the proxy (dunno TBH). If you have a minimal sample - no JMS etc. - I could try and debug it for you. – Dave Syer Oct 15 '19 at 08:23
  • @DaveSyer Thanks for the reply! – JCP Oct 21 '19 at 20:25

0 Answers0