- driver=com.mysql.jdbc.Driver
- url=jdbc:mysql:
- user=xxxxxxxxxxx
- password=xxxxxxxxxx
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://xxx.xxx.xx.xx:3306/xxx
user=xxxxxxxxxxx
password=xxxxxxxxxx
源数据库配置文件
- driver=com.mysql.jdbc.Driver
- url=jdbc:mysql:
- user=root
- password=root
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/customer
user=root
password=root
目标数据库配置文件
接下来,要做一个数据库连接通用工厂,用来获得连接和在操作结束后释放连接,代码如下
- package com.bj58.job.utils;
-
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.Properties;
-
-
-
-
-
-
-
-
- public final class ConnectionFactory_DB_src {
- private static String url;
- private static String driver;
- private static String user;
- private static String password;
-
- private ConnectionFactory_DB_src() {
- }
-
- static {
- try {
- Properties props = new Properties();
-
- props.load(ConnectionFactory_DB_src.class.getClassLoader()
- .getResourceAsStream("db_src.properties"));
-
- driver = props.getProperty("driver").trim();
- url = props.getProperty("url").trim();
- user = props.getProperty("user").trim();
- password = props.getProperty("password").trim();
- Class.forName(driver);
-
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- }
-
-
-
-
-
- public static Connection getConnection() {
- try {
- Connection conn = DriverManager.getConnection(url, user, password);
- return conn;
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
-
-
-
-
-
-
-
- public static void free(ResultSet rs, PreparedStatement pstmt, Connection conn) {
- try {
- if (rs != null) {
- rs.close();
- }
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- try {
- if (pstmt != null) {
- pstmt.close();
- }
- } catch (SQLException e) {
- e.printStackTrace();
- } finally {
- try {
- if (conn != null) {
- conn.close();
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
package com.bj58.job.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
/**
* 通用数据库连接工厂
* @author [email protected]
* Jun 8, 2010
*
*/
public final class ConnectionFactory_DB_src {
private static String url;
private static String driver;
private static String user;
private static String password;
private ConnectionFactory_DB_src() {
}
static {
try {
Properties props = new Properties();
props.load(ConnectionFactory_DB_src.class.getClassLoader()
.getResourceAsStream("db_src.properties")); // 得到当前类的类加载器,以流的方式读取配置文件
driver = props.getProperty("driver").trim();
url = props.getProperty("url").trim();
user = props.getProperty("user").trim();
password = props.getProperty("password").trim();
Class.forName(driver); // 加载驱动程序
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取Connection连接
* @return
*/
public static Connection getConnection() {
try {
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 释放连接资源
* @param rs
* @param pstmt
* @param conn
*/
public static void free(ResultSet rs, PreparedStatement pstmt, Connection conn) {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
这个是用来获取源数据库连接的,目标的和这个是一样的,这里就不再赘述了。
接下来就要做QuartzJob定时任务类了,这个类要实现的是Job接口,然后重写execute方法,方法中就是执行你具体要做的事情了。不过首先需要一个配置文件,里面定义了一些参数,是Quartz的一些配置。配置文件如下
- #============================================================================
- # Configure Main Scheduler Properties
- #============================================================================
- org.quartz.scheduler.instanceName = QuartzScheduler
- org.quartz.scheduler.instanceId = AUTO
-
- #============================================================================
- # Configure ThreadPool
- #============================================================================
- org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
- org.quartz.threadPool.threadCount = 3
- org.quartz.threadPool.threadPriority = 5
-
- #===============================================================
- #Configure JobStore
- #===============================================================
- org.quartz.jobStore.misfireThreshold = 60000
- org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
-
- #============================================================================
- # Configure Plugins
- #============================================================================
- #org.quartz.plugin.triggHistory.class = org.quartz.plugins.history.LoggingJobHistoryPlugin
- #org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.JobInitializationPlugin
- #org.quartz.plugin.jobInitializer.fileName = conf/quartz_jobs.xml
- #org.quartz.plugin.jobInitializer.overWriteExistingJobs = true
- #org.quartz.plugin.jobInitializer.failOnFileNotFound = true
- #org.quartz.plugin.jobInitializer.scanInterval = 5
#============================================================================
# Configure Main Scheduler Properties
#============================================================================
org.quartz.scheduler.instanceName = QuartzScheduler
org.quartz.scheduler.instanceId = AUTO
#============================================================================
# Configure ThreadPool
#============================================================================
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 3
org.quartz.threadPool.threadPriority = 5
#===============================================================
#Configure JobStore
#===============================================================
org.quartz.jobStore.misfireThreshold = 60000
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
#============================================================================
# Configure Plugins
#============================================================================
#org.quartz.plugin.triggHistory.class = org.quartz.plugins.history.LoggingJobHistoryPlugin
#org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.JobInitializationPlugin
#org.quartz.plugin.jobInitializer.fileName = conf/quartz_jobs.xml
#org.quartz.plugin.jobInitializer.overWriteExistingJobs = true
#org.quartz.plugin.jobInitializer.failOnFileNotFound = true
#org.quartz.plugin.jobInitializer.scanInterval = 5
好了,这就是配置文件,接下来我们就要做这个计划类了,这个类执行的是去源数据库中查询数据,并插入目标数据库中,并且不允许有重复。代码如下
- package com.bj58.job.servlet;
-
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
-
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.quartz.Job;
- import org.quartz.JobExecutionContext;
- import org.quartz.JobExecutionException;
-
- import com.bj58.job.utils.ConnectionFactory_DB_dest;
- import com.bj58.job.utils.ConnectionFactory_DB_src;
-
-
-
-
-
-
-
- public class QuartzJob implements Job {
- private static final Log log = LogFactory.getLog(Job.class);
- private static final String findSql_src = "select PortalId from t_portalinfo";
- private static final String findSql_dest = "select userId from t_user";
- private static final String insertSql_dest = "insert into t_user(userId) values (?)";
-
-
-
-
- @Override
- public void execute(JobExecutionContext context) throws JobExecutionException {
- Connection conn_db_src = ConnectionFactory_DB_src.getConnection();
- Connection conn_db_dest = ConnectionFactory_DB_dest.getConnection();
- PreparedStatement pstmt_src = null;
- PreparedStatement pstmt_dest = null;
- ResultSet rs_src = null;
- ResultSet rs_dest = null;
- try {
- pstmt_src = conn_db_src.prepareStatement(findSql_src);
- rs_src = pstmt_src.executeQuery();
-
- pstmt_dest = conn_db_dest.prepareStatement(findSql_dest);
- rs_dest = pstmt_dest.executeQuery();
- pstmt_dest = conn_db_dest.prepareStatement(insertSql_dest);
-
- while(rs_dest.next()) {
- String userId = rs_dest.getString(1);
- while (rs_src.next()) {
- String protalId = rs_src.getString(1);
- if(userId.equals(protalId)) {
- break;
- } else {
- pstmt_dest.setString(1, protalId);
- pstmt_dest.execute();
- }
- }
- }
-
- while (rs_src.next()) {
- String protalId = rs_src.getString(1);
- pstmt_dest.setString(1, protalId);
- pstmt_dest.execute();
-
- }
- log.info("存储数据...");
- } catch (SQLException e) {
- log.info("存储数据出现异常...");
- e.printStackTrace();
- } finally {
- ConnectionFactory_DB_dest.free(rs_dest, pstmt_dest, conn_db_dest);
- ConnectionFactory_DB_src.free(rs_src, pstmt_src, conn_db_src);
- log.info("数据库连接已经关闭...");
- }
-
- }
- }
package com.bj58.job.servlet;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import com.bj58.job.utils.ConnectionFactory_DB_dest;
import com.bj58.job.utils.ConnectionFactory_DB_src;
/**
* 定时任务类
* @author [email protected]
* Jun 8, 2010
*
*/
public class QuartzJob implements Job {
private static final Log log = LogFactory.getLog(Job.class);
private static final String findSql_src = "select PortalId from t_portalinfo";
private static final String findSql_dest = "select userId from t_user";
private static final String insertSql_dest = "insert into t_user(userId) values (?)";
/**
* 执行任务方法
*/
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
Connection conn_db_src = ConnectionFactory_DB_src.getConnection();
Connection conn_db_dest = ConnectionFactory_DB_dest.getConnection();
PreparedStatement pstmt_src = null;
PreparedStatement pstmt_dest = null;
ResultSet rs_src = null;
ResultSet rs_dest = null;
try {
pstmt_src = conn_db_src.prepareStatement(findSql_src);
rs_src = pstmt_src.executeQuery();
pstmt_dest = conn_db_dest.prepareStatement(findSql_dest);
rs_dest = pstmt_dest.executeQuery();
pstmt_dest = conn_db_dest.prepareStatement(insertSql_dest);
while(rs_dest.next()) { //第二次或第n次入库,第一次入库的话rs_dest没有结果,所以此段操作都不执行
String userId = rs_dest.getString(1);
while (rs_src.next()) {
String protalId = rs_src.getString(1);
if(userId.equals(protalId)) { //如果两个结果相同,则不进行入库
break;
} else {
pstmt_dest.setString(1, protalId);
pstmt_dest.execute();
}
}
}
while (rs_src.next()) { //如果是第一次入库
String protalId = rs_src.getString(1);
pstmt_dest.setString(1, protalId);
pstmt_dest.execute();
}
log.info("存储数据...");
} catch (SQLException e) {
log.info("存储数据出现异常...");
e.printStackTrace();
} finally {
ConnectionFactory_DB_dest.free(rs_dest, pstmt_dest, conn_db_dest);
ConnectionFactory_DB_src.free(rs_src, pstmt_src, conn_db_src);
log.info("数据库连接已经关闭...");
}
}
}
这里一次性查出了源数据库中所有字段,因为源数据库表中的数据最多不会超过10000条,我在做测试的时候,在本机上发现一次查询10W条也不会出现结果集溢出情况,所以这里就忽略这个问题了。
最后我们就要做调度这个任务的类了,这里我做了个servlet,将调度代码写在init方法里,部署在服务器下,然后重新启动服务器,这样的话,任务就加载了,等到定时的时候就可以自动执行了。这个任务调度类的代码如下
- package com.bj58.job.servlet;
-
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
-
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.quartz.CronTrigger;
- import org.quartz.JobDetail;
- import org.quartz.Scheduler;
- import org.quartz.SchedulerFactory;
- import org.quartz.impl.StdSchedulerFactory;
-
-
-
-
-
-
-
- public class QuartzJobServlet extends HttpServlet {
- private static final long serialVersionUID = 1L;
- private static final Log log = LogFactory.getLog(QuartzJobServlet.class);
-
-
-
-
- @Override
- public void init() throws ServletException {
- log.info("任务初始化...");
- try {
- JobDetail jobDetail = new JobDetail("Income Report","Report Generation", QuartzJob.class);
- SchedulerFactory sf = new StdSchedulerFactory();
- Scheduler sched;
- sched = sf.getScheduler();
- sched.start();
- CronTrigger trigger = new CronTrigger("Income Report", "Report Generation");
- trigger.setCronExpression("0 0 1 * * ?");
- sched.scheduleJob(jobDetail, trigger);
- log.info("任务加载成功...");
- } catch (Exception e) {
- log.info("任务加载失败...");
- }
- }
- }