Pārlūkot izejas kodu

git-svn-id: https://192.168.57.71/svn/jsgkj@285 931142cf-59ea-a443-aa0e-51397b428577

xt_yuanxd 9 gadi atpakaļ
vecāks
revīzija
b51468c6d3

+ 91 - 0
xtdsp/trunk/src/main/java/com/xt/dsp/bean/TaskCacheBean.java

@@ -0,0 +1,91 @@
+package com.xt.dsp.bean;
+
+import java.io.File;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.util.StringUtils;
+
+/**
+ * 任务缓存实例
+ * 
+ * @author yuanxd
+ *
+ */
+public class TaskCacheBean {
+	private final Logger log = LoggerFactory.getLogger(TaskCacheBean.class);
+	private String cacheFolder;
+	private String subFolder;
+
+	public String getSubFolder() {
+		return subFolder;
+	}
+
+	public void setSubFolder(String subFolder) {
+		this.subFolder = subFolder;
+	}
+
+	private Object cacheData;
+	private String fileName;
+
+	public String getFileName() {
+		return fileName;
+	}
+
+	public void setFileName(String fileName) {
+		this.fileName = fileName;
+	}
+
+	/**
+	 * 设置可用缓存目录
+	 * 
+	 * @param folder
+	 * @return
+	 */
+	public int setAvalidCacheFolder(String folder) {
+		File cachefolder = null;
+		if (StringUtils.hasText(folder)) {
+			cachefolder = new File(folder);
+			if (!cachefolder.exists()) {
+				try {
+					boolean ctFolder = cachefolder.mkdirs();
+					if (ctFolder && cachefolder.canRead() && cachefolder.canWrite()) {
+						this.cacheFolder = folder;
+					}
+				} catch (Exception e) {
+
+				}
+			} else {
+				if (cachefolder.isDirectory() && cachefolder.canWrite() && cachefolder.canRead()) {
+					this.cacheFolder = folder;
+				}
+			}
+		}
+		if (StringUtils.isEmpty(this.cacheFolder)) {
+			File f = new File("");
+			if (f.isDirectory() && f.canRead() && f.canWrite()) {
+				this.cacheFolder = "";
+			} else {
+				log.error("设置缓存目录异常:{}", folder);
+				return -1;
+			}
+		}
+		return 0;
+	}
+
+	public String getCacheFolder() {
+		return cacheFolder;
+	}
+
+	public void setCacheFolder(String cacheFolder) {
+		this.cacheFolder = cacheFolder;
+	}
+
+	public Object getCacheData() {
+		return cacheData;
+	}
+
+	public void setCacheData(Object cacheData) {
+		this.cacheData = cacheData;
+	}
+}

+ 0 - 1
xtdsp/trunk/src/main/java/com/xt/dsp/jdbc/JDBCTools.java

@@ -41,7 +41,6 @@ public class JDBCTools {
 		} catch (SQLException e) {
 			e.printStackTrace();
 		} catch (ClassNotFoundException e) {
-			// TODO Auto-generated catch block
 			e.printStackTrace();
 		} finally {
 			releaseDB(conn, statement);

+ 19 - 33
xtdsp/trunk/src/main/java/com/xt/dsp/job/SqlSynJob.java

@@ -1,8 +1,5 @@
 package com.xt.dsp.job;
 
-import java.io.File;
-import java.io.FileWriter;
-import java.io.IOException;
 import java.sql.Connection;
 import java.sql.DatabaseMetaData;
 import java.sql.ResultSet;
@@ -22,6 +19,7 @@ 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;
@@ -33,12 +31,14 @@ 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;
 import com.xt.dsp.model.TaskBean;
 import com.xt.dsp.model.TaskSqlBean;
+import com.xt.dsp.service.CacheBeanService;
 import com.xt.dsp.service.DataSourceService;
 import com.xt.dsp.service.JobService;
 import com.xt.dsp.service.TaskService;
@@ -49,10 +49,12 @@ public class SqlSynJob implements Job {
 	private TaskService taskService;
 	private TaskSqlService taskSqlService;
 	private DataSourceService dataSourceService;
+	private JobBean job = null;
+	private CacheBeanService cacheBeanService;
 
 	@Override
 	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) {
 			throw new IllegalArgumentException("未找到执行任务信息!");
 		}
@@ -70,14 +72,12 @@ public class SqlSynJob implements Job {
 			this.taskService = appCtx.getBean(TaskService.class);
 			this.taskSqlService = appCtx.getBean(TaskSqlService.class);
 			this.dataSourceService = appCtx.getBean(DataSourceService.class);
+			this.cacheBeanService = appCtx.getBean(CacheBeanService.class);
 			List<TaskBean> tasks = taskService.selectByJobCode(job.getCode());
 			for (TaskBean t : tasks) {
 				// SQL 同步任务
 				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) {
@@ -90,35 +90,21 @@ public class SqlSynJob implements Job {
 	 * 
 	 * @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) {
 			JSONObject result = getSqlResultJsonObject(ts);
 			int rest = runTaskSqlWithJdbcTemplate(ts, result);
 			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);
 			}
 		}
 	}

+ 29 - 10
xtdsp/trunk/src/main/java/com/xt/dsp/model/TaskBean.java

@@ -1,7 +1,15 @@
 package com.xt.dsp.model;
 
+/**
+ * 抽取任务对象
+ * 
+ * @author yuanxd
+ *
+ */
 public class TaskBean {
+	/** SQL->TABLE任务 */
 	public static final String TYPE_SQL = "1";
+
 	private String id;
 
 	private String name;
@@ -12,40 +20,51 @@ public class TaskBean {
 
 	private String type;
 
+	/** 文件缓存目录 */
+	private String cacheFolder;
+
+	public String getCacheFolder() {
+		return cacheFolder;
+	}
+
+	public String getCode() {
+		return code;
+	}
+
 	public String getId() {
 		return id;
 	}
 
-	public void setId(String id) {
-		this.id = id == null ? null : id.trim();
+	public String getJobCode() {
+		return jobCode;
 	}
 
 	public String getName() {
 		return name;
 	}
 
-	public void setName(String name) {
-		this.name = name == null ? null : name.trim();
+	public String getType() {
+		return type;
 	}
 
-	public String getCode() {
-		return code;
+	public void setCacheFolder(String cacheFolder) {
+		this.cacheFolder = cacheFolder;
 	}
 
 	public void setCode(String code) {
 		this.code = code == null ? null : code.trim();
 	}
 
-	public String getJobCode() {
-		return jobCode;
+	public void setId(String id) {
+		this.id = id == null ? null : id.trim();
 	}
 
 	public void setJobCode(String jobCode) {
 		this.jobCode = jobCode == null ? null : jobCode.trim();
 	}
 
-	public String getType() {
-		return type;
+	public void setName(String name) {
+		this.name = name == null ? null : name.trim();
 	}
 
 	public void setType(String type) {

+ 42 - 42
xtdsp/trunk/src/main/java/com/xt/dsp/model/TaskSqlBean.java

@@ -1,63 +1,63 @@
 package com.xt.dsp.model;
 
 public class TaskSqlBean {
-    private String id;
+	private String id;
 
-    private String taskId;
+	private String taskId;
 
-    private String srcConn;
+	private String srcConn;
 
-    private String targetConn;
+	private String targetConn;
 
-    private String sql;
+	private String sql;
 
-    private String targetTable;
+	private String targetTable;
 
-    public String getId() {
-        return id;
-    }
+	public String getId() {
+		return id;
+	}
 
-    public void setId(String id) {
-        this.id = id == null ? null : id.trim();
-    }
+	public void setId(String id) {
+		this.id = id == null ? null : id.trim();
+	}
 
-    public String getTaskId() {
-        return taskId;
-    }
+	public String getTaskId() {
+		return taskId;
+	}
 
-    public void setTaskId(String taskId) {
-        this.taskId = taskId == null ? null : taskId.trim();
-    }
+	public void setTaskId(String taskId) {
+		this.taskId = taskId == null ? null : taskId.trim();
+	}
 
-    public String getSrcConn() {
-        return srcConn;
-    }
+	public String getSrcConn() {
+		return srcConn;
+	}
 
-    public void setSrcConn(String srcConn) {
-        this.srcConn = srcConn == null ? null : srcConn.trim();
-    }
+	public void setSrcConn(String srcConn) {
+		this.srcConn = srcConn == null ? null : srcConn.trim();
+	}
 
-    public String getTargetConn() {
-        return targetConn;
-    }
+	public String getTargetConn() {
+		return targetConn;
+	}
 
-    public void setTargetConn(String targetConn) {
-        this.targetConn = targetConn == null ? null : targetConn.trim();
-    }
+	public void setTargetConn(String targetConn) {
+		this.targetConn = targetConn == null ? null : targetConn.trim();
+	}
 
-    public String getSql() {
-        return sql;
-    }
+	public String getSql() {
+		return sql;
+	}
 
-    public void setSql(String sql) {
-        this.sql = sql == null ? null : sql.trim();
-    }
+	public void setSql(String sql) {
+		this.sql = sql == null ? null : sql.trim();
+	}
 
-    public String getTargetTable() {
-        return targetTable;
-    }
+	public String getTargetTable() {
+		return targetTable;
+	}
 
-    public void setTargetTable(String targetTable) {
-        this.targetTable = targetTable == null ? null : targetTable.trim();
-    }
+	public void setTargetTable(String targetTable) {
+		this.targetTable = targetTable == null ? null : targetTable.trim();
+	}
 }

+ 9 - 0
xtdsp/trunk/src/main/java/com/xt/dsp/service/CacheBeanService.java

@@ -0,0 +1,9 @@
+package com.xt.dsp.service;
+
+import com.xt.dsp.bean.TaskCacheBean;
+
+public interface CacheBeanService {
+	public int writeCache(TaskCacheBean taskCacheBean);
+
+	public TaskCacheBean readCache(TaskCacheBean taskCacheBean);
+}

+ 59 - 0
xtdsp/trunk/src/main/java/com/xt/dsp/serviceImpl/CacheBeanServiceImpl.java

@@ -0,0 +1,59 @@
+package com.xt.dsp.serviceImpl;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Service;
+
+import com.xt.dsp.bean.TaskCacheBean;
+import com.xt.dsp.service.CacheBeanService;
+
+@Service
+public class CacheBeanServiceImpl implements CacheBeanService {
+	private final Logger log = LoggerFactory.getLogger(CacheBeanServiceImpl.class);
+
+	@Override
+	public int writeCache(TaskCacheBean taskCacheBean) {
+		// String fileName = job.getId() + "-" + ts.getTaskId() + "-" +
+		// ts.getId() + System.currentTimeMillis();
+		String cacheFolder = taskCacheBean.getCacheFolder() == null ? "" : taskCacheBean.getCacheFolder();
+		File folder = new File(cacheFolder);
+		String fileName = taskCacheBean.getFileName();
+		File f = new File(folder, fileName);
+		int count = 0;
+		while (f.exists()) {
+			f = new File(folder, fileName + "-" + (++count));
+		}
+		if (count > 0) {
+			f = new File(folder, fileName + "-" + count);
+		}
+		FileWriter fw = null;
+		try {
+			fw = new FileWriter(f);
+			fw.write(getTaskCacheBeanData(taskCacheBean));
+		} catch (IOException e) {
+			log.error("缓存{}保存失败:{}", taskCacheBean.getFileName(), e.getMessage());
+			return -1;
+		} finally {
+			if (null != fw) {
+				try {
+					fw.close();
+				} catch (IOException e) {
+				}
+			}
+		}
+		return 0;
+	}
+
+	private String getTaskCacheBeanData(TaskCacheBean taskCacheBean) {
+		return "";
+	}
+
+	@Override
+	public TaskCacheBean readCache(TaskCacheBean taskCacheBean) {
+		return null;
+	}
+}