|
@@ -1,8 +1,5 @@
|
|
package com.xt.dsp.job;
|
|
package com.xt.dsp.job;
|
|
|
|
|
|
-import java.io.File;
|
|
|
|
-import java.io.FileWriter;
|
|
|
|
-import java.io.IOException;
|
|
|
|
import java.sql.Connection;
|
|
import java.sql.Connection;
|
|
import java.sql.DatabaseMetaData;
|
|
import java.sql.DatabaseMetaData;
|
|
import java.sql.ResultSet;
|
|
import java.sql.ResultSet;
|
|
@@ -22,6 +19,7 @@ import org.quartz.SchedulerContext;
|
|
import org.quartz.SchedulerException;
|
|
import org.quartz.SchedulerException;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.context.ApplicationContext;
|
|
import org.springframework.context.ApplicationContext;
|
|
import org.springframework.dao.DataAccessException;
|
|
import org.springframework.dao.DataAccessException;
|
|
import org.springframework.jdbc.core.ConnectionCallback;
|
|
import org.springframework.jdbc.core.ConnectionCallback;
|
|
@@ -33,12 +31,14 @@ import org.springframework.util.StringUtils;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
import com.alibaba.fastjson.JSONArray;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
+import com.xt.dsp.bean.TaskCacheBean;
|
|
import com.xt.dsp.jdbc.JDBCTools;
|
|
import com.xt.dsp.jdbc.JDBCTools;
|
|
import com.xt.dsp.jdbc.JdbcTemplateUtils;
|
|
import com.xt.dsp.jdbc.JdbcTemplateUtils;
|
|
import com.xt.dsp.model.DataSourceBean;
|
|
import com.xt.dsp.model.DataSourceBean;
|
|
import com.xt.dsp.model.JobBean;
|
|
import com.xt.dsp.model.JobBean;
|
|
import com.xt.dsp.model.TaskBean;
|
|
import com.xt.dsp.model.TaskBean;
|
|
import com.xt.dsp.model.TaskSqlBean;
|
|
import com.xt.dsp.model.TaskSqlBean;
|
|
|
|
+import com.xt.dsp.service.CacheBeanService;
|
|
import com.xt.dsp.service.DataSourceService;
|
|
import com.xt.dsp.service.DataSourceService;
|
|
import com.xt.dsp.service.JobService;
|
|
import com.xt.dsp.service.JobService;
|
|
import com.xt.dsp.service.TaskService;
|
|
import com.xt.dsp.service.TaskService;
|
|
@@ -49,10 +49,12 @@ public class SqlSynJob implements Job {
|
|
private TaskService taskService;
|
|
private TaskService taskService;
|
|
private TaskSqlService taskSqlService;
|
|
private TaskSqlService taskSqlService;
|
|
private DataSourceService dataSourceService;
|
|
private DataSourceService dataSourceService;
|
|
|
|
+ private JobBean job = null;
|
|
|
|
+ private CacheBeanService cacheBeanService;
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public void execute(JobExecutionContext context) throws JobExecutionException {
|
|
public void execute(JobExecutionContext context) throws JobExecutionException {
|
|
- JobBean job = (JobBean) context.getJobDetail().getJobDataMap().get(JobService.PARAM_JOB);
|
|
|
|
|
|
+ job = (JobBean) context.getJobDetail().getJobDataMap().get(JobService.PARAM_JOB);
|
|
if (null == job) {
|
|
if (null == job) {
|
|
throw new IllegalArgumentException("未找到执行任务信息!");
|
|
throw new IllegalArgumentException("未找到执行任务信息!");
|
|
}
|
|
}
|
|
@@ -70,14 +72,12 @@ public class SqlSynJob implements Job {
|
|
this.taskService = appCtx.getBean(TaskService.class);
|
|
this.taskService = appCtx.getBean(TaskService.class);
|
|
this.taskSqlService = appCtx.getBean(TaskSqlService.class);
|
|
this.taskSqlService = appCtx.getBean(TaskSqlService.class);
|
|
this.dataSourceService = appCtx.getBean(DataSourceService.class);
|
|
this.dataSourceService = appCtx.getBean(DataSourceService.class);
|
|
|
|
+ this.cacheBeanService = appCtx.getBean(CacheBeanService.class);
|
|
List<TaskBean> tasks = taskService.selectByJobCode(job.getCode());
|
|
List<TaskBean> tasks = taskService.selectByJobCode(job.getCode());
|
|
for (TaskBean t : tasks) {
|
|
for (TaskBean t : tasks) {
|
|
// SQL 同步任务
|
|
// SQL 同步任务
|
|
if (TaskBean.TYPE_SQL.equals(t.getType())) {
|
|
if (TaskBean.TYPE_SQL.equals(t.getType())) {
|
|
- List<TaskSqlBean> taskSqlBeans = taskSqlService.selectByTaskId(t.getId());
|
|
|
|
- if (taskSqlBeans != null && taskSqlBeans.size() > 0) {
|
|
|
|
- doTaskSqlBeans(taskSqlBeans);
|
|
|
|
- }
|
|
|
|
|
|
+ doTaskSqlBeans(t);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} catch (SchedulerException e1) {
|
|
} catch (SchedulerException e1) {
|
|
@@ -90,35 +90,21 @@ public class SqlSynJob implements Job {
|
|
*
|
|
*
|
|
* @param taskSqlBeans
|
|
* @param taskSqlBeans
|
|
*/
|
|
*/
|
|
- private void doTaskSqlBeans(List<TaskSqlBean> taskSqlBeans) {
|
|
|
|
|
|
+ private void doTaskSqlBeans(TaskBean t) {
|
|
|
|
+ List<TaskSqlBean> taskSqlBeans = taskSqlService.selectByTaskId(t.getId());
|
|
|
|
+ if (taskSqlBeans == null || taskSqlBeans.size() == 0) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
for (TaskSqlBean ts : taskSqlBeans) {
|
|
for (TaskSqlBean ts : taskSqlBeans) {
|
|
JSONObject result = getSqlResultJsonObject(ts);
|
|
JSONObject result = getSqlResultJsonObject(ts);
|
|
int rest = runTaskSqlWithJdbcTemplate(ts, result);
|
|
int rest = runTaskSqlWithJdbcTemplate(ts, result);
|
|
if (rest != 0) {
|
|
if (rest != 0) {
|
|
- String fileName = "SqlSynJob" + System.currentTimeMillis();
|
|
|
|
- File f = new File(fileName);
|
|
|
|
- int count = 0;
|
|
|
|
- while (f.exists()) {
|
|
|
|
- f = new File(fileName + "-" + (++count));
|
|
|
|
- }
|
|
|
|
- if (count > 0) {
|
|
|
|
- f = new File(fileName + "-" + count);
|
|
|
|
- }
|
|
|
|
- FileWriter fw = null;
|
|
|
|
- try {
|
|
|
|
- fw = new FileWriter(f);
|
|
|
|
- fw.write(result.toJSONString());
|
|
|
|
- } catch (IOException e) {
|
|
|
|
- e.printStackTrace();
|
|
|
|
- } finally {
|
|
|
|
- if (null != fw) {
|
|
|
|
- try {
|
|
|
|
- fw.close();
|
|
|
|
- } catch (IOException e) {
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- result.toJSONString();
|
|
|
|
|
|
+ String fileName = ts.getId() + "-" + System.currentTimeMillis();
|
|
|
|
+ TaskCacheBean cacheBean = new TaskCacheBean();
|
|
|
|
+ cacheBean.setAvalidCacheFolder(t.getCacheFolder());
|
|
|
|
+ cacheBean.setCacheData(result);
|
|
|
|
+ cacheBean.setFileName(fileName);
|
|
|
|
+ cacheBeanService.writeCache(cacheBean);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|