Spring task scheduler + jboss - java

I'm having an strange behavior with spring task schedule on jboss 6.3.0.GA. I don't know why everytime when a task is launched in jboss it's launched twice at same time, in tomcat just once a time.
<task:scheduler id="taskScheduler" pool-size="1"/>
<task:scheduled-tasks scheduler="taskScheduler">
<task:scheduled ref="jobListener" method="pickUpChanges" cron="*/5 * * * * ?"/>
</task:scheduled-tasks>
spring.version: 3.1.1.RELEASE
Any help?

You cannot have both scheduler corn expression and fixed-delay remove one of them.
<task:scheduled ref="jobListener" method="pickUpChanges" cron="*/5 * * * * ?"/>
or
<task:scheduled ref="jobListener" method="pickUpChanges" fixed-delay="3000"/>
Refer the link for more detials. In short in your case two triggers are triggered one for fixed-delay and other for cron expression resulting in two times execution of the method.

Related

How to call spring scheduler from UI

I have defined a spring scheduler and it works automatically based on the cron i gave but i would like to call the scheduler from UI so that this scheduler can be run whenever some one wants to run.
<bean id="schedulerToCall" class="validPackagename.schedulerToCallTask" />
I would like to call this spring bean in some controller manually.
how to call that ?
Thanks
for example your context config is like this:
<bean id="schedulerToCall" class="validPackagename.SchedulerToCallTask" />
<task:scheduled-tasks>
<task:scheduled ref="schedulerToCall" method="runTaskMethod" cron="0 1 0 * * MON"/>
</task:scheduled-tasks>
In SchedulerToCallTask.java:
#Component
public class SchedulerToCallTask{
In the controller class you can just:
#Resource
SchedulerToCallTask schedulerToCallTask;
In the controller function you want to call this task:
schedulerToCallTask.runTaskMethod();
If I understood your query correctly. since cron runs based on the cron parameters, you need to pass the current time in the cron parameter. Also that cron parameters should be passed dynamically when the use want to run.
eg:
<task:scheduled ref="cronService" method="runCron" cron="* 0 0 * * ?"></task:scheduled>

Scheduled Task in Spring waiting for other task of the same scheduler

I have in my xml file something like
<task:scheduled-tasks scheduler="idOfScheduler">
<task:scheduled ref="myBean" method="update1" cron="...">
<task:scheduled ref="myBean" method="delete2" cron="...">
</task:scheduled-tasks>
<task:scheduler id="idOfScheduler" pool-size="5">
The problem is that when method update1 takes very long, method delete2 is not executed until update1 finishes. Is it because both tasks are scheduled in the same scheduler (it is configured with a max pool of 5)? Could one point out to the documentation that describes this part?

Calling Java Web Service Method In A Regular Time Period

I have a java web service in centos 7 on tomcat 7, and i use this service to update a database. I need to call its method regulary, example:
createCustomer will called in every 12 hour,
createOrder will called in every 3 minute etc.
How can i write this triger and where this code have run (in same server or something else)
You can give quartz scheduler a shot. Here you can find more infomation regarding the subject :
http://quartz-scheduler.org/generated/2.2.1/html/qs-all/#page/Quartz_Scheduler_Documentation_Set/_qs_all.1.009.html#
In a nutshell you can define an xml file, stating which task would work on which condition ( or time frame ). In the example below :
<!-- Order Recorder Job -->
<bean id="orderRecorderJob" class="com.cemgunduz.btcenter.job.OrderRecorderJob"/>
<bean id="orderTask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="orderRecorderJob"/>
<property name="targetMethod" value="execute"/>
</bean>
<bean id="orderRecorderJobTrigger"
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="orderTask" />
<property name="cronExpression" value="0 0/5 * * * ?" />
</bean>
A task is defined named orderTask, which is the execute method in orderRecorderJob.
This task is associated with a cron expression, thereby the trigger is defined. So to sum it all up the example above would trigger execute method of orderRecorderJob class every five minutes as stated on its cron expression ( 0 0/5 * * * ?). More on cron expression syntax :
http://www.quartz-scheduler.org/documentation/quartz-1.x/tutorials/crontrigger

How to stop jobs scheduled using spring task

I have implemented a sample spring scheduled task, with an applicationContext as follows,
<task:scheduled-tasks scheduler="myScheduler">
<task:scheduled ref="cron" method="show" cron="0/10 * * * * ?"/>
<task:scheduled ref="cron" method="show2" cron="0/15 * * * * ?"/>
</task:scheduled-tasks>
<task:scheduler id="myScheduler" pool-size="10"/>
How can I stop this schedule method?
Inject the ThreadPoolTaskScheduler into another bean, and invoke shutdown(). If that isn't acceptable, you could configure the cron bean to accept a flag. For example:
public class Job() {
private final AtomicBoolean stop = new AtomicBoolean(false);
public void show() {
if (stop.get()) {
return;
}
...
}
public void stop() {
stop.set(true);
}
}
Note that this won't remove the job from the scheduler. The only way to prevent that would be to obtain a reference to the ScheduledFuture and call cancel().
Depends on what you mean by "stop".
Business Condition Stop:
Stop as result of a business condition, you should have those conditions evaluated in your methods and just simply not execute the code. This way you can stop unwanted execution at runtime, run your logic to handle the condition fail (logging,notification,etc) as a result.
Non Business Condition:
Externalize the chron expression to properties file or as I prefer a system variable in the JVM. Then you can just change the property value to a 9999 scenario to stop any execution.
System Variable Example.
<task:scheduled-tasks scheduler="myScheduler">
<task:scheduled ref="cron" method="show" cron="#{systemProperties['chron1']}"/>
<task:scheduled ref="cron" method="show2" cron="#{systemProperties['chron2']}"/>

Scheduling tasks to run once, using the Spring task namespace

I'm setting up a scheduled tasks scheme in spring, using the task namespace.
I want to schedule most tasks to fire according to a cron expression, and some to fire just once, a fixed delay after startup, and then never again (i.e. what setting repeatCount to 0 on a SimpleTriggerBean would achieve).
Is it possible to achieve this within the task namespace, or do I need to revert to defining beans for my triggers?
If you don't need an initial delay, you can make it run 'just once' on startup as follows:
<task:scheduled-tasks>
<!-- Long.MAX_VALUE ms = 3E8 years; will run on startup
and not run again for 3E8 years -->
<task:scheduled ref="myThing" method="doStuff"
fixed-rate="#{ T(java.lang.Long).MAX_VALUE }" />
</task:scheduled-tasks>
(Of course, if you think your code is going to run for longer than 3E8 years, you may need a different approach...)
If you need an initial delay, you can configure it as follows (I'm testing with Spring 3.1.1) - this doesn't require any additional dependencies and you don't have to write your own trigger, but you do have to configure the PeriodicTrigger provided by Spring:
<bean id="onstart" class="org.springframework.scheduling.support.PeriodicTrigger" >
<!-- Long.MAX_VALUE ms = 3E8 years; will run 5s after startup and
not run again for 3E8 years -->
<constructor-arg name="period" value="#{ T(java.lang.Long).MAX_VALUE }" />
<property name="initialDelay" value="5000" />
</bean>
<task:scheduled-tasks>
<task:scheduled ref="myThing" method="doStuff" trigger="onstart" />
</task:scheduled-tasks>
Spring 3.2 appears to support the "initial-delay" attribute directly, but I haven't tested this; I'd guess this works:
<task:scheduled-tasks>
<task:scheduled ref="myThing" method="doStuff"
fixed-rate="#{ T(java.lang.Long).MAX_VALUE }"
initial-delay="5000"/>
</task:scheduled-tasks>
My working example:
<bean id="whateverTriggerAtStartupTime" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="whateverJob"/>
<property name="repeatCount" value="0"/>
<property name="repeatInterval" value="10"/>
</bean>
If you have a look at the Task namespace XSD, you'll see that there are only three different configuration types: fixed-delay, fixed-rate and cron.
And if you look at the source of ScheduledTasksBeanDefinitionParser, you'll see that no more than one of these values are evaluated. Here is the relevant part:
String cronAttribute = taskElement.getAttribute("cron");
if (StringUtils.hasText(cronAttribute)) {
cronTaskMap.put(runnableBeanRef, cronAttribute);
}
else {
String fixedDelayAttribute = taskElement.getAttribute("fixed-delay");
if (StringUtils.hasText(fixedDelayAttribute)) {
fixedDelayTaskMap.put(runnableBeanRef, fixedDelayAttribute);
}
else {
String fixedRateAttribute = taskElement.getAttribute("fixed-rate");
if (!StringUtils.hasText(fixedRateAttribute)) {
parserContext.getReaderContext().error(
"One of 'cron', 'fixed-delay', or 'fixed-rate' is required",
taskElement);
// Continue with the possible next task element
continue;
}
fixedRateTaskMap.put(runnableBeanRef, fixedRateAttribute);
}
}
So there is no way to combine these attributes. In short: the namespace won't get you there.
This works and is way easier than the other answers.
// Will fire the trigger 1 + repeatCount number of times, start delay is in milliseconds
simple name: 'mySimpleTrigger', startDelay: 5000, repeatCount: 0

Categories