spring定时器的动态设置
spring还是以整合已有的框架为主,它对时间的设置比较灵活。
在spring中,可以继承QuartzJobBean,也可以不做任何继承,当然写法也不一样,这里用的非继承的写法。
定时器的注册过程:1.创建bean,2.声明bean为一个定时器,3.设置任务时间,4.在调度中注册定时器
===================================================================
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<bean id="mySessionFactory" value="classpath:hibernate.cfg.xml"/>
</bean>
<bean id="myTxManager" ref="mySessionFactory"/>
</bean>
<tx:advice id="txAdvice" transaction-manager="myTxManager">
<tx:attributes>
<tx:method name="find*" propagation="REQUIRED" read-only="true"/>
<tx:method name="get*" propagation="REQUIRED" read-only="true"/>
<tx:method name="save*" propagation="REQUIRED" read-only="false" rollback-for="com.HSException"/>
<tx:method name="audit*" propagation="REQUIRED" read-only="false" rollback-for="com.HSException"/>
<tx:method name="do*" propagation="REQUIRED" read-only="false" rollback-for="com.HSException"/>
<tx:method name="add*" propagation="REQUIRED" read-only="false" rollback-for="com.HSException"/>
<tx:method name="modify*" propagation="REQUIRED" read-only="false" rollback-for="com.HSException"/>
<tx:method name="delete*" propagation="REQUIRED" read-only="false" rollback-for="com.HSException"/>
<tx:method name="create*" propagation="REQUIRED" read-only="false" rollback-for="com.HSException"/>
<tx:method name="update*" propagation="REQUIRED" read-only="false" rollback-for="com.HSException"/>
<tx:method name="*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="systemServiceMethods" expression="execution(* com.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="systemServiceMethods"/>
</aop:config>
<bean id="testJob" value="web.testJob"/>
<property name="jobDataAsMap">
<map>
<entry key="ftpConfigService">
<ref bean="ftpConfigService"/>
</entry>
</map>
</property>
</bean>
<bean id="testJob1" value="web.test1Job"/>
<property name="jobDataAsMap">
<map>
<entry key="ftpConfigService">
<ref bean="ftpConfigService"/>
</entry>
</map>
</property>
</bean>
<bean id="testTrigger" ref="testJob"/>
<property name="cronExpression" value="0 19 8,9,19,20 * * ?"/>
</bean>
<bean id="testTrigger1" ref="testJob"/>
<property name="cronExpression" value="0 18 8,9,19,20 * * ?"/>
</bean>
<bean id="testTrigger2" ref="testJob1"/>
<property name="cronExpression" value="0 17 8,9,19,20 * * ?"/>
</bean>
<!--
<bean id="testTrigger" ref="testJob"/>
<property name="startDelay" value="6000"/>
<property name="repeatInterval" value="3000"/>
</bean>
-->
<bean id="facbean" scope="prototype" />
</property>
</bean>
<bean id="ftpConfigService" scope="prototype" class="com.FtpConfigServiceSpringImp">
<property name="ftpConfigDAO">
<ref local="ftpConfigDAO"/>
</property>
</bean>
<beans>
================================================================
public class testJob extends QuartzJobBean{
private IFtpConfigService ftpConfigService;
public IFtpConfigService getFtpConfigService() {
return ftpConfigService;
}
public void setFtpConfigService(IFtpConfigService ftpConfigService) {
this.ftpConfigService = ftpConfigService;
}
@Override
protected void executeInternal(JobExecutionContext arg0)
throws JobExecutionException {
System.out.println("执行================");
try{
FtpConfig ftp = this.getFtpConfigService().findFtpConfigById("2");
System.out.println(ftp.getFtpUrl());
}catch(Exception e){}
}
===========================================================
public class testservlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
String time = request.getParameter("time");
WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext());
try{
SchedulerFactoryBean jobFact = (SchedulerFactoryBean)ctx.getBean("&facbean");
CronTriggerBean bean = (CronTriggerBean)ctx.getBean("testTrigger");
String newVal = "0 16 8,9,19,20 * * ?";
bean.setCronExpression(newVal);
jobFact.destroy();
jobFact.afterPropertiesSet();
}catch(Exception e){
e.printStackTrace();
}
response.sendRedirect("succ.jsp");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req,resp);
}
}