Job管理器使用

Job管理器是基于数据库管理Quartz Job任务,更方便管理Job的生命周期

项目下载 URL:http://pan.baidu.com/s/1ntAH4Zf   密码:j8za

Job管理器主要特点:

  • 配置applicationContext.xml

  • 配置jdbcSource.xml连接池

  • 配置jdbc.properties属性

  • 配置job.xml

  • 配置job.properties

  • 启动任务扫描器

  • 加载job类

配置applicationContext.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        classpath:spring-beans.xsd">
	<!-- 加载作业调度 -->
	<import resource="job.xml" />
	<!-- 加载初始化参数 -->
	<import resource="jdbcSource.xml" />
</beans> 

配置jdbcSource.xml连接池

<?xml version="1.0" encoding="GBK"?>
<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
        classpath:spring-beans.xsd">

	<!-- 加载属性文件 -->
	<bean id="configBean"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>
					file:${JDBC_CONFIG}/jdbc/jdbc.properties
				</value>
			</list>
		</property>
	</bean>
	
	<!--配置数据源 -->
	<bean id="saveOrace" class="com.venustech.dao.PooledDataSource">
		<!-- 指定连接数据库的URL -->
		<property name="url" value="${zjaudit.url}" />
		<!-- 指定连接数据库的驱动 -->
		<property name="driverClassName" value="${zjaudit.driverClassName}" />
		<!-- 指定连接数据库的用户名 -->
		<property name="username" value="${zjaudit.username}" />
		<!-- 指定连接数据库的密码 -->
		<property name="password" value="${zjaudit.password}" />
		<!-- 指定连接数据库的最大线程 -->
		<property name="maxActive" value="${zjaudit.maxActive}" />
		<!-- 指定连接数据库的初始化值 -->
		<property name="initialSize" value="${zjaudit.initialSize}" />
		<!-- 最大空闲数 -->
		<property name="maxIdle" value="${zjaudit.maxIdle}" />
		<!-- 指定连接数据库的最大等待时间 -->
		<property name="maxWait" value="${zjaudit.maxWait}" />
		<!-- 指定连接数据库的回收超时连接的时长 -->
		<property name="removeAbandonedTimeout" value="${zjaudit.removeAbandonedTimeout}" />
		<!--检查空闲连接的时间间隔 -->
		<property name="timeBetweenEvictionRunsMillis" value="${zjaudit.timeBetweenEvictionRunsMillis}" />
		<!--逐出空闲时间 毫秒 -->
		<property name="minEvictableIdleTimeMillis" value="${zjaudit.minEvictableIdleTimeMillis}" />
	</bean>

	<!--分页组件 -->
	<bean id="pageModel" class="com.venustech.data.PageModel">
		<property name="pageSize" value="10" />
		<property name="pageIndex" value="1" />
		<property name="pageSizeName" value="pageSize" />
		<property name="pageIndexName" value="currentPage" />
		<property name="pageCountName" value="totalRecords" />
		<property name="pageTotalPage" value="totalPage" />
	</bean>

	<bean id="sqlSessionFactory" class="com.venustech.sql.SqlSessionFactory">
		<property name="dataSource" ref="saveOrace" />
		<property name="type" value="5" />
		<property name="pageModel" ref="pageModel" />
	</bean>
</beans>

jdbc.properties属性


#save oracele
zjaudit.url=jdbc:mysql://192.168.0.200/test?characterEncoding=utf8
zjaudit.driverClassName=com.mysql.jdbc.Driver
zjaudit.username=root
zjaudit.password=admin
zjaudit.maxActive=15
zjaudit.initialSize=5
zjaudit.maxIdle=5
zjaudit.maxWait=172800000 
zjaudit.removeAbandonedTimeout=600
zjaudit.timeBetweenEvictionRunsMillis=600000
zjaudit.minEvictableIdleTimeMillis=900000

配置job.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        classpath:spring-beans.xsd">

	<!--自动扫描JOB -->
	<bean id="quartzManageFactory" class="com.venustech.quartz.QuartzManageImpl">
		<property name="jobPath">
			<value>${QUARTZ_HOME}job.properties</value>
		</property>
	</bean> 

	<!-- 不要改变 调度 loadingServiceImpl -->
	<bean id="loadingServiceImpl" class="com.venustech.quartz.service.impl.LoadingServiceImpl">
		<property name="queryTask">
			<value>
				select * from TASK_JOB_CONFIG_COLLECTOR where
				JOB_STATUS =0 and
				JOB_IP = :JOB_IP
			</value>
		</property>
		<property name="indexPrefix" value="=[" />
		<property name="indexSuffix" value="]" />
		<property name="quartzManageFactory" ref="quartzManageFactory" />
		<property name="sqlSessionFactory" ref="sqlSessionFactory" />
		<property name="jobTask" value="com.job.Startjob" />
	</bean>
</beans>

配置job.properties

#JOB 调度信息
org.quartz.scheduler.instanceName = DefaultQuartzScheduler   
org.quartz.scheduler.rmi.export = false  
org.quartz.scheduler.rmi.proxy = false  
org.quartz.scheduler.wrapJobExecutionInUserTransaction = false 
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool 
#最大创建50个JOB任务同时执行
org.quartz.threadPool.threadCount = 50 
#线程优先级
org.quartz.threadPool.threadPriority = 5 
org.quartz.jobStore.misfireThreshold = 60000   
#基于内存的调度
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore


设置环境变量

export QUARTZ_HOME=\javaEE\javaSrc\quartzJob\config

启动扫描器

package com.job;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.venustech.cache.bean.BeanFactory;
import com.venustech.quartz.service.LoadingService;
import com.venustech.quartz.service.impl.LoadingServiceImpl;

public class Startjob implements Job {
	Logger logger = LoggerFactory.getLogger(this.getClass());

	public final static String PATH_HOME = System.getenv().get("QUARTZ_HOME")
			+ "/";
	public final static String PATH_APP = PATH_HOME + "applicationContext.xml";

	public final static String JOB_BEAN = "loadingServiceImpl";

	private static LoadingService loadingService;

	public static LoadingService getLoadingService() {
		if (loadingService == null) {
			return (LoadingServiceImpl) BeanFactory.getBean(PATH_APP, JOB_BEAN);
		}
		return loadingService;

	}

	public static void main(String[] args) {
		try {
			Startjob.getLoadingService().init();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void execute(JobExecutionContext context)
			throws JobExecutionException {
		LoadingService loadingService = getLoadingService();
		try {
			loadingService.getUpdateJob(loadingService.getQuartzManage());
		} catch (Exception e) {
			logger.error("exec jobTask exception:" + e);
		}
	}

} 

加入DB管理器

/*
SQLyog Ultimate v11.24 (32 bit)
MySQL - 5.1.73 
*********************************************************************
*/
/*!40101 SET NAMES utf8 */;

create table `TASK_JOB_CONFIG_COLLECTOR` (
	`JOB_ID` varchar (765),
	`JOB_GROUP` varchar (765),
	`JOB_TIME` varchar (765),
	`JOB_CLASS` varchar (765),
	`JOB_IP` varchar (765),
	`JOB_STATUS` int (11),
	`JOB_PARAMETERS` varchar (765),
	`JOB_START` varchar (765),
	`JOB_END` varchar (765)
); 
insert into `TASK_JOB_CONFIG_COLLECTOR` (`JOB_ID`, `JOB_GROUP`, `JOB_TIME`, `JOB_CLASS`, `JOB_IP`, `JOB_STATUS`, `JOB_PARAMETERS`, `JOB_START`, `JOB_END`) values('47','JOB_TASK','0/5 * * * * ?','test.B','192.168.174.1','0','id=[111111],type=[111]','2014-07-30 18:20:00','2020-01-01 00:00:00');
insert into `TASK_JOB_CONFIG_COLLECTOR` (`JOB_ID`, `JOB_GROUP`, `JOB_TIME`, `JOB_CLASS`, `JOB_IP`, `JOB_STATUS`, `JOB_PARAMETERS`, `JOB_START`, `JOB_END`) values('11','JOB_TASK','0/5 * * * * ?','test.Startjob','192.168.174.1','0','','2014-06-30 18:20:00','2020-01-01 00:00:00');
insert into `TASK_JOB_CONFIG_COLLECTOR` (`JOB_ID`, `JOB_GROUP`, `JOB_TIME`, `JOB_CLASS`, `JOB_IP`, `JOB_STATUS`, `JOB_PARAMETERS`, `JOB_START`, `JOB_END`) values('112','JOB_TASK','0/5 * * * * ?','test.C','192.168.174.1','0','id=[22222],type=[333332]','2015-07-01 00:00:00','2020-01-01 00:00:00');



 加入Job调度类

package com.job;

import java.util.Iterator;
import java.util.Map;

import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.venustech.data.DataMap;
import com.venustech.data.IData;

public class A implements Job {


	@Override
	public void execute(JobExecutionContext context)
			throws JobExecutionException {
		JobDataMap map = context.getJobDetail().getJobDataMap();
		@SuppressWarnings("unchecked")
		Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
		IData<String, Object> jobParameters = new DataMap<String, Object>();
		// 将参数存储到data中
		while (it.hasNext()) {
			Map.Entry<String, String> entry = it.next();
			jobParameters.put(entry.getKey(), entry.getValue());
		}
		System.out.println(jobParameters);
	}
} 




你可能感兴趣的:(Job管理器使用)