spring2.5 dbcp连接数据库所需jar包以及Quartz任务调度

spring2.5 dbcp连接数据库所需jar包:  spring.jar ,commons-pool-xxx.jar ,commons-dbcp-xxx.jar,commons-collections-xxx.jar.   其中xxx为版本号。

 

如果需要加入任务计划Quartz,所需要的jar包为

 

spring+Quartz

 

 

 

配置文件在src平级目录conf下,可以采用FileSystemXmlApplicationContext 来读取文件,如:

 private static ApplicationContext applicationContext = new FileSystemXmlApplicationContext("conf/applicationContext.xml");
 
 private static GateDao dao = (GateDao)applicationContext.getBean("gateDao");

 

 

配置文件在src下,可以采用ClassPathXmlApplicationContext 来载入文件,如:

 

 private static ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
 
 private static GateDao dao = (GateDao)applicationContext.getBean("gateDao");

 

 

任务的配置文件applicationContext-task.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
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">


	<!--任务执行bean-->
	<bean name="taskBean"
		class="com.fsti.newdev.schedule.TaskBean">
		<property name="dao">
			<ref bean="gateDao" />
		</property>
		
		<property name="gateService">
			<ref bean="gateService" />
		</property>
	</bean>

	<!--同步时间调用的方法JobDetail-->
	<bean id="syncTime"
		class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject">
			<ref bean="taskBean" />
		</property>
		<property name="targetMethod">
			<value>syncTime</value>
		</property>
		<property name="concurrent" value="false"/>
	</bean>

<!-- 同步时间的触发器Trigger-->
	<bean id="cronSyncTimeTrigger"
		class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail">
			<ref bean="syncTime" />
		</property>
		<property name="cronExpression">
			<!--<value>0/10 * * * * ?</value>-->
			<value>0 30 0 * * ?</value> <!--每隔一天同步时间-->
		</property>
	</bean>


	<!-- Quartz Scheduler 任务列表-->
	<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>
				
				<!-- --> 
				<ref bean="cronSyncTimeTrigger" /> 
			</list>
		</property>
	</bean>
	

 

执行任务的TaskBean:

 

package com.fsti.newdev.schedule;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.fsti.newdev.dao.GateDao;
import com.fsti.newdev.model.BaseDev;
import com.fsti.newdev.model.ListBean;
import com.fsti.newdev.service.GateService;
import com.fsti.newdev.util.StrUtil;



public class TaskBean  {

	private static final Logger logger = Logger.getLogger(TaskBean.class);
	
	private GateDao dao;
	
	private GateService gateService; 

	/**
	 * 开始同步门禁控制器时间
	 */
	public void syncTime(){
		logger.info("开始同步门禁时间");
		List<BaseDev> gates = new ArrayList<BaseDev>();
		try {
			logger.info("开始查询门禁设备列表");
			gates = dao.getDevices();
			logger.info("查询到的门禁设备个数:" + gates.size());
			
		} catch (SQLException e) {
			logger.error("查询门禁设备列表出现异常",e);
			return;
		}
		
		for(BaseDev gate : gates) {
			logger.info("同步门禁时间:设备编号="+gate.getStationNo()+
					",设备ip="+gate.getIp());
			int i = gateService.setDateTime(gate.getStationNo(),gate.getIp());
			if(i==1) {
				logger.info("同步门禁时间成功:设备编号="+gate.getStationNo());
			} else {
				logger.info("同步门禁时间失败:设备编号="+gate.getStationNo());
			}
		}
		
	}
	
		
	
	public GateDao getDao() {
		return dao;
	}
	public void setDao(GateDao dao) {
		this.dao = dao;
	}
	public GateService getGateService() {
		return gateService;
	}
	public void setGateService(GateService gateService) {
		this.gateService = gateService;
	}


	
}

 

 

 

最后,Main函数直接载入xml配置文件即可自动调用任务:

package com.fsti.newdev;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class Main {

	/**
	 * @param args
	 */
	@SuppressWarnings("unused")
	public static void main(String[] args) {
		ApplicationContext context = new FileSystemXmlApplicationContext("conf/applicationContext-task.xml"); 
		
	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值