项目中的定时任务

记录一次我自己在项目中写的,或者复制的,定时任务。
首先这个其实算是一个工具类
包:com.xxxxx.common.scheduler
类和接口:ShedulerProcessor(定时任务处理者基类) Scheduler(任务处理者接口)
接口:

/**
*任务处理者接口
*/
public interface Scheduler{
		/*处理方法
		*/
		public abstract void process();
	
}

类:

/**
*定时任务处理者基类
*/
public class SchedulerProcessor{
	Logger log = LoggerFactory.getLogger( getClass() );
	//成员变量+get/set方法
	private String scheduler;
	public String getScheduler(){
		return scheduler;
	}
	public void setScheduler(String scheduler){
		this.scheduler = scheduler ;
	}
	//执行任务
	public void execute(){
			//获取接口的实现类
			Scheduler processor = SpringUtils.getBean(Scheduler.class,getScheduler());
			if(processor != null ){
				processor.process(); //说明得到了该接口的实现类,执行接口的方法,即实现类中的方法
			}else{
				log.error(scheduler+" not  start yet !");
			}
	}
}

上面的其实是通用的,写在工具包里面
下面的就是实际要做事的类了
包:com.xxxxx.web.scheduler
类:2个 SiebelCheckScheduler (清理LOV缓存) UnbidFieldEffectCheckScheduler(非标附件有效期校验)

SiebelCheckScheduler.java

@Service  ---交给Spring管理,不然无法注入
 public  class SiebelCheckScheduler implements  Scheduler{

	@Autowired
	ListOfValueService service;
	
	//内部类,去查询客户类型列表
	class MyThread implements ThreadRun{
		@Override
		public void run() throws Exception{
			JDBFactory.setAuthByAdmin();//设置管理员权限
			service.getList("CUSTOMER_TYPE");
		}
	}
	//执行定时任务
	@Override
	public void process(){
		try{
				service.clear(); //清理LOV缓存.
			}catch(Exception e){
			e.printStackTrace();
			}
	}
}

UnbidFileEffectCheckScheduler(这个是我自己后来加的,根据需求)

/**
*非标附件列表的有效期检验
*/
@Service
public class UnbidFileEffectCheckScheduler implements Scheduler{
	@Autowired
	AccountAttaMapper accountAttaMapper;
	
	@Override
	public void process(){
	//System.out.println("定时任务启动! 线程Id:"+Thread.currentThread().getId()); //只是自己测试用的
	try{
			//当前日期的转换,以便进行比较
			Date a = new Date(); //这中获取的日期是带有 时区那种,不能进行比较,需要格式化
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String formatd = sdf.format(a); //把日期转换成日期格式的字符串
			Date ndate = sdf.parse(formatd); //再把字符串转换成日期,也就成了 2019-03-19 这种,就可以进行比较了。
			//查有效时间数据
			List<AccountAttaModel> list = null;
			model.setLoginId(SystemUtils.getCurrentConfigModel().getLoginId());
			model.setLoginName(SystemUtils.getCurrentConfigModel().getLoginName());
			list = accountAttaMapper.selectContractAttaList(model); //我觉得是这样,关键我不知道怎么传参进来!
			for(AccountAttaModel accountAttaModel : list){ //得到合同下的每个非标附件
				Date  edate = DateUtils.toDate(   accountAttaModel.getEffectiveDate()  ); //得到数据库中的有效日期
				if("最终版".equals(accountAttaModel.getStatus()) && (edate.after(ndate)) ){ //这个是需求
							//说明已经过期
							accountAttaModel.setStatus("已过期");
					}
				}
		}catch(Exception e){
			e.printStackTrace();
		}



}//类尾




配置文件,明天再写
配置文件如下:
基本这里的配置文件全部都在 src/main/resources/spring 下,全部以spring_开头,这样多个配置文件也好扫描
扫描用 spring/spring_* 就可以了。具体在web.xml中:

指定配置文件的位置
<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>classpath:spring/spring_*.xml</param-value>	
</context-param>
类似的还有log4jConfigListener
<context-param>
	<param-name>log4jConfigListener</param-name>
	<param-value>classpath:log4j.properties</param-value>	
</context-param>
自定义配置文件,必须配置下面的,默认是applicationContext.xml
讲如何部署applicationContext的xml文件,如果在web.xml中不写任何参数配置信息,默认的路径是"/WEB-INF/applicationContext.xml,在WEB-INF目录下创建的xml文件的名称必须是applicationContext.xml。如果是要自定义文件名可以在web.xml里加入contextConfigLocation这个context参数:
<listener>
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 

spring_scheduler.xml
配置如下:

头,我不写了,但是在头里面有这样的写法:
<beans xmlns=.....
			http://.......aop 
			http://......aop/spring-aop-4.1.xsd
			default-autowire = "no" default-lazy-init = "false" > 
	<!--抽象的任务执行者-->
	<bean id="abstractSchedulerProcessor" abstract="true" class="com.xxxxx.common.scheduler.SchedulerProcessor" />

	<bean id="siebelcheckscheduler" class = "org.springframework.scheduling.quartz.CronTriggerFactoryBean">
		触发器调jobDetail按一定的表达式执行任务
		<property name="jobDetail">固定的名字
					  jobDetail工厂
					<bean class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
								指定一个抽象的任务执行者--基类去执行任务
								<property name="targetObject">
										<bean parent="abstractScheduleProcessor">
											<property  name="scheduler"  value="siebelCheckScheduler" />
										</bean>
								</property>
								<property name="targetMethod" value="execute" />
					</bean>
		</prroperty>
		<property name = "cronExpression"> 每天凌晨1点执行任务
				0 0 1 * * ? *
		</property>
	</bean>
<!--用师父的,规范化-->
<bean id="unbidcheckscheduler" class = "org.springframework.scheduling.quartz.CronTriggerFactoryBean">
		触发器调jobDetail按一定的表达式执行任务
		<property name="jobDetail">固定的名字
					  jobDetail工厂
					<bean class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
								指定一个抽象的任务执行者--基类去执行任务
								<property name="targetObject">
										<bean parent="abstractScheduleProcessor">
										去父类SchedulerProcessor中执行execute方法,该方法中调用了Scheduler的processs方法,所以只需
										要写类去集成Scheduler,重写process方法即可
											<property  name="scheduler"  value="unbidFileEffectCheckScheduler" />这里为引用的类,也就是实际干活的类,但是第一个字母要小写,不然无法找到。
										</bean>
								</property>
								<property name="targetMethod" value="execute" />
					</bean>
		</prroperty>
		<property name = "cronExpression"> 每5s执行任务
				0/5 * * * * ? *
		</property>
	</bean>

	调度器调用一个或者多个触发器
	<bean   id="schedulerBean" lazy-init="false" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
			<property  name="triggers">
					<list>
							<ref bean= "siebelcheckscheduler" />
							<ref bean= "unbidcheckscheduler">
					</list>
			</property>
	</bean>
</beans>

补充web.xml
全栈中文乱码问题解决

全栈中文乱码问题解决
<filter>
		<filter-name>EncodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<async-supported>true</async-supported>
		<init-param>
				<param-name>encoding</param-name>
				<param-value>UTF-8</param-value>
		</init-param>
</filter>
<filter-mapping>
		<filter-name>EncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
</filter-mapping>

认证过滤
<filter>
		<filter-name>AuthFilter</filter-name>
		<filter-class>com.xxxxx.web.filter.AuthFilter</filter-class>
		<init-param>
				<param-name>ignore_uri</param-name>
				<param-value>/verification.do,/getMessages.do,/logout.do,
				/sys/board/asynList.do,/sys/setting/switchControl.do,/sys/setting/isOff.do,
				/sso/businessDemand.do,/sso/businessDemands.do,/sso/login.do,/login.do,
				/dist/**,/ws/**,/images/**,/html/**,/themes/**,/lib/**,/css/**,/template/**,
				/**/**.js,/**/**.jsp,/**/**.html,/**/**.ico,/script/**
				</param-value>
		</init-param>
</filter>
<filter-mapping>
		<filter-name>AuthFilter</filter-name>
		<url-pattern>/*</url-pattern>
</filter-mapping>
还有springSecurityFilterChain 过滤器

<!-- 404页面不存在错误 -->
<error-page>
		<error-code>404</error-code>
		<location>/common/404.jsp<location>
</error-page>

<!--500服务器内部错误-->
<error-page>
		<error-code>500</error-code>
		<location>/common/500.jsp<location>
</error-page>
这个我完全没用过,jsp估计快消亡了吧
<jsp-config>
		<taglib>
				<taglib-uri>http://cnf.xxxxx.com</taglib-uri>
				<taglib-location>/WEB-INF/classes/taglib/cfn.tld</taglib-location>
		</taglib>
</jsp-config>

欢迎列表?
<welcome-file-list>
	<welcome-file>/login.do</welcome-file>
</welome-file-list>

配置session超时时间为7days(单位为 分钟)
<session-config>
	<session-timeout>86400</session-timeout>
			<cookie-config>
				<name>JSESSIONID</name>
				<path>/</path>
				<http-only>false</http-only>
			</cookie-config>
</session-config>

猜你喜欢

转载自blog.csdn.net/little_dream2018/article/details/88671454