downloadalipayrzipsender.java.svn-base

来自「项目支付宝批量打款,采用httpclient+spring +quarz实现.」· SVN-BASE 代码 · 共 123 行

SVN-BASE
123
字号
package com.szhelper.pay.quartz;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.zip.ZipEntry;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.szhelper.pay.ApplicationContextWrapper;
import com.szhelper.pay.Constants;
import com.szhelper.pay.model.DownloadPayBean;
import com.szhelper.pay.quartz.base.BaseSender;
import com.szhelper.pay.quartz.base.Message;
import com.szhelper.pay.quartz.message.DownloadAlipRZipMessage;
import com.szhelper.pay.quartz.message.ParseAlipayRZipMessage;
import com.szhelper.pay.quartz.message.PayUploadMessage;
import com.szhelper.pay.quartz.util.FileCSVHelper;
import com.szhelper.pay.quartz.util.FileZipHelper;
import com.szhelper.pay.quartz.util.PayUtil;
import com.szhelper.pay.service.IPayProxyService;
import com.szhelper.pay.service.impl.PayProxyServiceImpl;

public class DownloadAlipayRZipSender extends BaseSender {
	private static Log logger = LogFactory.getLog(DownloadAlipayRZipSender.class);
	private long threadSleepPeriod = 0;
	private int clientConnTimeout;
	private Map downloadPayConfig;
	private IPayProxyService payProxyService;
	private String downloadPath;
	private String seqPreFix, dbErrLog,backupPath;
	private static BaseSender instance = new DownloadAlipayRZipSender();

	public static BaseSender getInstance() {		
		return instance;
	}

	private DownloadAlipayRZipSender() {
		downloadPayConfig = (Map) ApplicationContextWrapper.getInstance()
				.getBean("downloadPayConfig");		
		threadSleepPeriod = Long.parseLong(downloadPayConfig.get("jobSleepOfDownloadRZip")
				.toString());
		logger.info(threadSleepPeriod);
		clientConnTimeout = Integer.parseInt(downloadPayConfig.get(
				"clientConnTimeout").toString());
		downloadPath = downloadPayConfig.get("downloadPath").toString();
		if(!downloadPath.endsWith("/"))
			downloadPath+="/";
		payProxyService = (PayProxyServiceImpl) ApplicationContextWrapper
				.getInstance().getBean("PayProxyService");
		seqPreFix = ApplicationContextWrapper.getInstance()
		.getBean("seqPreFix").toString();
		dbErrLog = downloadPayConfig.get("dbErrLog").toString();
		backupPath = downloadPayConfig.get("backupPath").toString();	
		if(!backupPath.endsWith("/"))
			backupPath+="/";
	}

	public void run() {
		while (!runFlag) {
			synchronized (this) {
				if (!queue.empty()) {
					DownloadAlipRZipMessage message = (DownloadAlipRZipMessage) queue.pop();
					try {
						if (!send(message)) {
							message.autoIncreaseSentTimes();
							if (message.isAllowSend()) {
								queue.push(message);
							} else {
								message.setStatus(Constants.SENT_RESP_STATUS_E);
								message.getPostMethod().releaseConnection();
							}
						}
					} catch (Exception ex) {
						logger.error(ex);
					} 
				}
				try {
					// Thread.sleep(1L * 1000L);
					Thread.sleep(threadSleepPeriod);
				} catch (Exception e) {
					logger.error(e);
				}
			}
		}
	}
	
	public boolean send(Message msg) {
		DownloadAlipRZipMessage message = (DownloadAlipRZipMessage) msg;
		boolean flag = false;
		HttpClient client = new HttpClient();
		try {	
			String zipfile = message.getCsvname().substring(0,message.getCsvname().indexOf("."))+"_"+PayUtil.getCurrDate()+Constants.POSTFIX_ZIP;
			logger.info("begin to download zip file ["+ zipfile +"] from Alipay by csv file [" + message.getCsvname() +"]");		
			client.setConnectionTimeout(clientConnTimeout);
			int responseCode = client.executeMethod(message.getPostMethod());
			logger.info("download zip file ["+zipfile+"] responseCode: "+ responseCode);			
			//InputStream in = message.getPostMethod().getResponseBodyAsStream();
			byte b[] = message.getPostMethod().getResponseBody();
			FileOutputStream out = new FileOutputStream(downloadPath+zipfile);
			out.write(b, 0, b.length);
			out.close();
			/*FileZipHelper.unZip(in,downloadPath+message.getCsvname().substring(0,message.getCsvname().indexOf("."))+PayUtil.getCurrDate()+Constants.POSTFIX_ZIP);*/
			FileCSVHelper.delUploadedFileName(message.getCsvname(), message
					.getUploadLog());			
			message.getPostMethod().releaseConnection();
			flag = true;	
		} catch (IOException ex) {
			//rollbackLog(message);
			// if error ,ignore do.
			logger.info(ex.getMessage());
		}
		return flag;
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?