Java调度实现

根据自己在项目中用到的调度,简单说说:(如有不正确的地方,请留言。。。)

Java调度:他是用来解决访问时间慢的手段。

通俗的讲就是为需要的数据(你需要展示的数据)建立一张中间表存放,提前把数据读出来插入到中间表中,当需要的时候再从中间表读数据就可以,如何存放数据呢,那么就需要所谓的调度,他可以设置某个时间,比如凌晨几点让服务器执行一段非常耗时的代码。这样就提高了访问速度。

调度实现:1.编写调度类;2.进行配置

调度类 例如如下代码:

 

public class ReportSchedule {
	@Inject
	private xxxMapper  xxxMapper;
	
	@Inject
	private xxxMapper xxxMapper;
	
	//调度执行插入数据
		public void qdwy_loginReport(){
			System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+":调度开始");

    			//想办法拿到自己想插入的数据,其实就是select操作
                       //调用mapper层接口实现向中间表插入数据,其实就是插入表操作insert

			System.out.println("**********************************");
			System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+":调度结束");
			bgTongjibaobiaoMapper.dispatchBg();
			System.out.println("========================调度结束==========================");
		}    

 

 在spring-application中配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

	<!-- ______________________________调度配置开始______________________________ -->
	<!-- 配置调度类 -->
	<bean id="reportSchedule" class="com.ly.filter.ReportSchedule"></bean>
	<!-- 配置MethodInvokingJobDetailFactoryBean -->
	<bean id="jobtask2" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject" ref="reportSchedule"></property>   <!--当有多个调度方法时,需要单独在写一个bean-->
		<property name="targetMethod" value="qdwy_hyloginReport"></property>
	</bean>
	
	<!-- 配置定时表达式 -->
	<bean id="doTime2" class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail" ref="jobtask2"></property>
                <!-- 0 0 15 * * ? *  代表秒 分 小时 日 月 星期 年 -->
		<property name="cronExpression" value="0 0 15 * * ? *"></property>
	</bean>
	
	<!-- 配置调度工厂:如果将lazy-init='false'那么容器启动就会执行调度程序 -->
	<bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>
				<ref bean="doTime2"/>
			</list>
		</property>
	</bean>
	<!-- 调度配置结束 -->	
</beans>    

 关于调度关键实现就是上面代码了,有需要的可以看看。

 

posted @ 2017-07-24 17:04  懒得烧蛇吃  阅读(1742)  评论(0编辑  收藏  举报