1

I am working with cron in a STS web app I am developing and I am having a string format issue when trying to run the SpringMailSender on a cron timer. I am pulling the value for the cron from an external properties file and for some reason it does not seem to be liking it. Any ideas? Here is my code...

public class Timer {

     @Autowired
      private ApplicationContext ctx;

     @Autowired
        private SpringMailSender springMailSender;

    @Scheduled(cron="${ctx.getMessage('timer.time', null, null)}")
    public void timer()
    {
        System.out.println("timer() in Timer Class has been stepped into");
        try {
            springMailSender.sendMail();
            } catch (Exception e) {
                e.printStackTrace();
            }
        System.out.println("Method executed on every 2nd Monday of each month. Current time is :: "+ new Date());
    }

}

The info in the external properties file looks like this...

timer.time=0 0 8 ? 1/1 MON#2 *

the error I am getting is this...

"java.lang.IllegalStateException: Encountered invalid @Scheduled method 'timer': Could not resolve placeholder 'ctx.getMessage('timer.time', null, null)' in string value "${ctx.getMessage('timer.time', null, null)}"
Stevo
  • 85
  • 3
  • 11
  • possible duplicate of [Could not resolve Spring property placeholder](http://stackoverflow.com/questions/4779572/could-not-resolve-spring-property-placeholder) – ruhungry Mar 28 '14 at 14:55
  • I think you're looking for `#{...}`. – Sotirios Delimanolis Mar 28 '14 at 14:58
  • Why aren't you simlpy using a `PlaceHolderConfigurer` and `${timer.time}` instead of trying to abuse a `MessageSource` to obtain the property. – M. Deinum Mar 28 '14 at 15:07
  • Dear Sotirios; well that sort of worked. It gave me a new error because now with the "#" symbol in it it seems to be trying to read {ctx.getMessage('timer.time', null, null)} as the cron expression and the error says "java.lang.IllegalStateException: Encountered invalid @Scheduled method 'timer': Cron expression must consist of 6 fields (found 3 in "#{ctx.getMessage('timer.time', null, null)}")" – Stevo Mar 28 '14 at 15:09
  • @M. Deinum. I've never used that before. How would the code look? – Stevo Mar 28 '14 at 15:14

1 Answers1

0

The @Scheduled annotation has support for using propert placeholders (i.e. ${...}). For this to work you n eed to have a property placeholder configured.

<context:placholder-configurer location="path/to/your/external.properties" />

Then in your @Scheduled annotation you can simply reference the properties from the file.

@Scheduled(cron="${timer.time}")

That way you can also remove the dependency on the ApplicationContext.

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
  • do you mean Spring Property Placeholder Configurer? I'm not seeing anything for context:placeholder-configurer on Google. – Stevo Mar 28 '14 at 15:46
  • What Deinum meant was to place this element context:placeholder-configurer in the bean definitions or context file (something like applicationContext.xml) where your Timer bean defined(since I dont see any annotation). – Prasad Mar 28 '14 at 16:22