|
@@ -8,6 +8,7 @@ import java.sql.SQLException;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.Collection;
|
|
|
+import java.util.Collections;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
@@ -19,7 +20,6 @@ import org.quartz.SchedulerContext;
|
|
|
import org.quartz.SchedulerException;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.context.ApplicationContext;
|
|
|
import org.springframework.dao.DataAccessException;
|
|
|
import org.springframework.jdbc.core.ConnectionCallback;
|
|
@@ -32,7 +32,6 @@ import org.springframework.util.StringUtils;
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.xt.dsp.bean.TaskCacheBean;
|
|
|
-import com.xt.dsp.jdbc.JDBCTools;
|
|
|
import com.xt.dsp.jdbc.JdbcTemplateUtils;
|
|
|
import com.xt.dsp.model.DataSourceBean;
|
|
|
import com.xt.dsp.model.JobBean;
|
|
@@ -44,16 +43,29 @@ import com.xt.dsp.service.JobService;
|
|
|
import com.xt.dsp.service.TaskService;
|
|
|
import com.xt.dsp.service.TaskSqlService;
|
|
|
|
|
|
+/**
|
|
|
+ * 工作调度控制
|
|
|
+ *
|
|
|
+ * @author yuanxd
|
|
|
+ *
|
|
|
+ */
|
|
|
public class SqlSynJob implements Job {
|
|
|
+ /** 日志记录 */
|
|
|
private final Logger log = LoggerFactory.getLogger(SqlSynJob.class);
|
|
|
+ /** 任务服务接口 */
|
|
|
private TaskService taskService;
|
|
|
+ /** sql抽取任务服务接口 */
|
|
|
private TaskSqlService taskSqlService;
|
|
|
+ /** 数据源服务接口 */
|
|
|
private DataSourceService dataSourceService;
|
|
|
+ /** 当前执行的任务对象 */
|
|
|
private JobBean job = null;
|
|
|
+ /** 任务缓存服务 */
|
|
|
private CacheBeanService cacheBeanService;
|
|
|
|
|
|
@Override
|
|
|
public void execute(JobExecutionContext context) throws JobExecutionException {
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
job = (JobBean) context.getJobDetail().getJobDataMap().get(JobService.PARAM_JOB);
|
|
|
if (null == job) {
|
|
|
throw new IllegalArgumentException("未找到执行任务信息!");
|
|
@@ -61,14 +73,9 @@ public class SqlSynJob implements Job {
|
|
|
if (StringUtils.isEmpty(job.getCode())) {
|
|
|
throw new IllegalArgumentException("job code不可为空!");
|
|
|
}
|
|
|
- // 获取JobExecutionContext中的service对象
|
|
|
try {
|
|
|
- // 获取JobExecutionContext中的service对象
|
|
|
SchedulerContext schCtx = context.getScheduler().getContext();
|
|
|
- // 获取Spring中的上下文
|
|
|
ApplicationContext appCtx = (ApplicationContext) schCtx.get("applicationContext");
|
|
|
- // User u = securityMgr.userService().findByUname("admin");
|
|
|
- // System.err.println(u.getId());
|
|
|
this.taskService = appCtx.getBean(TaskService.class);
|
|
|
this.taskSqlService = appCtx.getBean(TaskSqlService.class);
|
|
|
this.dataSourceService = appCtx.getBean(DataSourceService.class);
|
|
@@ -82,33 +89,97 @@ public class SqlSynJob implements Job {
|
|
|
}
|
|
|
} catch (SchedulerException e1) {
|
|
|
e1.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ long cost = System.currentTimeMillis() - start;
|
|
|
+ log.debug("任务执行完成,耗时:{}", cost);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * sql同步任务处理
|
|
|
+ * SQL同步任务处理
|
|
|
*
|
|
|
* @param taskSqlBeans
|
|
|
*/
|
|
|
private void doTaskSqlBeans(TaskBean t) {
|
|
|
+ // 执行缓存
|
|
|
+ executeCache(t);
|
|
|
List<TaskSqlBean> taskSqlBeans = taskSqlService.selectByTaskId(t.getId());
|
|
|
if (taskSqlBeans == null || taskSqlBeans.size() == 0) {
|
|
|
return;
|
|
|
}
|
|
|
for (TaskSqlBean ts : taskSqlBeans) {
|
|
|
JSONObject result = getSqlResultJsonObject(ts);
|
|
|
- int rest = runTaskSqlWithJdbcTemplate(ts, result);
|
|
|
- if (rest != 0) {
|
|
|
- String fileName = ts.getId() + "-" + System.currentTimeMillis();
|
|
|
- TaskCacheBean cacheBean = new TaskCacheBean();
|
|
|
- cacheBean.setAvalidCacheFolder(t.getCacheFolder());
|
|
|
- cacheBean.setCacheData(result);
|
|
|
- cacheBean.setFileName(fileName);
|
|
|
- cacheBeanService.writeCache(cacheBean);
|
|
|
+ if (null == result || result.getJSONArray("data").size() == 0) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (TaskBean.CACHE_USE.equals(t.getCacheUse())) {
|
|
|
+ saveCache(t, ts, result);
|
|
|
+ } else {
|
|
|
+ int rest = runTaskSqlWithJdbcTemplate(t, ts, result);
|
|
|
+ if (rest != 0) {
|
|
|
+
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 执行缓存,执行成功返回0,否则返回其他
|
|
|
+ *
|
|
|
+ * @param t
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private int executeCache(TaskBean t) {
|
|
|
+ List<TaskCacheBean> taskDatas = cacheBeanService.readCache(t.getCacheFolder());
|
|
|
+ // 无缓存数据
|
|
|
+ if (taskDatas.size() == 0) {
|
|
|
+ t.setCacheUse(TaskBean.CACHE_UNUSE);
|
|
|
+ taskService.updateByPrimaryKey(t);
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ Collections.sort(taskDatas);
|
|
|
+ Map<String, TaskSqlBean> tsbs = new HashMap<>();
|
|
|
+ for (TaskCacheBean tc : taskDatas) {
|
|
|
+ String tsbid = tc.getTaskId();
|
|
|
+ TaskSqlBean bean = null;
|
|
|
+ if (tsbs.containsKey(tsbid)) {
|
|
|
+ bean = tsbs.get(tsbid);
|
|
|
+ } else {
|
|
|
+ bean = taskSqlService.selectByPrimaryKey(tsbid);
|
|
|
+ if (null == bean) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ tsbs.put(tsbid, bean);
|
|
|
+ }
|
|
|
+ if (null == bean) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ runTaskSqlWithJdbcTemplate(t, bean, JSONObject.parseObject((String) tc.getCacheData()));
|
|
|
+ }
|
|
|
+ t.setCacheUse(TaskBean.CACHE_UNUSE);
|
|
|
+ taskService.updateByPrimaryKey(t);
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void saveCache(TaskBean t, TaskSqlBean ts, JSONObject result) {
|
|
|
+ String fileName = ts.getId() + "-" + System.currentTimeMillis();
|
|
|
+ TaskCacheBean cacheBean = new TaskCacheBean();
|
|
|
+ cacheBean.setAvalidCacheFolder(t.getCacheFolder());
|
|
|
+ cacheBean.setCacheData(result);
|
|
|
+ cacheBean.setFileName(fileName);
|
|
|
+ cacheBeanService.writeCache(cacheBean);
|
|
|
+ cacheBeanService.writeCache(cacheBean);
|
|
|
+ cacheBeanService.writeCache(cacheBean);
|
|
|
+ t.setCacheUse(TaskBean.CACHE_USE);
|
|
|
+ taskService.updateByPrimaryKey(t);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取sql检索结果
|
|
|
+ *
|
|
|
+ * @param taskSqlBean
|
|
|
+ * @return
|
|
|
+ */
|
|
|
private JSONObject getSqlResultJsonObject(TaskSqlBean taskSqlBean) {
|
|
|
String dsid = taskSqlBean.getSrcConn();
|
|
|
DataSourceBean dsb = dataSourceService.selectByPrimaryKey(dsid);
|
|
@@ -135,21 +206,34 @@ public class SqlSynJob implements Job {
|
|
|
}
|
|
|
results.add(resultJson);
|
|
|
}
|
|
|
+ if (results.size() == 0) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
result.put("columns", columns.toString());
|
|
|
result.put("data", results);
|
|
|
return result;
|
|
|
}
|
|
|
});
|
|
|
- log.info("result:{}", result.toJSONString());
|
|
|
+ log.debug("result:{}", result);
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
- private int runTaskSqlWithJdbcTemplate(final TaskSqlBean taskSqlBean, JSONObject result) {
|
|
|
+ /**
|
|
|
+ * 结果同步到目标源
|
|
|
+ *
|
|
|
+ * @param taskSqlBean
|
|
|
+ * @param result
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private int runTaskSqlWithJdbcTemplate(final TaskBean t, final TaskSqlBean taskSqlBean, JSONObject result) {
|
|
|
try {
|
|
|
// 此处应在插入或更新失败时回滚,并把查询结果以文件方式保存,留待以后再执行此任务;
|
|
|
String[] srcColumns = result.getString("columns").split(";");
|
|
|
String dsid = taskSqlBean.getTargetConn();
|
|
|
DataSourceBean dsb = dataSourceService.selectByPrimaryKey(dsid);
|
|
|
+ if (null == dsb) {
|
|
|
+ throw new RuntimeException("数据源获取失败:" + dsid);
|
|
|
+ }
|
|
|
JdbcTemplate template = JdbcTemplateUtils.getJdbcTemplate(dsb);
|
|
|
final Collection<String> srcColumnsCollection = Arrays.asList(srcColumns);
|
|
|
Map<String, List<String>> targetTableColumns = template
|
|
@@ -225,11 +309,13 @@ public class SqlSynJob implements Job {
|
|
|
|
|
|
insertSql.append(")VALUES(").append(insertParaSql);
|
|
|
insertSql.append(")");
|
|
|
- System.err.println(insertSql.toString());
|
|
|
- System.err.println(updateSql.toString());
|
|
|
+ log.debug("insertSql:{}", insertSql.toString());
|
|
|
+ log.debug("updateSql:{}", updateSql.toString());
|
|
|
JSONArray data = (JSONArray) result.get("data");
|
|
|
return doCommit(data, insertSql.toString(), updateSql.toString(), tableColumns, template);
|
|
|
} catch (Exception e) {
|
|
|
+ log.error("执行插入失败:{}", e.getMessage());
|
|
|
+ saveCache(t, taskSqlBean, result);
|
|
|
return -1;
|
|
|
}
|
|
|
}
|
|
@@ -253,7 +339,6 @@ public class SqlSynJob implements Job {
|
|
|
param.put(col, jo.get(col));
|
|
|
}
|
|
|
int count = nameJdbcTemplate.update(updateSql, new MapSqlParameterSource(param));
|
|
|
- System.err.println("count:" + count);
|
|
|
if (count == 0) {
|
|
|
nameJdbcTemplate.update(insertSql, param);
|
|
|
}
|
|
@@ -264,40 +349,4 @@ public class SqlSynJob implements Job {
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
|
-
|
|
|
- /**
|
|
|
- * 执行单个同步任务
|
|
|
- *
|
|
|
- * @param taskSqlBean
|
|
|
- */
|
|
|
- private void runTaskSql(TaskSqlBean taskSqlBean) {
|
|
|
- JSONObject srcJson = JSONObject.parseObject(taskSqlBean.getSrcConn());
|
|
|
- JSONObject targetJson = JSONObject.parseObject(taskSqlBean.getTargetConn());
|
|
|
- String sql = taskSqlBean.getSql();
|
|
|
- JDBCTools jdbc = new JDBCTools(srcJson);
|
|
|
- JSONObject result = new JSONObject();
|
|
|
- try {
|
|
|
- ResultSet rs = jdbc.getResultSet(sql);
|
|
|
- ResultSetMetaData rsmd = rs.getMetaData();
|
|
|
- List<String> columns = new ArrayList<>();
|
|
|
- for (int i = 0; i < rsmd.getColumnCount(); i++) {
|
|
|
- columns.add(rsmd.getColumnName(i + 1));
|
|
|
- }
|
|
|
- JSONArray results = new JSONArray();
|
|
|
- while (rs.next()) {
|
|
|
- JSONObject resultJson = new JSONObject();
|
|
|
- for (String column : columns) {
|
|
|
- resultJson.put(column, rs.getObject(column));
|
|
|
- }
|
|
|
- results.add(resultJson);
|
|
|
- }
|
|
|
- result.put("columns", columns);
|
|
|
- result.put("data", results);
|
|
|
- } catch (SQLException e) {
|
|
|
- e.printStackTrace();
|
|
|
- } finally {
|
|
|
- jdbc.releaseDB();
|
|
|
- }
|
|
|
- log.info("result:{}", result.toJSONString());
|
|
|
- }
|
|
|
}
|