How do I schedule a method in a spring MVC controller? - java

I have defined various scheduler in my configuration file as follows:
<task:executor id="xxxxxExecutor" pool-size="${async.executor.pool.size}"/>
<task:scheduler id="xxxxwwwScheduler" pool-size="1" />
<task:scheduler id="qqqqSchedular" pool-size="1" />
<task:scheduler id="lastScheduler" pool-size="1" />
My controller has been annotated via #Controller annotation. How do I specify a particular Scheduler in #Scheduled annotation in Spring?
p.s. I am trying to schedule a method in a controller using #Scheduled annotation.

Using multiple schedulers and pointing to them via the #Scheduled annotation is unfortunately not possible.
However, if you really do need that flexibility, you can define the jobs in XML:
<task:scheduled-tasks scheduler="myScheduler">
<task:scheduled ref="beanA" method="methodA" fixed-delay="5000"/>
</task:scheduled-tasks>
That allows you to specify the exact id of the scheduler you need to use, and then simply reference the actual task.
Hope this helps.

Related

Which executor and scheduler is getting used

I was reading below article to implement executor and scheduler.
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html#scheduling-enable-annotation-support
Can any one please tell me which executor and scheduler does it use when we specify below xml entry
<task:annotation-driven executor="myExecutor" scheduler="myScheduler"/>
<task:executor id="myExecutor" pool-size="5"/>
<task:scheduler id="myScheduler" pool-size="10"/>
Internally spring uses org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor for namespace of task:executor and org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler for task:scheduler. Refer respective API docs here and here
specify the package
any classes inside that package which have the annotation #Configuration, #EnableScheduling and method with #Scheduled(cron="") will be scheduled on the mentioned time in the cron expression.

how can I pass default information like loginUserInfo to MyScheduler in spring?

I am using annotation based scheduler like #Scheduled(fixedDelay = 1200000)
and I want to pass information like langId , loginUserId etc.
I have a scheduler MyScheduler, I have configured it in .property file as:
<task:annotation-driven />
<bean id="myScheduler" class="ab.abc.txn.service.MyScheduler"></bean>
use <property in bean defination

Implementing Web Services timer for Tweet Service

My problem is that I have a function implemented on my website which searches for Particular Tweet when I press the button. I want it to make it automatic such that, that function is called again and again after every two minutes, regardless some one uses the website or not.. How to do this in java?? Spring Solution is better
You could use Spring scheduling (see documentation here)
From the doc:
XML config
<context:component-scan base-package="some.package" />
<task:annotation-driven executor="myExecutor" scheduler="myScheduler"/>
<task:executor id="myExecutor" pool-size="5"/>
<task:scheduler id="myScheduler" pool-size="10"/>
Java bean
#Component
public class SomeBean {
#Scheduled(fixedRate=5000)
public void doSomething() {
// something that should execute periodically
}
}

Spring #Async without xml config

25.5.3 The Element
To enable both #Scheduled and #Async annotations, simply include the
'annotation-driven' element from the task namespace in your
configuration.
<task:annotation-driven executor="myExecutor" scheduler="myScheduler"/>
<task:executor id="myExecutor" pool-size="5"/>
<task:scheduler id="myScheduler" pool-size="10"/>
Notice that an executor reference is provided for handling those tasks
that correspond to methods with the #Async annotation, and the
scheduler reference is provided for managing those methods annotated
with #Scheduled.
Is there anyway to do this without XML?
This is possible with the newly-released Spring 3.1, but not 3.0 - See #EnableAsync and #EnableScheduling.
For #Dejel question: (as I am not able to post comment)
It is possible to specify executor for certain task. This can be achieved by specyfing executor name as value of #Async annotation. See:
https://jira.spring.io/browse/SPR-6847
http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/scheduling/annotation/Async.html
http://www.baeldung.com/spring-async

How to schedule a new method in a Spring enabled web app at runtime?

Right now I have one bean with a #Scheduled method working fine; it's declared in my applicationContext.xml.
<!-- some JPA stuff -->
<bean id="aWorkingBean" class="some.package.WorkingBean">
<property name="someDAO" ref="someDAO" />
</bean>
<task:annotation-driven scheduler="myScheduler" />
<task:scheduler id="myScheduler" pool-size="10" />
What I'm trying to do is programmatically schedule another method (e.g. loading some annotated class and inject its dependencies) upon request. Something like:
WebApplicationContext ctx = ContextLoader.getCurrentWebApplicationContext();
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(NonWorkingBean.class);
// add DAO references...
ctx.registerBeanDefinition("nonWorkingBean", builder.getBeanDefinition()); // <-- this doesn't work
Obviously it doesn't work because the XmlWebApplicationContext is read-only and has no registerBeanDefinition method. Is there any other way to achieve this?
I'm using Tomcat 6.0.29 and Spring 3.0.4
<task:scheduler> and #Scheduled is really just a convenience approach to scheduling static tasks. It's not really suitable for dynamic scheduling. Yes, you can make it work, but it's going to be awkward.
When you put <task:scheduler id="myScheduler"> into your config, Spring creates a TaskScheduler bean called myScheduler. This can be injected into your own beans, and can be invoked programmatically in order to schedule new tasks. You'll need to create a Runnable to pass to the TaskScheduler, but that should be simple enough.
There are several ways to do that, but you would usually use an AutowireCapableBeanFactory.
Here's one way to do it:
final WebApplicationContext ctx =
ContextLoader.getCurrentWebApplicationContext();
// create the object yourself
// and inject the dependenices you want manually
final Object existingBean = initializeYourObjectHere();
AutowireCapableBeanFactory beanFactory = ctx.getAutowireCapableBeanFactory();
// now autowire it, injecting the remaining dependencies
beanFactory.autowireBeanProperties(
existingBean, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);
// run post processors and register bean under this name
beanFactory.initializeBean(existingBean, "newBeanName");

Categories

Resources