1

I'm using the Spring task executor system for the first time and just cannot get it to work...

I've read the various posts on SO already about this but can get no indication that the task is being executed. First of all I tried @Scheduled annotations on my service beans but after reading that this encountered problems with AOP proxies I'm using straight XML configuration as so:

<task:executor id="executorWithPoolSizeRange"
        pool-size="5-25"
        queue-capacity="100" />       
 <task:scheduler id="taskScheduler" pool-size="2" /> 
 <task:scheduled-tasks>
 <task:scheduled ref="fileWriter" method="test" fixed-rate="5000" />
</task:scheduled-tasks>

With fileWriter bean being a regular Spring bean with the test method as so:

public void test (){
   System.err.println("run in job");
}

From running with DEBUG logging settings I know the following:

  1. The beans are loaded and initialised.
  2. If I mistype the name of the 'method' attribute, an exception is thrown, so the task definition is at least parsed.
  3. There is nothing in the debug statements indicating any activation of the task
  4. A breakpoint in the test method is never triggered.

I'm expecting to see that every 5 seconds while either my app or spring unit tests are running, to see the message from the test() method printed out on the console. I'm using Spring 3.0.6 and testing the app running through Jetty in Eclipse 3.7 on Mac 10.6 Java 6. All other Spring features we use (database, security, MVC work fine). Would be really grateful for any suggestions!

Community
  • 1
  • 1
otter606
  • 335
  • 1
  • 4
  • 21
  • 1
    I havent used it with XML but when I have used it with annotations then the class that is being scheduled has to have @Component annotation. What's your definition for fileWriter? – RNJ Oct 15 '12 at 08:54
  • Thanks for replying - ` ` and I've added a Component annotation and added the package to the list scanned: ``. But still no activation of the method ( nor when I was scheduling a method in a @Service bean) – otter606 Oct 15 '12 at 09:19
  • Just realised I mistyped the package names in the previous comment: should be `com.blah.archive.FileArchiveWriter` and `` – otter606 Oct 15 '12 at 09:26
  • Personally I find the annotations easier to deal with. Ignoring your AOP issues does it work with annotations? If so then you can convert to xml once it works with annotations – RNJ Oct 15 '12 at 09:27
  • No, it was the lack of success with annotations which prompted me to try the XML config. I agree the annotations are more attractive to use, and would be my preference if I could get the things to work! – otter606 Oct 15 '12 at 10:47

2 Answers2

1

If you haven't try adding Quartz to your classpath (you shouldn't need it but...).

Whats most likely happening is that the scheduler is running and failing to execute your proxy. The proxy probably throws an exception and the scheduler's exception policy is probably discarding it.

The other option... (and I am probably going to get down voted) is to not use Springs scheduler. Unless you need the Quartz Cron stuff I find Springs Task scheduler overly complicated and yet weak compared to:

Guava's ListeningScheduledExecutorService

The listening executor service will allow you to chain events. You can easily wrap the above in some service bean. Yes I know you probably want the decoupling that Spring provides... but you can get far better decoupling and event based by combing the ExecutorService and Guava's EventBus.

Adam Gent
  • 47,843
  • 23
  • 153
  • 203
  • Thanks for the suggestion, I'll look into it. Since my last comment I tried Quartz as per chapter 25 of the the Spring docs which worked very nicely - but I'll have a look at this for sure, I wasn't aware of it. – otter606 Oct 16 '12 at 07:55
0

Try this:

 <task:scheduled-tasks scheduler="taskScheduler">
   <task:scheduled ref="fileWriter" method="test" fixed-rate="5000" />
 </task:scheduled-tasks>
Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
  • Thanks, no success though. I understand that if a scheduler isn't explicitly mentioned, a default scheduler is used. Do you know if there is any log statements I should be looking for during start-up which give any clue what might (not ) be going on? – otter606 Oct 15 '12 at 10:45
  • Hmm... Maybe problem is with your class? Try using some simple class with one method. Also try to increase your scheduler pool. – Aleksandr M Oct 15 '12 at 11:28
  • I think it's to do with the configuration... I've now got it running using XML config in the Spring JUnits by re-ordering the list of classpath elements in the test's ContextConfiguration annotation (interestingly it seems to hit the method several times each timepoint ) but still nothing in the application itself even with the same ordering of config files in web.xml. But at least it seems it can work now.... – otter606 Oct 15 '12 at 13:24
  • `(interestingly it seems to hit the method several times each timepoint )` It seems like you initializing multiple instances of the same bean. – Aleksandr M Oct 15 '12 at 13:28
  • Yes, this turned out to be an artifact of running the Spring Junit tests - each test class launched a new scheduler. Anyway, sorry to be defeatist and thank you for your suggestions but I'm having to give up on this for now, I have no idea why it runs in the tests but not in the application. I've set up an old-fashioned Timer task via Spring and that seems to work fine, it's not as powerful as the scheduler but will have to do for the moment. Perhaps it will work in 3.1 when we upgrade! – otter606 Oct 15 '12 at 22:06