Spring中配置Quartz定时任务

Quartz是一个强大的企业级任务调度框架,Spring中继承并简化了Quartz,下面是在Spring中配置Quartz:

<!-- ***************属性配置文件读入 ,多个用逗号隔开*************** -->
<context:property-placeholder location="classpath:conf/ibatis/jdbc.properties , classpath:config.properties" />

<!-- ************************数据源连接配置********************* -->
<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName">
<value>${driver}</value>
</property>
<property name="url">
<value>${url}</value>
</property>
<property name="username">
<value>${username}</value>
</property>
<property name="password">
<value>${password}</value>
</property>
</bean>

<!-- ************************定时任务配置********************* -->
   <!-- 要调用的工作类 -->
   <bean id="quartzJob" class="com.maosheng.util.schedule.ProductSynJob">
   <property name="host" value="${ftp_host}" />
   <property name="port" value="${ftp_port}" />
   <property name="username" value="${ftp_username}" />
   <property name="password" value="${ftp_password}" />
   <property name="remotePath" value="${ftp_remotePath}" />
   <property name="localPath" value="${ftp_localPath}" />
   <property name="dataSource">
    <ref bean="dataSource"/>
   </property>
   </bean>
   <!-- 定义调用对象和调用对象的方法 -->
   <bean id="jobtask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
      <!-- 调用的类 -->
   <property name="targetObject">
    <ref bean="quartzJob"/>
   </property>
   <!-- 调用类中的方法 -->
<property name="targetMethod">
<value>execute</value>
</property>
  </bean>
  <!-- 定义触发时间 -->
  <bean id="doTime" class="org.springframework.scheduling.quartz.CronTriggerBean">
  <property name="jobDetail">
  <ref bean="jobtask"/>
  </property>
  <!-- cron表达式 每天上午14:30触发-->
  <property name="cronExpression">
  <value>0 30 14 ? * *</value>
  </property>
  </bean>
  <!-- 总管理类 如果将lazy-init='false'那么容器启动就会执行调度程序 -->
  <bean id="startQuertz" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref local="doTime"/>
</list>
</property>
  </bean>
  <!--********************************************************************-->


<!--***************************config.properties*******************-->
#FTP config parameter
ftp_host=110.18.14.250
ftp_port=21
ftp_username=test
ftp_password=test
ftp_remotePath=D:\download
ftp_localPath=D:\oracle\myfile

<!--**************************jdbc.properties******************** -->
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@110.18.14.250:1521:orcl
username=test
password=testpwd


package com.maosheng.util.schedule;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import oracle.jdbc.OracleTypes;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
*
* @author maosheng
*
*/

public class ProductSynJob{

final static Logger logger = LoggerFactory
.getLogger(ProductSynJob.class);

//FTP服务器hostname
private String host;
//FTP服务器端口
private int port;
//FTP登录账号
private String username;
//FTP登录密码
private String password;
    //FTP服务器上的相对路径
private String remotePath;
//下载后保存到本地的路径
private String localPath;

private DataSource dataSource;

/**
*
* 创建人     :maosheng
* 创建时间:2013-5-7
* 功能描述:同步处理程序
*/
public void execute() {


        //下载同步理财产品文件
List<String> files_path = FTPUtil.downFile(host, port, username,
password, remotePath, localPath);

if (files_path != null && files_path.size() == 1) {

String file_name=files_path.get(0);

logger.info("下载同步产品文件"+file_name);

//解析同步产品文件并入库
Connection connection = null;
CallableStatement callableStatement = null;
int result=0;
try {

connection = dataSource.getConnection();

callableStatement=connection.prepareCall("{ call PR_FINANCE_PRODUCT_PARSE(?,?) }");

callableStatement.setString(1,file_name);

callableStatement.registerOutParameter(2, OracleTypes.INTEGER);

callableStatement.execute();

result = callableStatement.getInt(2);

if(result==1){
logger.info("解析同步产品文件,成功完成");
}else
{
logger.info("解析同步产品文件,完成失败");
}

} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if(callableStatement!=null){
callableStatement.close();
}
if(connection!=null){
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}



public String getHost() {
return host;
}


public void setHost(String host) {
this.host = host;
}


public int getPort() {
return port;
}

public void setPort(int port) {
this.port = port;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getRemotePath() {
return remotePath;
}

public void setRemotePath(String remotePath) {
this.remotePath = remotePath;
}

public String getLocalPath() {
return localPath;
}

public void setLocalPath(String localPath) {
this.localPath = localPath;
}

public DataSource getDataSource() {
return dataSource;
}

public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}

}

猜你喜欢

转载自maosheng.iteye.com/blog/1866243