spring - provide user defined value to scheduler at run time -
i have create xml file scheduler unable run time customization code.
below xml file
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:task="http://www.springframework.org/schema/task" xsi:schemalocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"> <bean id="runmetask" class="com.spring.server.tasks.sharemetask" /> <bean name="runmejob" class="org.springframework.scheduling.quartz.jobdetailbean"> <property name="jobclass" value="com.spring.server.job.sharemejob" /> <property name="jobdataasmap"> <map> <entry key="runmetask" value-ref="runmetask" /> </map> </property> </bean> <bean id="simpletrigger" class="org.springframework.scheduling.quartz.simpletriggerbean"> <property name="jobdetail" ref="runmejob" /> <property name="repeatinterval" value="5000" /> <property name="startdelay" value="1000" /> </bean> <bean id="crontrigger" class="org.springframework.scheduling.quartz.crontriggerbean"> <property name="jobdetail" ref="runmejob" /> <property name="cronexpression" value="* 0/10 * * * ?" /> </bean> <bean id="scheduler" class="org.springframework.scheduling.quartz.schedulerfactorybean"> <!-- <property name="jobdetails"> <list> <ref bean="runmejob" /> </list> </property> --> <property name="triggers"> <list> <ref bean="crontrigger" /> </list> </property> </bean> </beans>
.
import org.quartz.jobexecutioncontext; import org.quartz.jobexecutionexception; import org.springframework.scheduling.quartz.quartzjobbean; import com.spring.server.tasks.sharemetask; public class sharemejob extends quartzjobbean { private sharemetask sharemetask; public void setrunmetask(sharemetask sharemetask) { this.sharemetask = sharemetask; } protected void executeinternal(jobexecutioncontext context) throws jobexecutionexception { system.out.println("sharemejob"); sharemetask.printme1(); } }
.
public class sharemetask { public void printme2() { system.err.println("spring 3 + quartz 1.8.6 ~"); } public void printme1() { system.err.println("spring job execution"); } }
when time reached above class call , method run. if admin want configure cron @ run time how can configure , please tell me code run time configuration , starting scheduler.
i found there methods in shecduler shutdown , start how can use these method @ run time.
we can store cron-expression properties fil configure dynamically, can modify when ever need without touching application code , repacking.
step(1): create file under /path/myfile.properties cronexp.value=* 0/10 * * * ?
step (2): modify spring xml below:
<context:property-placeholder location= "file:/path/myfile.properties" /> <bean id="crontrigger" class="org.springframework.scheduling.quartz.crontriggerbean"> <property name="jobdetail" ref="runmejob" /> <property name="cronexpression" value="${cronexp.value}" /> </bean>
Comments
Post a Comment