Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Spring scheduler Task

 
Baskar Sikkayan
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I use Spring Quartz scheduler(Clustered) in my application as follows. Inside the scheduler, i instantiate a thread class to spawn a new thread for each record and inside the thread class, i use reference of other bean class using wiring concept. As the thread class inside the scheduler is instantiated using the new operator, the dependency injection inside the thread class is not working. Is there any way, we can achieve this?

From applicationContext.xml,



CampaignNow.java class,



Thread class,



Thanks,
Baskar.S
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are a couple of ways you could handle this. My first suggestion would be to make an async service bean.



Then autowire AsyncCamapignService in your CampaignNowJob and call the method.

dont forget to add



to your XML configuration.

see the docs for more info on @Async
http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/scheduling.html#scheduling-annotation-support-async
 
Baskar Sikkayan
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks for the info.

I may not be able to go with this as i use ThreadPoolExecutor and the excute method needs runnable implementor. Also, i can configure many things in ThreadPoolExecutor. Is there any other equivalent class in Spring?

Thanks,
Baskar.S
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can specify an executor on the annotation-driven tag. Alternatively you can specify any executor on the annotation itself see the documentation here:
http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/scheduling.html#scheduling-annotation-support-qualification

The executor provided must of course must have a bean definition in your context. It can be any Executor of type java.util.concurrent.Executor or org.springframework.core.task.TaskExecutor. Since ThreadPoolExecutor meets this criteria there is no reason you could not use it. Be aware if there are multiple methods within the class needing the async behavior this annotation can be applied at the class level as well.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic