javaWeb SSH 设置后台自启线程,一般用于系统的自主更新
XML写法:
<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<bean name="job1" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="com.lesport.tools.TimesThread" /><!-- 任务执行类 -->
<property name="jobDataAsMap">
<map>
<entry key="timeout" value="0" />
</map>
</property>
</bean>
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
<property name="jobDetail" ref="job1" />
<property name="startDelay" value="10000" /><!-- 调度工厂实例化后,经过0秒开始执行调度 -->
<property name="repeatInterval" value="900000" /><!-- 每15分钟调度一次 -->
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="simpleTrigger" />
</list>
</property>
</bean>
</beans>
类文件:
package com.lesport.tools;
import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.lesport.service.AdminService;
import com.lesport.service.impl.AdminServiceImpl;
import com.opensymphony.xwork2.ActionContext;
/**
* 线程类
* @author Administrator
*
*/
public class TimesThread extends QuartzJobBean{
@Resource
private AdminService adminService;
private int timeout=0;
private WebApplicationContext app;
//调度工厂实例化后,经过timeout时间开始执行调度
public void setTimeout(int timeout) {
this.timeout = timeout;
}
//初始化
public void init(){
app = ContextLoader.getCurrentWebApplicationContext();
}
//任务调用
@Override
protected void executeInternal(JobExecutionContext arg0)
throws JobExecutionException {
init();
try {
System.out.println("实例数量:"+app.getBeanDefinitionCount());
System.out.println("定时任务执行中…");
adminService=(AdminService) app.getBean(AdminService.class);
adminService.Updata_MapData();//自动更新约战信息是否有效adminService.Updata_OPearLeveling();
adminService.Updata_OPearLeveling();
} catch (Exception e) {
e.printStackTrace();
}
}
}