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" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
</bean>
<bean id="myTxManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" 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.HXException"/>
<tx:method name="audit*" propagation="REQUIRED" read-only="false" rollback-for="com.HXException"/>
<tx:method name="do*" propagation="REQUIRED" read-only="false" rollback-for="com.HXException"/>
<tx:method name="add*" propagation="REQUIRED" read-only="false" rollback-for="com.HXException"/>
<tx:method name="modify*" propagation="REQUIRED" read-only="false" rollback-for="com.HXSException"/>
<tx:method name="delete*" propagation="REQUIRED" read-only="false" rollback-for="com.HXException"/>
<tx:method name="create*" propagation="REQUIRED" read-only="false" rollback-for="com.HXException"/>
<tx:method name="update*" propagation="REQUIRED" read-only="false" rollback-for="com.HXException"/>
<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" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="web.testJob"/>
<property name="jobDataAsMap">
<map>
<entry key="ftpConfigService">
<ref bean="ftpConfigService"/>
</entry>
</map>
</property>
</bean>

<bean id="testJob1" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="web.test1Job"/>
<property name="jobDataAsMap">
<map>
<entry key="ftpConfigService">
<ref bean="ftpConfigService"/>
</entry>
</map>
</property>

</bean>

<bean id="testTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="testJob"/>
<property name="cronExpression" value="0 19 8,9,19,20 * * ?"/>
</bean>
<bean id="testTrigger1" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="testJob"/>
<property name="cronExpression" value="0 18 8,9,19,20 * * ?"/>
</bean>
<bean id="testTrigger2" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="testJob1"/>
<property name="cronExpression" value="0 17 8,9,19,20 * * ?"/>
</bean>

<!--
<bean id="testTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="testJob"/>
<property name="startDelay" value="6000"/>
<property name="repeatInterval" value="3000"/>
</bean>
-->
<bean id="facbean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="testTrigger"/>
<ref bean="testTrigger1"/>
<ref bean="testTrigger2"/>
</list>
</property>
</bean>

<bean id="ftpConfigDAO" scope="prototype" class="com.FtpConfigHibernateDAO">
<property name="sessionFactory">
<ref local="mySessionFactory" />
</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);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值