Sfoglia il codice sorgente

增加多线程处理

koul 5 anni fa
parent
commit
77b8b69179

+ 41 - 0
src/main/java/com/uas/config/AsyncConfig.java

@@ -0,0 +1,41 @@
+package com.uas.config;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.scheduling.annotation.EnableAsync;
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
+
+import java.util.concurrent.Executor;
+import java.util.concurrent.ThreadPoolExecutor;
+
+/**
+ * @author koul
+ * @email koul@usoftchina.com
+ * @date 2020-11-30 16:58
+ */
+@Configuration
+@EnableAsync  // 启用异步任务
+public class AsyncConfig {
+    // 声明一个线程池(并指定线程池的名字)
+    @Bean("taskExecutor")
+    public Executor asyncExecutor() {
+        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
+        //核心线程数5:线程池创建时候初始化的线程数
+        executor.setCorePoolSize(5);
+        //最大线程数20:线程池最大的线程数,只有在缓冲队列满了之后才会申请超过核心线程数的线程
+        executor.setMaxPoolSize(20);
+        //缓冲队列500:用来缓冲执行任务的队列
+        executor.setQueueCapacity(1000);
+        //允许线程的空闲时间60秒:当超过了核心线程出之外的线程在空闲时间到达之后会被销毁
+        executor.setKeepAliveSeconds(60);
+        //线程池名的前缀:设置好了之后可以方便我们定位处理任务所在的线程池
+        executor.setThreadNamePrefix("DailyAsync-");
+        //所有任务处理完毕开始关闭线程池
+        executor.setWaitForTasksToCompleteOnShutdown(true);
+        // rejection-policy:当pool已经达到max size的时候,如何处理新任务
+        // CALLER_RUNS:不在新线程中执行任务,而是有调用者所在的线程来执行
+        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
+        executor.initialize();
+        return executor;
+    }
+}

+ 11 - 0
src/main/java/com/uas/service/DownloadFilePathService.java

@@ -0,0 +1,11 @@
+package com.uas.service;
+
+/**
+ * @author koul
+ */
+public interface DownloadFilePathService {
+
+    void runDownloadFilePath();
+	
+	void downloadFilePath(String depot,String sob);
+ }

+ 9 - 0
src/main/java/com/uas/service/DownloadService.java

@@ -0,0 +1,9 @@
+package com.uas.service;
+
+
+public interface DownloadService{
+
+	void downloadRun();
+
+	void downloadByDepotAndSob(String depot,String sob,String enterprise);
+ }

+ 11 - 5
src/main/java/com/uas/main/DownloadFilePath.java → src/main/java/com/uas/service/Impl/DownloadFilePathServiceImpl.java

@@ -1,10 +1,12 @@
-package com.uas.main;
+package com.uas.service.Impl;
 
+import com.uas.service.DownloadFilePathService;
 import com.uas.util.*;
 import org.apache.commons.net.ftp.FTPClient;
 import org.apache.http.entity.ContentType;
-
 import org.springframework.mock.web.MockMultipartFile;
+import org.springframework.scheduling.annotation.Async;
+import org.springframework.stereotype.Service;
 import org.springframework.web.multipart.MultipartFile;
 
 import java.io.File;
@@ -18,9 +20,11 @@ import java.util.Map;
 /**
  * @author koul
  */
-public class DownloadFilePath {
+@Service
+public class DownloadFilePathServiceImpl implements DownloadFilePathService {
 
-    public static void runDownloadFilePath(){
+	@Override
+    public void runDownloadFilePath(){
 		//联合创泰-富森
         downloadFilePath("N_YITOA_LHCT-FS","N_YITOA_LHCT");
 		//联合创泰-顺丰
@@ -31,7 +35,9 @@ public class DownloadFilePath {
 	}
 	
 	@SuppressWarnings("unchecked")
-	public static void downloadFilePath(String depot,String sob){
+	@Async("taskExecutor")
+	@Override
+	public void downloadFilePath(String depot,String sob){
 		BaseUtil.getLogger().info("download file" + " from "+depot);
 		FTPClient client = null;
 		SFTPUtil sftp = null;

+ 145 - 134
src/main/java/com/uas/main/Download.java → src/main/java/com/uas/service/Impl/DownloadServiceImpl.java

@@ -1,6 +1,7 @@
-package com.uas.main;
+package com.uas.service.Impl;
 
 import com.jcraft.jsch.JSchException;
+import com.uas.service.DownloadService;
 import com.uas.util.BaseUtil;
 import com.uas.util.FtpUtil;
 import com.uas.util.JdbcUtil;
@@ -10,6 +11,8 @@ import org.codehaus.jackson.map.ObjectMapper;
 import org.dom4j.Document;
 import org.dom4j.Element;
 import org.dom4j.io.SAXReader;
+import org.springframework.scheduling.annotation.Async;
+import org.springframework.stereotype.Service;
 
 import java.io.File;
 import java.io.FileInputStream;
@@ -19,11 +22,138 @@ import java.sql.Connection;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Statement;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 
-public class Download{
+@Service
+public class DownloadServiceImpl implements DownloadService {
 
-	public static Boolean checkXmlFileToSqlToEnterprise(File file,String enterprise){
+	@Override
+	public void downloadRun(){
+		//联合创泰-富森
+		downloadByDepotAndSob("N_YITOA_LHCT-FS","N_YITOA_LHCT","聯合創泰科技有限公司");
+		//联合创泰-顺丰
+		downloadByDepotAndSob("N_YITOA_LHCT-SF","N_YITOA_LHCT","聯合創泰科技有限公司");
+		//联合创泰-准时达
+		downloadByDepotAndSob("N_YITOA_LHCT-ZSD","N_YITOA_LHCT","聯合創泰科技有限公司");
+		//联合创泰-SLC上海保税仓
+		downloadByDepotAndSob("N_YITOA_LHCT-SLC","N_YITOA_LHCT","聯合創泰科技有限公司");
+		//联合创泰-中芯
+		//downloadByDepotAndSob("N_YITOA_LHCT-ZX","N_YITOA_LHCT","聯合創泰科技有限公司");
+		//联合创泰-泓明
+		downloadByDepotAndSob("N_YITOA_LHCT-HM","N_YITOA_LHCT","聯合創泰科技有限公司");
+		//英唐创泰香港-富森
+		downloadByDepotAndSob("N_YITOA_LHCT_HK-FS","N_YITOA_LHCT_HK","英唐創泰香港科技有限公司");
+
+
+	}
+
+	@SuppressWarnings("unchecked")
+	@Override
+	@Async("taskExecutor")
+	public void downloadByDepotAndSob(String depot,String sob,String enterprise){
+		BaseUtil.getLogger().info("download xml" + " from "+depot);
+		FTPClient client = null;
+		SFTPUtil sftp = null;
+		List<String> sqls = new ArrayList<String>();
+		boolean executeRes = false;
+		Connection connect = null;
+
+		try {
+			Map<String,Object> servMap = JdbcUtil.getFtpConfigs();
+			Map<String,Object> ftpMap = (Map<String,Object>)servMap.get(depot);
+			if ("N_YITOA_LHCT-SF".equals(depot)){
+				sftp = new SFTPUtil(ftpMap.get("user").toString(),ftpMap.get("password").toString(),
+						ftpMap.get("ip").toString(),
+						Integer.parseInt(ftpMap.get("port").toString()));
+				try {
+					sftp.login();
+				} catch (JSchException e) {
+					e.printStackTrace();
+				}
+			}else {
+				client = FtpUtil.connect(ftpMap);
+			}
+			if(client!=null||sftp!=null) {
+				List<File> files = null;
+				if ("N_YITOA_LHCT-SF".equals(depot)) {
+					files = sftp.download(ftpMap.get("downloadpath").toString());
+
+				}else if("N_YITOA_LHCT-SLC".equals(depot)){
+					files = FtpUtil.downloadDirFileByType(client, "XML",
+							ftpMap.get("downloadpath").toString());
+				}else{
+					files = FtpUtil.downloadAllFileByType(client, "XML");
+				}
+				String fileName = "";
+				if (files != null && files.size() > 0) {
+					connect = JdbcUtil.getConnectBySob(sob);
+					if (connect != null) {
+						for (File file : files) {
+							try {
+								fileName += "," + file.getName();
+								//判断当前帐套和文件是否一致
+								if (!checkXmlFileToSqlToEnterprise(file, enterprise)) {
+									continue;
+								}
+								Statement statement = connect.createStatement();
+								sqls = parseXmlFileToSqls(file, statement);
+								if (sqls == null) {
+									continue;
+								}
+
+								executeRes = JdbcUtil.executeSqls(connect, sqls);
+
+								if (executeRes) {
+									if ("N_YITOA_LHCT-SF".equals(depot)) {
+										String s = file.getName().substring(0, file.getName().indexOf(".")) + ".xml";
+										Boolean rename = sftp.rename(ftpMap.get("downloadpath").toString() + "/" + s, "goodsreceipt/backup/" + s);
+									} else {
+										//移动处理完的文件到bak文件夹下
+										client.rename(file.getName(), "bak/" + file.getName());
+									}
+								}
+								statement.close();
+							} catch (Exception e) {
+								e.printStackTrace();
+								BaseUtil.getLogger().error(e.toString());
+								continue;
+							}
+						}
+
+					}
+					if (!"".equals(fileName)) {
+						BaseUtil.logDownload(fileName.substring(1), ftpMap.get("ip").toString(), ftpMap.get("downloadpath").toString());
+					}
+				}
+			}
+		} catch (Exception e) {
+			BaseUtil.getLogger().error(e.toString());
+			e.printStackTrace();
+		}finally{
+			if(client!=null){
+				FtpUtil.closeFtpClient(client);
+				client = null;
+			}
+			if (sftp!=null){
+				sftp.logout();
+			}
+			//把当前的连接关闭
+			try {
+				if(connect!=null){
+					connect.close();
+				}
+			} catch (SQLException e) {
+				e.printStackTrace();
+			}finally{
+				connect = null;
+			}
+		}
+	}
+
+	private Boolean checkXmlFileToSqlToEnterprise(File file,String enterprise){
 		String ent = null;
 		Element root = getElementRoot(file);  //获取根节点
 		Map<String, String> mainData = getMainData(root); //获取主表数据 {tag:data}
@@ -38,7 +168,7 @@ public class Download{
 	/*
 	 * 通过配置解析xml为sql语句
 	 */
-	public static List<String> parseXmlFileToSqls(File file,Statement statement){
+	private List<String> parseXmlFileToSqls(File file,Statement statement){
 		List<String> sqls = new ArrayList<String>();
 		String mainTableUpdateSql = null;
 		String updateDetailConditon = null;
@@ -164,7 +294,7 @@ public class Download{
 	/*
 	 * 获取明细表更新sql
 	 */
-	public static String getUpdateDetailSql(Map<String, String> data,Map<String,String> xmlConfig,List<Map<String,String>> detailXmlConfig){
+	private String getUpdateDetailSql(Map<String, String> data,Map<String,String> xmlConfig,List<Map<String,String>> detailXmlConfig){
 		StringBuffer sb = new StringBuffer();
 		String sql = null;
 		String condition = null;
@@ -185,7 +315,7 @@ public class Download{
 	/*
 	 * 获取添加临时表数据sql
 	 */
-	public static String getInsertDetailSql(String maincode,Map<String, String> data,Map<String,String> xmlConfig,List<Map<String,String>> detailXmlConfig){
+	private String getInsertDetailSql(String maincode,Map<String, String> data,Map<String,String> xmlConfig,List<Map<String,String>> detailXmlConfig){
 		StringBuffer sb = new StringBuffer();
 		StringBuffer sb2 =new StringBuffer();
 		String sql = null;
@@ -223,7 +353,7 @@ public class Download{
 	/*
 	 * 获取明细表和主表关联的更新条件
 	 */
-	public static String getUpdateDetailConditionByMainTable(Map<String, String> mainData,Map<String,String> xmlConfig){
+	private String getUpdateDetailConditionByMainTable(Map<String, String> mainData,Map<String,String> xmlConfig){
 		String mainConditionField = xmlConfig.get("xd_main_conditionfield");
 		String mainConditionTag = xmlConfig.get("xd_main_conditiontag");
 		String[] fields = mainConditionField.split(",");
@@ -247,7 +377,7 @@ public class Download{
 	/*
 	 * 获取更新主表字段的sql
 	 */
-	public static String getUpdateMainFields(Map<String, String> mainData,Map<String,String> xmlConfig,List<Map<String,String>> mainXmlConfig){
+	private String getUpdateMainFields(Map<String, String> mainData,Map<String,String> xmlConfig,List<Map<String,String>> mainXmlConfig){
 		StringBuffer sb = new StringBuffer();
 		String update = "update " + xmlConfig.get("xd_table") + " set ";
 		for(Map<String,String> map:mainXmlConfig){
@@ -263,7 +393,7 @@ public class Download{
 	/*
 	 * 获取主表更新条件
 	 */
-	public static String getUpdateMainCondtion(Map<String, String> mainData,Map<String,String> xmlConfig){
+	private String getUpdateMainCondtion(Map<String, String> mainData,Map<String,String> xmlConfig){
 		String mainConditionField = xmlConfig.get("xd_main_conditionfield");
 		String mainConditionTag = xmlConfig.get("xd_main_conditiontag");
 		String[] fields = mainConditionField.split(",");
@@ -284,7 +414,7 @@ public class Download{
 	/*
 	 * 获取更新明细表的条件
 	 */
-	public static String getUpdateDetailCondition(Map<String, String> mainData,Map<String,String> xmlConfig){
+	private String getUpdateDetailCondition(Map<String, String> mainData,Map<String,String> xmlConfig){
 		String mainConditionField = xmlConfig.get("xd_detail_conditionfield");
 		String mainConditionTag = xmlConfig.get("xd_detail_conditiontag");
 		String[] fields = mainConditionField.split(",");
@@ -305,7 +435,7 @@ public class Download{
 	/*
 	 * 获取xmldataconfigdetail配置
 	 */
-	public static List<Map<String,String>> getXmlConfigDetail(Statement statement,String id,String type,String conditiontag){
+	private List<Map<String,String>> getXmlConfigDetail(Statement statement,String id,String type,String conditiontag){
 		List<Map<String,String>> list = new ArrayList<Map<String,String>>();
 		
 		ResultSet rs = null;
@@ -328,7 +458,7 @@ public class Download{
 	/*
 	 * 获取xmldataconfigdetail配置
 	 */
-	public static List<Map<String,String>> getXmlConfigDetail(Statement statement,String id,String type){
+	private List<Map<String,String>> getXmlConfigDetail(Statement statement,String id,String type){
 		List<Map<String,String>> list = new ArrayList<Map<String,String>>();
 		
 		ResultSet rs = null;
@@ -351,7 +481,7 @@ public class Download{
 	/*
 	 * 获取xmldataconfig配置
 	 */
-	public static Map<String,String> getXmlConfig(Statement statement,String docType,String filePrefix){
+	private Map<String,String> getXmlConfig(Statement statement,String docType,String filePrefix){
 		Map<String,String> map = new HashMap<String,String>();
 		ResultSet rs = null;
 		try{
@@ -383,7 +513,7 @@ public class Download{
 	/*
 	 * 获取根节点
 	 */
-	public static Element getElementRoot(File file){
+	private Element getElementRoot(File file){
         Element element = null;
 		try {
 			InputStream in = new FileInputStream(file);
@@ -427,124 +557,5 @@ public class Download{
 			data.add(detail);
     	}
 		return data;
-	}			
-    public static void downloadRun(){
-		//联合创泰-富森
-		downloadByDepotAndSob("N_YITOA_LHCT-FS","N_YITOA_LHCT","聯合創泰科技有限公司");
-		//联合创泰-顺丰
-		downloadByDepotAndSob("N_YITOA_LHCT-SF","N_YITOA_LHCT","聯合創泰科技有限公司");
-		//联合创泰-准时达
-		downloadByDepotAndSob("N_YITOA_LHCT-ZSD","N_YITOA_LHCT","聯合創泰科技有限公司");
-		//联合创泰-SLC上海保税仓
-        downloadByDepotAndSob("N_YITOA_LHCT-SLC","N_YITOA_LHCT","聯合創泰科技有限公司");
-		//联合创泰-中芯
-		//downloadByDepotAndSob("N_YITOA_LHCT-ZX","N_YITOA_LHCT","聯合創泰科技有限公司");
-		//联合创泰-泓明
-		downloadByDepotAndSob("N_YITOA_LHCT-HM","N_YITOA_LHCT","聯合創泰科技有限公司");
-		//英唐创泰香港-富森
-		downloadByDepotAndSob("N_YITOA_LHCT_HK-FS","N_YITOA_LHCT_HK","英唐創泰香港科技有限公司");
-
-
-	}
-
-	@SuppressWarnings("unchecked")
-	public static void downloadByDepotAndSob(String depot,String sob,String enterprise){
-		BaseUtil.getLogger().info("download xml" + " from "+depot);
-		FTPClient client = null;
-		SFTPUtil sftp = null;
-		List<String> sqls = new ArrayList<String>();
-		boolean executeRes = false;
-		Connection connect = null;
-
-		try {
-			Map<String,Object> servMap = JdbcUtil.getFtpConfigs();
-			Map<String,Object> ftpMap = (Map<String,Object>)servMap.get(depot);
-			if ("N_YITOA_LHCT-SF".equals(depot)){
-				sftp = new SFTPUtil(ftpMap.get("user").toString(),ftpMap.get("password").toString(),
-						ftpMap.get("ip").toString(),
-						Integer.parseInt(ftpMap.get("port").toString()));
-				try {
-					sftp.login();
-				} catch (JSchException e) {
-					e.printStackTrace();
-				}
-			}else {
-				client = FtpUtil.connect(ftpMap);
-			}
-			if(client!=null||sftp!=null) {
-				List<File> files = null;
-				if ("N_YITOA_LHCT-SF".equals(depot)) {
-					files = sftp.download(ftpMap.get("downloadpath").toString());
-
-				}else if("N_YITOA_LHCT-SLC".equals(depot)){
-					files = FtpUtil.downloadDirFileByType(client, "XML",
-							ftpMap.get("downloadpath").toString());
-				}else{
-					files = FtpUtil.downloadAllFileByType(client, "XML");
-				}
-				String fileName = "";
-				if (files != null && files.size() > 0) {
-					connect = JdbcUtil.getConnectBySob(sob);
-					if (connect != null) {
-						for (File file : files) {
-							try {
-								fileName += "," + file.getName();
-								//判断当前帐套和文件是否一致
-								if (!checkXmlFileToSqlToEnterprise(file, enterprise)) {
-									continue;
-								}
-								Statement statement = connect.createStatement();
-								sqls = parseXmlFileToSqls(file, statement);
-								if (sqls == null) {
-									continue;
-								}
-
-								executeRes = JdbcUtil.executeSqls(connect, sqls);
-
-								if (executeRes) {
-									if ("N_YITOA_LHCT-SF".equals(depot)) {
-										String s = file.getName().substring(0, file.getName().indexOf(".")) + ".xml";
-										Boolean rename = sftp.rename(ftpMap.get("downloadpath").toString() + "/" + s, "goodsreceipt/backup/" + s);
-									} else {
-										//移动处理完的文件到bak文件夹下
-										client.rename(file.getName(), "bak/" + file.getName());
-									}
-								}
-								statement.close();
-							} catch (Exception e) {
-								e.printStackTrace();
-								BaseUtil.getLogger().error(e.toString());
-								continue;
-							}
-						}
-
-					}
-					if (!"".equals(fileName)) {
-						BaseUtil.logDownload(fileName.substring(1), ftpMap.get("ip").toString(), ftpMap.get("downloadpath").toString());
-					}
-				}
-			}
-		} catch (Exception e) {
-			BaseUtil.getLogger().error(e.toString());
-			e.printStackTrace();
-		}finally{
-			if(client!=null){
-				FtpUtil.closeFtpClient(client);
-				client = null;
-			}
-			if (sftp!=null){
-				sftp.logout();
-			}
-			//把当前的连接关闭
-			try {
-				if(connect!=null){
-					connect.close();
-				}
-			} catch (SQLException e) {
-				e.printStackTrace();
-			}finally{
-				connect = null;
-			}
-		}
 	}
  }

+ 97 - 90
src/main/java/com/uas/main/Upload.java → src/main/java/com/uas/service/Impl/UploadServiceImpl.java

@@ -1,102 +1,39 @@
-package com.uas.main;
+package com.uas.service.Impl;
 
 import com.jcraft.jsch.JSchException;
+import com.uas.service.UploadService;
 import com.uas.util.BaseUtil;
 import com.uas.util.FtpUtil;
 import com.uas.util.JdbcUtil;
 import com.uas.util.SFTPUtil;
 import org.apache.commons.net.ftp.FTPClient;
+import org.springframework.scheduling.annotation.Async;
+import org.springframework.stereotype.Service;
 
 import java.io.*;
 import java.sql.Connection;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Statement;
-import java.util.Date;
 import java.util.Map;
 import java.util.regex.Pattern;
 
-public class Upload {
-	public static Pattern pat = Pattern.compile("\\{(.*?)\\}");	
-	
-	public static File createXmlFile(String data,String id,String type,String fileprefix,String date){ //生成xml文件
-		File xmlFile = null;
-		try{
-			String xml =data; 
-			xmlFile = new File( System.getProperty("java.io.tmpdir")+File.separator +fileprefix+date + "_" + id +".xml");
-			if(!xmlFile.exists()){
-				if(!xmlFile.getParentFile().exists()){
-					xmlFile.mkdir();
-				}
-				xmlFile.createNewFile();
-			}
-			FileWriter fw = new FileWriter(xmlFile.getAbsoluteFile());
-            OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(xmlFile.getAbsoluteFile()),"UTF-8");
-            out.write(xml);
-            out.flush();
-            out.close();
-			fw.close();				
-		}catch(Exception e){
-			BaseUtil.getLogger().error(e.toString());
-			e.printStackTrace();
-		}
-		return xmlFile;
-	}
-	
-	
-	public static void turnToFormal(Connection connection,String id,String fileName){ //临时表数据转入正式
-		Statement statement = null;
-		try{
-			connection.setAutoCommit(false);
-			statement = connection.createStatement();
-			statement.setQueryTimeout(180);
-			statement.execute("insert into xmldatalog(xl_id,xl_data,xl_date,xl_depot,xl_caller,xl_code,xl_from,xl_status,xl_sourceid,xl_filename) "
-					+ "select XMLDATALOG_SEQ.NEXTVAL,xl_data,sysdate,xl_depot,xl_caller,xl_code,'upload','success',xl_sourceid,'"+fileName+"' from xmldatalogtemp where xl_id=" + id);
-			statement.execute("delete from xmldatalogtemp where xl_id=" + id);
-			connection.commit();
-		}catch(Exception e){
-			try {
-				connection.rollback();
-			} catch (SQLException e1) {
-				BaseUtil.getLogger().error(e1.toString());
-				e1.printStackTrace();
-			}
-			BaseUtil.getLogger().error(e.toString());
-			e.printStackTrace();
-		}finally{
-			try{
-				if(statement!=null){
-					statement.close();
-				}			
-			}catch(Exception e){
-				BaseUtil.getLogger().error(e.toString());
-			}
-			statement = null;
-		}
-	}
-	
-	public static boolean upload(FTPClient client,String ip,String folder,File file){
-		boolean flag = false;
-		if(client!=null){
-			flag = FtpUtil.uploadFile(client, folder,file);
-		}
-		BaseUtil.getLogger().info("upload " + file.getName() + " to " + ip + ":" + folder + " " + (flag?"success":"fail")+"");
-		return flag;
-	}
-	
-	public static ResultSet getXmlData(Statement statement) throws SQLException{
-		return statement.executeQuery("select xl_id,xl_data,xl_fileprefix,to_char(xl_date,'yyyymmddhh24miss') xl_date,xl_depot from xmldatalogtemp where xl_data is not null order by xl_depot desc");
-	}
-	
-	public static void uploadRun() {
-        //联合创泰
+@Service
+public class UploadServiceImpl implements UploadService {
+	private Pattern pat = Pattern.compile("\\{(.*?)\\}");
+
+	@Override
+	public void uploadRun() {
+		//联合创泰
 		uploadBySob("N_YITOA_LHCT");
-        //联合创泰香港
+		//联合创泰香港
 		//uploadBySob("N_YITOA_LHCT_HK");
 	}
-	
+
+	@Override
 	@SuppressWarnings({ "unchecked"})
-	public static void uploadBySob(String sob) {
+	@Async("taskExecutor")
+	public void uploadBySob(String sob) {
 		BaseUtil.getLogger().info("upload" + " from "+sob);
 		Statement statement = null;
 		FTPClient client = null;
@@ -113,9 +50,9 @@ public class Upload {
 			connect = JdbcUtil.getConnectBySob(sob);
 			if(connect!=null){
 				statement = connect.createStatement();
-				ResultSet rs = 	getXmlData(statement);	
-				String depot = "";	
-				while(rs.next()){	
+				ResultSet rs = 	getXmlData(statement);
+				String depot = "";
+				while(rs.next()){
 					try{
 						xldata = rs.getString("xl_data");
 						fileprefix = rs.getString("xl_fileprefix");
@@ -151,13 +88,13 @@ public class Upload {
 									continue;
 								}
 							}
-							
+
 							if(config==null){
 								continue;
 							}
-							
+
 							String folder = config.get("in").toString();
-							if(xldata==null||"".equals(xldata)){ 
+							if(xldata==null||"".equals(xldata)){
 								continue;
 							}
 
@@ -174,15 +111,15 @@ public class Upload {
 							//如果文件上传成功,则转入正式数据记录表
 							if(uploadSuccess){
 								//转入正式
-								turnToFormal(JdbcUtil.getConnect(),xlid,file.getName());
-							}														
-						}						
+								turnToFormal(connect,xlid,file.getName());
+							}
+						}
 					}catch(Exception e){
 						e.printStackTrace();
 						BaseUtil.getLogger().error(e.toString());
 						continue;
 					}
-				}	
+				}
 				rs.close();
 			}
 		}catch(Exception e){
@@ -196,7 +133,7 @@ public class Upload {
 					BaseUtil.getLogger().error(e1.toString());
 					e1.printStackTrace();
 				}
-				statement = null;				
+				statement = null;
 			}
 			if(client!=null){
 				FtpUtil.closeFtpClient(client);
@@ -214,4 +151,74 @@ public class Upload {
 		}
 
 	}
+	
+	private File createXmlFile(String data,String id,String type,String fileprefix,String date){ //生成xml文件
+		File xmlFile = null;
+		try{
+			String xml =data; 
+			xmlFile = new File( System.getProperty("java.io.tmpdir")+File.separator +fileprefix+date + "_" + id +".xml");
+			if(!xmlFile.exists()){
+				if(!xmlFile.getParentFile().exists()){
+					xmlFile.mkdir();
+				}
+				xmlFile.createNewFile();
+			}
+			FileWriter fw = new FileWriter(xmlFile.getAbsoluteFile());
+            OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(xmlFile.getAbsoluteFile()),"UTF-8");
+            out.write(xml);
+            out.flush();
+            out.close();
+			fw.close();				
+		}catch(Exception e){
+			BaseUtil.getLogger().error(e.toString());
+			e.printStackTrace();
+		}
+		return xmlFile;
+	}
+	
+	
+	private void turnToFormal(Connection connection,String id,String fileName){ //临时表数据转入正式
+		Statement statement = null;
+		try{
+			connection.setAutoCommit(false);
+			statement = connection.createStatement();
+			statement.setQueryTimeout(180);
+			statement.execute("insert into xmldatalog(xl_id,xl_data,xl_date,xl_depot,xl_caller,xl_code,xl_from,xl_status,xl_sourceid,xl_filename) "
+					+ "select XMLDATALOG_SEQ.NEXTVAL,xl_data,sysdate,xl_depot,xl_caller,xl_code,'upload','success',xl_sourceid,'"+fileName+"' from xmldatalogtemp where xl_id=" + id);
+			statement.execute("delete from xmldatalogtemp where xl_id=" + id);
+			connection.commit();
+		}catch(Exception e){
+			try {
+				connection.rollback();
+			} catch (SQLException e1) {
+				BaseUtil.getLogger().error(e1.toString());
+				e1.printStackTrace();
+			}
+			BaseUtil.getLogger().error(e.toString());
+			e.printStackTrace();
+		}finally{
+			try{
+				if(statement!=null){
+					statement.close();
+				}			
+			}catch(Exception e){
+				BaseUtil.getLogger().error(e.toString());
+			}
+			statement = null;
+		}
+	}
+	
+	private boolean upload(FTPClient client,String ip,String folder,File file){
+		boolean flag = false;
+		if(client!=null){
+			flag = FtpUtil.uploadFile(client, folder,file);
+		}
+		BaseUtil.getLogger().info("upload " + file.getName() + " to " + ip + ":" + folder + " " + (flag?"success":"fail")+"");
+		return flag;
+	}
+	
+	private ResultSet getXmlData(Statement statement) throws SQLException{
+		return statement.executeQuery("select xl_id,xl_data,xl_fileprefix,to_char(xl_date,'yyyymmddhh24miss') xl_date,xl_depot from xmldatalogtemp where xl_data is not null order by xl_depot desc");
+	}
+
 }

+ 8 - 0
src/main/java/com/uas/service/UploadService.java

@@ -0,0 +1,8 @@
+package com.uas.service;
+
+
+public interface UploadService {
+	void uploadRun();
+	
+	void uploadBySob(String sob);
+}

+ 21 - 33
src/main/java/com/uas/task/ScheduleTask.java

@@ -1,17 +1,15 @@
 package com.uas.task;
 
 
+import com.uas.service.DownloadFilePathService;
+import com.uas.service.DownloadService;
+import com.uas.service.UploadService;
 import com.uas.util.BaseUtil;
-import com.uas.util.JdbcUtil;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Component;
 
-import static com.uas.main.Download.downloadRun;
-import static com.uas.main.DownloadFilePath.runDownloadFilePath;
-import static com.uas.main.Upload.uploadRun;
-
 
 /**
  * @author koul
@@ -20,19 +18,26 @@ import static com.uas.main.Upload.uploadRun;
  */
 @Component("scheduleTask")
 public class ScheduleTask {
-    private final Logger logger = LoggerFactory.getLogger(this.getClass());
+
+    @Autowired
+    private UploadService uploadService;
+
+    @Autowired
+    private DownloadService downloadService;
+
+    @Autowired
+    private DownloadFilePathService downloadFilePathService;
 
     /**
      * XML文件处理
      */
     @Scheduled(fixedRate=1000*60*5)
     public void ediSendAndReceive() {
-        logger.info("XML文件处理开始");
+        BaseUtil.getLogger().info("XML文件处理开始");
         long timeMillis = System.currentTimeMillis();
-        uploadRun();
-        downloadRun();
-        closeJdbc();
-        logger.info("XML文件处理结束:用时" + ((System.currentTimeMillis() - timeMillis) / 1000));
+        uploadService.uploadRun();
+        downloadService.downloadRun();
+        BaseUtil.getLogger().info("XML文件处理结束:用时" + ((System.currentTimeMillis() - timeMillis) / 1000));
     }
 
     /**
@@ -40,26 +45,9 @@ public class ScheduleTask {
      */
     @Scheduled(fixedRate=1000*60*60)
     public void downloadFile() {
-        logger.info("签收单回传附件开始");
+        BaseUtil.getLogger().info("签收单回传附件开始");
         long timeMillis = System.currentTimeMillis();
-        runDownloadFilePath();
-        closeJdbc();
-        logger.info("签收单回传附件结束:用时" + ((System.currentTimeMillis() - timeMillis) / 1000));
-    }
-
-    /**
-     * 关闭数据库连接
-     */
-    private void closeJdbc(){
-        try{
-            if(JdbcUtil.connection!=null){
-                JdbcUtil.connection.close();
-            }
-        }catch(Exception e){
-            BaseUtil.getLogger().error(e.toString());
-            e.printStackTrace();
-        }finally{
-            JdbcUtil.connection = null;
-        }
+        downloadFilePathService.runDownloadFilePath();
+        BaseUtil.getLogger().info("签收单回传附件结束:用时" + ((System.currentTimeMillis() - timeMillis) / 1000));
     }
 }