定时任务实现方式总结

java timer  实现定时任务

web 应用  利用servlet或listenter 来启动定时任务

1. servlet

package com.rc.portal.webapp.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.rmi.RemoteException;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.rpc.ServiceException;

import md5.MD5Util;

import org.springframework.web.context.support.WebApplicationContextUtils;

import com._99bill.sandbox.apipay.services.gatewayOrderQuery.GatewayOrderQuery;
import com._99bill.sandbox.apipay.services.gatewayOrderQuery.GatewayOrderQueryServiceLocator;
import com.bill99.seashell.domain.dto.gatewayquery.GatewayOrderDetail;
import com.bill99.seashell.domain.dto.gatewayquery.GatewayOrderQueryRequest;
import com.bill99.seashell.domain.dto.gatewayquery.GatewayOrderQueryResponse;
import com.rc.commons.util.DateUtil;
import com.rc.portal.dao.QuaiqianOrderQueryDao;
import com.rc.portal.dao.impl.QuaiqianOrderQueryDaoImpl;
import com.rc.portal.service.QuaiQianOrderQueryManage;
import com.rc.portal.service.impl.QuaiQianOrderQueryImpl;
import com.rc.portal.vo.QuaiqianOrders;
import com.rc.portal.webapp.util.CommonParam;
import com.rc.portal.webapp.util.TimerManager;

public class KuaiQianServlet extends HttpServlet {
	
	TimerManager time=null;
	ServletContext context;
	org.springframework.context.ApplicationContext ctx; 
 
	private QuaiQianOrderQueryManage quaiqianorderquery;
	private QuaiqianOrderQueryDao query ;
	private String Errormess;

	public String getErrormess() {
		return Errormess;
	}

	public void setErrormess(String errormess) {
		Errormess = errormess;
	}

	public QuaiQianOrderQueryManage getQuaiqianorderquery() {
		return quaiqianorderquery;
	}

	public void setQuaiqianorderquery(
			QuaiQianOrderQueryManage quaiqianorderquery) {
		this.quaiqianorderquery = quaiqianorderquery;
	}

	public QuaiqianOrderQueryDao getQuery() {
		return query;
	}

	public void setQuery(QuaiqianOrderQueryDao query) {
		this.query = query;
	}
	/**
	 * Constructor of the object.
	 */
	public KuaiQianServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. 
*/ public void destroy() { time.timer.cancel(); System.out.println("停止了timer"); } /** * Initialization of the servlet.
* * @throws ServletException if an error occurs */ public void init() throws ServletException { System.out.println("我先执行。。。"); // spring是无法为servlet或listenter filter 等由容器来管理的对象注入依赖对象的 此时这样注入。。 context = this.getServletContext() ; ctx= WebApplicationContextUtils.getWebApplicationContext(context); // getBean("bean 的 id 属性") query=(QuaiqianOrderQueryDaoImpl)ctx.getBean("query"); quaiqianorderquery=(QuaiQianOrderQueryImpl)ctx.getBean("quaiqianorderquery"); kuaiQianOrderToSysDatabase(); } private void kuaiQianOrderToSysDatabase() { // TODO Auto-generated method stub Calendar calendar = Calendar.getInstance(); Calendar upcalendar = Calendar.getInstance(); CommonParam param = new CommonParam(); Integer count = 0; try { count = query.getKquaiqinaOrdersCount(); } catch (SQLException e2) { System.out.println("得到快钱订单数量失败。。"); e2.printStackTrace(); } if (count > 0) { List q = null; QuaiqianOrders qs=null; try { q = query.getlastDate(); qs=q.get(0); } catch (SQLException e2) { System.out.println("得到最近的快钱订单时间失败。"); e2.printStackTrace(); } if (q != null) { Date d = qs.getRecorddate(); calendar.setTime(d); int sysday = calendar.get(Calendar.DATE); calendar.setTime(new Date()); int upday = calendar.get(Calendar.DATE); if (sysday < upday) { // calendar.setTime(d); // calendar.add(Calendar.DATE, 1); // upcalendar.add(Calendar.DATE, -1); // calendar.set(Calendar.HOUR_OF_DAY, 0); // calendar.set(Calendar.SECOND, 0); // calendar.set(Calendar.MINUTE, 0); // upcalendar.set(Calendar.HOUR_OF_DAY, 23); // upcalendar.set(Calendar.SECOND, 59); // upcalendar.set(Calendar.MINUTE, 59); // String startime = DateUtil.sdfLongTimePlus_s // .format(calendar.getTime()); // String endtime = DateUtil.sdfLongTimePlus_s // .format(upcalendar.getTime()); //人民币账号 //本参数用来指定接收款项的快钱用户的人民币账号 String merchantAcctId="1001213884201"; //客户编号所对应的密钥。。在账户邮箱中获取 String key ="5UHQX2G65W4ECF5G"; //字符集 固定值:1 1代表UTF-8 String inputCharsqet ="1"; //查询接口版本 固定值:v2.0注意为小写字母 String versqion = "v2.0"; //签名类型 固定值:1 1代表MD5加密签名方式 String signType ="1"; //查询模式 固定值:1 1代表简单查询(返回基本订单信息) String queryMode ="1"; //请求记录集页码 在查询结果数据总量很大时,快钱会将支付结果分多次返回。本参数表示商户需要得到的记录集页码。 //默认为1,表示第1页。 String requestPage ="1"; GatewayOrderQueryRequest rsq=new GatewayOrderQueryRequest(); rsq.setInputCharset(inputCharsqet); rsq.setVersion(versqion); rsq.setSignType(Integer.valueOf(signType)); rsq.setMerchantAcctId(merchantAcctId); rsq.setQueryType(1); rsq.setQueryMode(Integer.valueOf(queryMode)); //测试时间 String startime="20130413000000"; String endtime="20130413235959"; rsq.setStartTime(startime); rsq.setEndTime(endtime); rsq.setRequestPage(requestPage); //组合字符串。。必须按照此顺序组串 String signMsgVal=""; signMsgVal = quaiqianorderquery.AppendParam(signMsgVal,"inputCharset",inputCharsqet); signMsgVal = quaiqianorderquery.AppendParam(signMsgVal,"version",versqion); signMsgVal = quaiqianorderquery.AppendParam(signMsgVal,"signType",signType); signMsgVal = quaiqianorderquery.AppendParam(signMsgVal,"merchantAcctId",merchantAcctId); signMsgVal = quaiqianorderquery.AppendParam(signMsgVal,"queryType","1"); signMsgVal =quaiqianorderquery. AppendParam(signMsgVal,"queryMode",queryMode); signMsgVal =quaiqianorderquery. AppendParam(signMsgVal,"startTime",startime); signMsgVal =quaiqianorderquery. AppendParam(signMsgVal,"endTime",endtime); signMsgVal =quaiqianorderquery. AppendParam(signMsgVal,"requestPage",requestPage); signMsgVal =quaiqianorderquery. AppendParam(signMsgVal,"orderId",""); signMsgVal =quaiqianorderquery. AppendParam(signMsgVal,"key",key); //默认singtype=1 即按照md5 方式加密 String signMsg = MD5Util.md5Hex(signMsgVal.getBytes()).toUpperCase(); rsq.setSignMsg(signMsg); GatewayOrderQueryServiceLocator locator = new GatewayOrderQueryServiceLocator(); GatewayOrderQuery gservice = null; GatewayOrderQueryResponse queryResponse = null; try { gservice = locator.getgatewayOrderQuery(); queryResponse = gservice.gatewayOrderQuery(rsq); } catch (RemoteException e1) { System.out.println("获取locator对象失败。。"); e1.printStackTrace(); } catch (ServiceException e1) { System.out.println("获取locator对象失败。。"); e1.printStackTrace(); } //一会改!="" if (!queryResponse.getErrCode().equals("")) { System.out.println("查找快钱订单数据出错..错误信息" + getErrormess()); } else { System.out.println("查找因服务器重启而没有同步至系统数据库中的快钱订单开始。。"); java.util.List lg = query .getQuaiQianOrders(rsq, queryResponse, gservice); System.out.println("执行入库操作开始。。。"); QuaiqianOrders qo=null; for (int i = 0; i < lg.size(); i++) { if(lg.get(i)!=null){ qo=new QuaiqianOrders(); GatewayOrderDetail g=lg.get(i); qo.setDealId(g.getDealId()); qo.setDealTime(DateUtil.stringToDate(g.getDealTime(), "yyyyMMddHHmmss") ); qo.setFee(g.getFee()); qo.setOrderAmount(g.getOrderAmount()); qo.setOrderId(g.getOrderId()); qo.setOrdernum(0); qo.setOrderTime(DateUtil.stringToDate(g.getOrderTime(), "yyyyMMddHHmmss")); qo.setPayType(g.getPayType()); qo.setPayAmount(g.getPayAmount()); try { query.insertQuaiqianDate(qo); } catch (SQLException e) { System.out.println("插入数据失败。。"); e.printStackTrace(); } } } } } } } // 初始时候启动定时任务 定时任务中需要这两个业务类 这里把他们的引用传过去 time=new TimerManager(quaiqianorderquery,query); System.out.println("定时任务启动。。。"); } }


2.. listenter

package com.rc.portal.service.impl;

import java.rmi.RemoteException;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.xml.rpc.ServiceException;

import org.springframework.web.context.support.WebApplicationContextUtils;

import md5.MD5Util;

import com._99bill.sandbox.apipay.services.gatewayOrderQuery.GatewayOrderQuery;
import com._99bill.sandbox.apipay.services.gatewayOrderQuery.GatewayOrderQueryServiceLocator;
import com.bill99.seashell.domain.dto.gatewayquery.GatewayOrderDetail;
import com.bill99.seashell.domain.dto.gatewayquery.GatewayOrderQueryRequest;
import com.bill99.seashell.domain.dto.gatewayquery.GatewayOrderQueryResponse;
import com.rc.commons.util.DateUtil;
import com.rc.portal.dao.QuaiqianOrderQueryDao;
import com.rc.portal.dao.impl.QuaiqianOrderQueryDaoImpl;
import com.rc.portal.service.QuaiQianOrderQueryManage;
import com.rc.portal.vo.QuaiqianOrders;
import com.rc.portal.webapp.util.CommonParam;
import com.rc.portal.webapp.util.TimerManager;
import com.sun.xml.internal.bind.v2.schemagen.xmlschema.List;

public class KuaiqianTaskListener implements ServletContextListener {
	TimerManager time=null;
	ServletContext context;
	org.springframework.context.ApplicationContext ctx; 
 
	private QuaiQianOrderQueryManage quaiqianorderquery;
	private QuaiqianOrderQueryDao query ;
	private String Errormess;

	public String getErrormess() {
		return Errormess;
	}

	public void setErrormess(String errormess) {
		Errormess = errormess;
	}

	public QuaiQianOrderQueryManage getQuaiqianorderquery() {
		return quaiqianorderquery;
	}

	public void setQuaiqianorderquery(
			QuaiQianOrderQueryManage quaiqianorderquery) {
		this.quaiqianorderquery = quaiqianorderquery;
	}

	public QuaiqianOrderQueryDao getQuery() {
		return query;
	}

	public void setQuery(QuaiqianOrderQueryDao query) {
		this.query = query;
	}

	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		// TODO Auto-generated method stub
		 System.out.println("我先执行。。。");
		 // 监听器服务启动时调用此方法contextInitialized()其他和servlet一样
                   context = arg0.getServletContext();   
		 ctx= WebApplicationContextUtils.getWebApplicationContext(context);
		 query=(QuaiqianOrderQueryDaoImpl)ctx.getBean("query");
		 quaiqianorderquery=(QuaiQianOrderQueryImpl)ctx.getBean("quaiqianorderquery");
		
		 kuaiQianOrderToSysDatabase();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @seejavax.servlet.ServletContextListener#contextDestroyed(javax.servlet.
	 * ServletContextEvent)
	 */
	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
            time.timer.cancel();
            System.out.println("停止了timer");
	}

	

	private void kuaiQianOrderToSysDatabase() {
		// TODO Auto-generated method stub

		Calendar calendar = Calendar.getInstance();
		Calendar upcalendar = Calendar.getInstance();
		CommonParam param = new CommonParam();

	
		Integer count = 0;
		try {
			count = query.getKquaiqinaOrdersCount();
		} catch (SQLException e2) {
			System.out.println("得到快钱订单数量失败。。");
			e2.printStackTrace();
		}
//		if (count > 0) {
//			QuaiqianOrders q = null;
//			try {
//				//q = query.getlastDate();
//			} catch (SQLException e2) {
//				System.out.println("得到最近的快钱订单时间失败。");
//				e2.printStackTrace();
//			}
			if (q != null) {
				Date d = q.getRecorddate();
				calendar.setTime(d);
				int sysday = calendar.get(Calendar.DATE);
				calendar.setTime(new Date());
				int upday = calendar.get(Calendar.DATE);
				if (sysday + 1 < upday) {
					calendar.setTime(d);
					calendar.add(Calendar.DATE, 1);
					upcalendar.add(Calendar.DATE, -1);
					calendar.set(Calendar.HOUR_OF_DAY, 0);
					calendar.set(Calendar.SECOND, 0);
					calendar.set(Calendar.MINUTE, 0);
					upcalendar.set(Calendar.HOUR_OF_DAY, 23);
					upcalendar.set(Calendar.SECOND, 59);
					upcalendar.set(Calendar.MINUTE, 59);
					String startime = DateUtil.sdfLongTimePlus_s
							.format(calendar.getTime());
					String endtime = DateUtil.sdfLongTimePlus_s
							.format(upcalendar.getTime());

					// 以下参数从配置文件中获取 -- start

					// 人民币账号
					// 本参数用来指定接收款项的快钱用户的人民币账号
					String merchantAcctId = param.getString("merchantAcctId");
					// 客户编号所对应的密钥。。在账户邮箱中获取
					String key = param.getString("key");
					// 字符集 固定值:1 1代表UTF-8
					String inputCharset = param.getString("inputCharset");
					// 查询接口版本 固定值:v2.0注意为小写字母
					String versqion = param.getString("version");
					// 签名类型 固定值:1 1代表MD5加密签名方式
					String signType = param.getString("signType");
					// 查询模式 固定值:1 1代表简单查询(返回基本订单信息)
					String queryMode = param.getString("queryMode");
					// 请求记录集页码 在查询结果数据总量很大时,快钱会将支付结果分多次返回。本参数表示商户需要得到的记录集页码。
					// 默认为1,表示第1页。
					String requestPage = param.getString("requestPage");
					// --end

					// 组装请求参数
					GatewayOrderQueryRequest rsq = new GatewayOrderQueryRequest();
					rsq.setInputCharset(inputCharset);
					rsq.setVersion(versqion);
					rsq.setSignType(Integer.valueOf(signType));
					rsq.setMerchantAcctId(merchantAcctId);
					rsq.setQueryType(1);
					rsq.setQueryMode(1);
					rsq.setStartTime(startime);
					rsq.setOrderId("");
					rsq.setEndTime(endtime);
					rsq.setRequestPage(requestPage);
					// rsq.setStartTime(model.getStartTime());
					// 组合字符串。。必须按照此顺序组串
					String signMsgVal = "";
					signMsgVal = quaiqianorderquery.AppendParam(signMsgVal,
							"inputCharset", inputCharset);
					signMsgVal = quaiqianorderquery.AppendParam(signMsgVal,
							"version", versqion);
					signMsgVal = quaiqianorderquery.AppendParam(signMsgVal,
							"signType", signType);
					signMsgVal = quaiqianorderquery.AppendParam(signMsgVal,
							"merchantAcctId", merchantAcctId);
					signMsgVal = quaiqianorderquery.AppendParam(signMsgVal,
							"queryType", "1");
					signMsgVal = quaiqianorderquery.AppendParam(signMsgVal,
							"queryMode", "1");
					signMsgVal = quaiqianorderquery.AppendParam(signMsgVal,
							"startTime", startime);
					signMsgVal = quaiqianorderquery.AppendParam(signMsgVal,
							"endTime", endtime);
					signMsgVal = quaiqianorderquery.AppendParam(signMsgVal,
							"requestPage", requestPage);
					signMsgVal = quaiqianorderquery.AppendParam(signMsgVal,
							"orderId", "");

					signMsgVal = quaiqianorderquery.AppendParam(signMsgVal,
							"key", key);
					// 默认singtype=1 即按照md5 方式加密
					String signMsg = MD5Util.md5Hex(signMsgVal.getBytes())
							.toUpperCase();
					rsq.setSignMsg(signMsg);
					GatewayOrderQueryServiceLocator locator = new GatewayOrderQueryServiceLocator();
					GatewayOrderQuery gservice = null;
					GatewayOrderQueryResponse queryResponse = null;
					try {
						gservice = locator.getgatewayOrderQuery();
						queryResponse = gservice.gatewayOrderQuery(rsq);
					} catch (RemoteException e1) {
						System.out.println("获取locator对象失败。。");
						e1.printStackTrace();
					} catch (ServiceException e1) {
						System.out.println("获取locator对象失败。。");
						e1.printStackTrace();
					}

					if (queryResponse.getErrCode().equals(
							quaiqianorderquery.accountError)) {
						setErrormess("该账号不存在或已注销");
					}
					if (queryResponse.getErrCode().equals(
							quaiqianorderquery.charsetError)) {
						setErrormess("字符集输入不正确");
					}
					if (queryResponse.getErrCode().equals(
							quaiqianorderquery.currentPageNotExistError)) {
						setErrormess("当前记录的页码不存在");
					}
					if (queryResponse.getErrCode().equals(
							quaiqianorderquery.endTimeError)) {
						setErrormess("查询结束时间不正确");
					}
					if (queryResponse.getErrCode().equals(
							quaiqianorderquery.merchantAcctIdError)) {
						setErrormess("人民币账号格式不正确");
					}
					if (queryResponse.getErrCode().equals(
							quaiqianorderquery.orderForPayNotSuccessError)) {
						setErrormess("订单号对应的交易未成功");
					}
					if (queryResponse.getErrCode().equals(
							quaiqianorderquery.orderId)) {
						setErrormess("商户订单号格式不正确");
					}
					if (queryResponse.getErrCode().equals(
							quaiqianorderquery.orderNotExistError)) {
						setErrormess("商户订单号不存在");
					}
					if (queryResponse.getErrCode().equals(
							quaiqianorderquery.queryEndTimeGtTodayTime)) {
						setErrormess("查询结束时间晚于当前时间");
					}
					if (queryResponse.getErrCode().equals(
							quaiqianorderquery.queryExceptionError)) {
						setErrormess("查询过程异常,请稍后再试");
					}
					if (queryResponse.getErrCode().equals(
							quaiqianorderquery.queryModeError)) {
						setErrormess("查询模式不正确或不存在");
					}
					if (queryResponse.getErrCode().equals(
							quaiqianorderquery.queryResultGtfileScopeError)) {
						setErrormess("查询结果超出能允许的文件范围");
					}
					if (queryResponse.getErrCode().equals(
							quaiqianorderquery.queryStartLtEndTimeError)) {
						setErrormess("开始时间不能再结束时间之后");
					}
					if (queryResponse.getErrCode().equals(
							quaiqianorderquery.singTypeError)) {
						setErrormess("签名类型不正确或不存在");
					}
					if (queryResponse.getErrCode().equals(
							quaiqianorderquery.startTimeError)) {
						setErrormess("查询开始时间不正确");
					}
					if (queryResponse.getErrCode().equals(
							quaiqianorderquery.systemBusy)) {
						setErrormess("系统忙,请稍后再试");
					}
					if (queryResponse.getErrCode().equals(
							quaiqianorderquery.timeInnerNotDateError)) {
						setErrormess("本段时间内无交易记录");
					}
					if (queryResponse.getErrCode().equals(
							quaiqianorderquery.timeInnerNotSuccessDateError)) {
						setErrormess("本段时间内无成功交易记录");
					}
					if (queryResponse.getErrCode().equals(
							quaiqianorderquery.unKnownError)) {
						setErrormess("未知错误");
					}
					if (queryResponse.getErrCode().equals(
							quaiqianorderquery.versionError)) {
						setErrormess("网关版本号不正确或不存在");
					}
					if (queryResponse.getErrCode().equals(
							quaiqianorderquery.queryTimeOnlyIsThirtyDaysError)) {
						setErrormess("允许查询的时间最长为30天");
					}
					if (queryResponse.getErrCode().equals(
							quaiqianorderquery.queryTypeError)) {
						setErrormess("查询方式不正确或不存在");
					}
					if (queryResponse.getErrCode().equals(
							quaiqianorderquery.singMsgError)) {
						setErrormess("签名字符串不匹配");
					}
					if (queryResponse.getErrCode().equals(
							quaiqianorderquery.singMsgPowerError)) {
						setErrormess("签名字符串不匹配,你无权查询");
					}

					if (queryResponse.getErrCode() != "") {
						System.out.println("查找快钱订单数据出错..错误信息" + getErrormess());
					} else {
						System.out.println("查找因服务器重启而没有同步至系统数据库中的快钱订单开始。。");
						java.util.List lg = query
								.getQuaiQianOrders(rsq, queryResponse, gservice);
						System.out.println("执行入库操作开始。。。");

						for (int i = 0; i < lg.size(); i++) {
							try {
							//	query.insertQuaiqianDate(lg.get(i));
							} catch (SQLException e) {
								System.out.println("插入数据失败。。");
								e.printStackTrace();
							}
						}
					}

				}
			}

		}
                  // 启动定时任务	
                  time=new TimerManager(quaiqianorderquery,query);
		System.out.println("定时任务启动。。。");
	
	}

}


定时任务类TimerManager

package com.rc.portal.webapp.util;

import java.util.Calendar;
import java.util.Date;
import java.util.Timer;

import com.rc.portal.dao.QuaiqianOrderQueryDao;
import com.rc.portal.service.QuaiQianOrderQueryManage;

public class TimerManager {
 
 //时间间隔
   private static final long PERIOD_DAY = 24 * 60 * 60 * 1000;
   public Timer timer=null;
  
   public TimerManager( QuaiQianOrderQueryManage quaiqianorderquery,QuaiqianOrderQueryDao query) {
   Calendar calendar = Calendar.getInstance(); 
        
   /*** 定制每日0:00执行方法 ***/ 

   calendar.set(Calendar.HOUR_OF_DAY, 0);
   calendar.set(Calendar.MINUTE, 0);
   calendar.set(Calendar.SECOND, 0);
   
   Date date=calendar.getTime(); //第一次执行定时任务的时间
   
   //如果第一次执行定时任务的时间 小于 当前的时间
   //此时要在 第一次执行定时任务的时间 加一天,以便此任务在下个时间点执行。如果不加一天,任务会立即执行。
//   if (date.before(new Date())) {
//       date = this.addDay(date, 1);
//   }   
  
    timer = new Timer();
   // 定义一个任务执行类
   KuanqianDataTimerTask task = new KuanqianDataTimerTask();
   task.setQuaiqianorderquery(quaiqianorderquery);
   task.setQuery(query);
   //安排指定的任务在指定的时间开始进行重复的固定延迟执行  (到时间执行task实例的run()方法)。
   timer.schedule(task,date,PERIOD_DAY);  // 这是指定时间点了 到这个点执行每过ERIOD_DAY到这个点都执行  如果 timer.schedule(task,0,PERIOD_DAY);  服务启动0秒后执行(可以自由设置时间只要不是时间点是秒数就ok) 每隔PERIOD_DAY时间执行一次
  }

  // 增加或减少天数
  public Date addDay(Date date, int num) {
   Calendar startDT = Calendar.getInstance();
   startDT.setTime(date);
   startDT.add(Calendar.DAY_OF_MONTH, num);
   return startDT.getTime();
  }
//  在 TimerManager 这个类里面,大家一定要注意 时间点的问题。如果你设定在凌晨2点执行任务。但你是在2点以后
//  发布的程序或是重启过服务,那这样的情况下,任务会立即执行,而不是等到第二天的凌晨2点执行。为了,避免这种情况
//  发生,只能判断一下,如果发布或重启服务的时间晚于定时执行任务的时间,就在此基础上加一天。

 


}


任务类:KuanqianDataTimerTask

package com.rc.portal.webapp.util;

import java.rmi.RemoteException;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;
import java.util.TimerTask;

import javax.xml.rpc.ServiceException;

import md5.MD5Util;

import com._99bill.sandbox.apipay.services.gatewayOrderQuery.GatewayOrderQuery;
import com._99bill.sandbox.apipay.services.gatewayOrderQuery.GatewayOrderQueryServiceLocator;
import com.bill99.seashell.domain.dto.gatewayquery.GatewayOrderDetail;
import com.bill99.seashell.domain.dto.gatewayquery.GatewayOrderQueryRequest;
import com.bill99.seashell.domain.dto.gatewayquery.GatewayOrderQueryResponse;
import com.rc.commons.util.DateUtil;
import com.rc.portal.dao.QuaiqianOrderQueryDao;
import com.rc.portal.service.QuaiQianOrderQueryManage;
import com.rc.portal.vo.QuaiqianOrders;


public class KuanqianDataTimerTask extends TimerTask {
	private QuaiQianOrderQueryManage quaiqianorderquery;
	private QuaiqianOrderQueryDao query ;
	private String Errormess;

	public String getErrormess() {
		return Errormess;
	}

	public void setErrormess(String errormess) {
		Errormess = errormess;
	}

	public QuaiQianOrderQueryManage getQuaiqianorderquery() {
		return quaiqianorderquery;
	}

	public void setQuaiqianorderquery(
			QuaiQianOrderQueryManage quaiqianorderquery) {
		this.quaiqianorderquery = quaiqianorderquery;
	}

	public QuaiqianOrderQueryDao getQuery() {
		return query;
	}

	public void setQuery(QuaiqianOrderQueryDao query) {
		this.query = query;
	}
	
	
	@Override
	 public void run() {
	  try {
		 System.out.println("服务器启动开始执行入库当天数据。。");
		kuaiQianOrdertoSysDatabase();
	   
	  } catch (Exception e) {
	        System.out.println("run返回异常。");
	        e.printStackTrace();
	  }
	 }
	
	public void kuaiQianOrdertoSysDatabase() {

		// TODO Auto-generated method stub
        System.out.println("开始执行当天数据的入库。。。");
//		Calendar calendar = Calendar.getInstance();
//		Calendar upcalendar = Calendar.getInstance();
		CommonParam param = new CommonParam(); // 
//		calendar.setTime(new Date());
//		upcalendar.setTime(new Date());
//		calendar.add(Calendar.DATE, -1);
//		upcalendar.add(Calendar.DATE, -1);
//		 calendar.set(Calendar.HOUR_OF_DAY, 0);
//		  calendar.set(Calendar.MINUTE, 0);
//		  calendar.set(Calendar.SECOND, 0);
//		upcalendar.set(Calendar.HOUR_OF_DAY, 23);
//		upcalendar.set(Calendar.SECOND, 59);
//		upcalendar.set(Calendar.MINUTE, 59);
//		System.out.println(calendar.getTime());
//		System.out.println(upcalendar.getTime());
//        String startime = DateUtil.sdfLongTimePlus_s.format(calendar.getTime());
//		String endtime = DateUtil.sdfLongTimePlus_s
//				.format(upcalendar.getTime());
      //测试时间
		 
		 String startime="20130413000000";
		 String endtime="20130413235959";
		
			//人民币账号
			//本参数用来指定接收款项的快钱用户的人民币账号 
			String merchantAcctId="1001213884201"; 
			//客户编号所对应的密钥。。在账户邮箱中获取
			String key ="5UHQX2G65W4ECF5G";                        
			//字符集  固定值:1 1代表UTF-8 
			String inputCharsqet ="1";
			//查询接口版本   固定值:v2.0注意为小写字母
			String versqion = "v2.0";
			//签名类型   固定值:1  1代表MD5加密签名方式
			String signType ="1";
			//查询模式   固定值:1  1代表简单查询(返回基本订单信息)
			String queryMode ="1";
			//请求记录集页码  在查询结果数据总量很大时,快钱会将支付结果分多次返回。本参数表示商户需要得到的记录集页码。
			//默认为1,表示第1页。
			String requestPage ="1";
			GatewayOrderQueryRequest rsq=new GatewayOrderQueryRequest();
 	    rsq.setInputCharset(inputCharsqet);
 	    rsq.setVersion(versqion);
 	    rsq.setSignType(Integer.valueOf(signType));
 	    rsq.setMerchantAcctId(merchantAcctId);
 	    rsq.setQueryType(1);
 	    rsq.setQueryMode(Integer.valueOf(queryMode));
 	   
 	 
 	     rsq.setStartTime(startime);

 	   
 	      rsq.setEndTime(endtime);
   	   
 	    
 	    rsq.setRequestPage(requestPage);
 	
 		//组合字符串。。必须按照此顺序组串
 		String signMsgVal="";
 		signMsgVal = quaiqianorderquery.AppendParam(signMsgVal,"inputCharset",inputCharsqet);
 		signMsgVal = quaiqianorderquery.AppendParam(signMsgVal,"version",versqion);
 		signMsgVal = quaiqianorderquery.AppendParam(signMsgVal,"signType",signType);
 		signMsgVal = quaiqianorderquery.AppendParam(signMsgVal,"merchantAcctId",merchantAcctId);
 		signMsgVal = quaiqianorderquery.AppendParam(signMsgVal,"queryType","1");
 		signMsgVal =quaiqianorderquery. AppendParam(signMsgVal,"queryMode",queryMode);
 		signMsgVal =quaiqianorderquery. AppendParam(signMsgVal,"startTime",startime);
 	    signMsgVal =quaiqianorderquery. AppendParam(signMsgVal,"endTime",endtime);
 		signMsgVal =quaiqianorderquery. AppendParam(signMsgVal,"requestPage",requestPage);
 	    signMsgVal =quaiqianorderquery. AppendParam(signMsgVal,"orderId","");
 		
 		signMsgVal =quaiqianorderquery. AppendParam(signMsgVal,"key",key);
         //默认singtype=1  即按照md5 方式加密
 		String signMsg = MD5Util.md5Hex(signMsgVal.getBytes()).toUpperCase();
 		rsq.setSignMsg(signMsg);
		
		GatewayOrderQueryServiceLocator locator = new GatewayOrderQueryServiceLocator();
		GatewayOrderQuery gservice = null;
		GatewayOrderQueryResponse queryResponse = null;
		try {
			gservice = locator.getgatewayOrderQuery();
			queryResponse = gservice.gatewayOrderQuery(rsq);
		} catch (RemoteException e1) {
			System.out.println("获取locator对象失败。。");
			e1.printStackTrace();
		} catch (ServiceException e1) {
			System.out.println("获取locator对象失败。。");
			e1.printStackTrace();
		}

		if (queryResponse.getErrCode().equals(quaiqianorderquery.accountError)) {
			setErrormess("该账号不存在或已注销");
		}
		if (queryResponse.getErrCode().equals(quaiqianorderquery.charsetError)) {
			setErrormess("字符集输入不正确");
		}
		if (queryResponse.getErrCode().equals(
				quaiqianorderquery.currentPageNotExistError)) {
			setErrormess("当前记录的页码不存在");
		}
		if (queryResponse.getErrCode().equals(quaiqianorderquery.endTimeError)) {
			setErrormess("查询结束时间不正确");
		}
		if (queryResponse.getErrCode().equals(
				quaiqianorderquery.merchantAcctIdError)) {
			setErrormess("人民币账号格式不正确");
		}
		if (queryResponse.getErrCode().equals(
				quaiqianorderquery.orderForPayNotSuccessError)) {
			setErrormess("订单号对应的交易未成功");
		}
		if (queryResponse.getErrCode().equals(quaiqianorderquery.orderId)) {
			setErrormess("商户订单号格式不正确");
		}
		if (queryResponse.getErrCode().equals(
				quaiqianorderquery.orderNotExistError)) {
			setErrormess("商户订单号不存在");
		}
		if (queryResponse.getErrCode().equals(
				quaiqianorderquery.queryEndTimeGtTodayTime)) {
			setErrormess("查询结束时间晚于当前时间");
		}
		if (queryResponse.getErrCode().equals(
				quaiqianorderquery.queryExceptionError)) {
			setErrormess("查询过程异常,请稍后再试");
		}
		if (queryResponse.getErrCode()
				.equals(quaiqianorderquery.queryModeError)) {
			setErrormess("查询模式不正确或不存在");
		}
		if (queryResponse.getErrCode().equals(
				quaiqianorderquery.queryResultGtfileScopeError)) {
			setErrormess("查询结果超出能允许的文件范围");
		}
		if (queryResponse.getErrCode().equals(
				quaiqianorderquery.queryStartLtEndTimeError)) {
			setErrormess("开始时间不能再结束时间之后");
		}
		if (queryResponse.getErrCode().equals(quaiqianorderquery.singTypeError)) {
			setErrormess("签名类型不正确或不存在");
		}
		if (queryResponse.getErrCode()
				.equals(quaiqianorderquery.startTimeError)) {
			setErrormess("查询开始时间不正确");
		}
		if (queryResponse.getErrCode().equals(quaiqianorderquery.systemBusy)) {
			setErrormess("系统忙,请稍后再试");
		}
		if (queryResponse.getErrCode().equals(
				quaiqianorderquery.timeInnerNotDateError)) {
			setErrormess("本段时间内无交易记录");
		}
		if (queryResponse.getErrCode().equals(
				quaiqianorderquery.timeInnerNotSuccessDateError)) {
			setErrormess("本段时间内无成功交易记录");
		}
		if (queryResponse.getErrCode().equals(quaiqianorderquery.unKnownError)) {
			setErrormess("未知错误");
		}
		if (queryResponse.getErrCode().equals(quaiqianorderquery.versionError)) {
			setErrormess("网关版本号不正确或不存在");
		}
		if (queryResponse.getErrCode().equals(
				quaiqianorderquery.queryTimeOnlyIsThirtyDaysError)) {
			setErrormess("允许查询的时间最长为30天");
		}
		if (queryResponse.getErrCode()
				.equals(quaiqianorderquery.queryTypeError)) {
			setErrormess("查询方式不正确或不存在");
		}
		if (queryResponse.getErrCode().equals(quaiqianorderquery.singMsgError)) {
			setErrormess("签名字符串不匹配");
		}
		if (queryResponse.getErrCode().equals(
				quaiqianorderquery.singMsgPowerError)) {
			setErrormess("签名字符串不匹配,你无权查询");
		}

		if (queryResponse.getErrCode()!="") {
			System.out.println("查找快钱订单数据出错..错误信息" + getErrormess());
		} else {
			System.out.println("查找每一天的快钱订单开始。。");
			java.util.List lg = query.getQuaiQianOrders(
					rsq, queryResponse, gservice);
			System.out.println("执行入库操作开始。。。");
           QuaiqianOrders qo=null;
			for (int i = 0; i < lg.size(); i++) {
				 qo=new QuaiqianOrders();
				 GatewayOrderDetail g=lg.get(i);
				 qo.setDealId(g.getDealId());
				 qo.setDealTime(DateUtil.stringToDate(g.getDealTime(), "yyyyMMddHHmmss") );
				 qo.setFee(g.getFee());
				 qo.setOrderAmount(g.getOrderAmount());
				 qo.setOrderId(g.getOrderId());
				 qo.setOrdernum(0);
				 qo.setOrderTime(DateUtil.stringToDate(g.getOrderTime(), "yyyyMMddHHmmss"));
				 qo.setPayType(g.getPayType());
				 qo.setPayAmount(g.getPayAmount());
				
				 try {
					 query.insertQuaiqianDate(qo);
				} catch (SQLException e) {
					System.out.println("插入数据失败。。");
					e.printStackTrace();
				}
			}
		}
		   System.out.println("开始执行当天数据的入库end。。。");
	}


}


web.xml 配置




	portal project
	
		webAppRootKey
		drugManage.root
	
	
	
		log4jConfigLocation
		/WEB-INF/classes/log4j.properties
	
	
		log4jRefreshInterval
		60000
	

	
	
	
	
		CodeFilter
		com.rc.portal.webapp.filter.CodeFilter
	
	
		CodeFilter
		/j_spring_security_check
	

   !--用servlet方式的配置  actioncom.rc.portal.webapp.action.KuaiQianServlet!-- 让服务器启动的时候加载servlet1   !-- 用listenter方式的配置com.rc.portal.service.impl.KuanqianDataTimerTask
	
		
			org.springframework.web.util.Log4jConfigListener
		
	

	
	
		contextConfigLocation
		
			/WEB-INF/classes/applicationContext-*.xml
		
	

	
		
			org.springframework.web.context.ContextLoaderListener
		
	
 


	
		action
		
			com.opensymphony.webwork.dispatcher.ServletDispatcher
		
		1
	

	
	
		springSecurityFilterChain
		org.springframework.web.filter.DelegatingFilterProxy
	
	
		springSecurityFilterChain
		/*
	

	
		ParameterCleanUp
		
			com.rc.portal.webapp.filter.ParameterFilter
		
	

	
		ParameterCleanUp
		*.htm
	

	
		ParameterCleanUp
		*.action
	

	
		ActionContextCleanUp
		
			com.opensymphony.webwork.dispatcher.ActionContextCleanUp
		
	

	
		ActionContextCleanUp
		*.htm
	

	
		ActionContextCleanUp
		*.action
	
	
	
	
		freemarker
		
			freemarker.ext.servlet.FreemarkerServlet
		
		
		
			TemplatePath
			/
		
		
			NoCache
			true
		
		
			ContentType
			text/html
		

		
		
			template_update_delay
			0
			
		
		
			default_encoding
			UTF-8
		
		
			locale
			zh_CN
		
		
			number_format
			0.##########
		
		1
	

	
		MakeImageServlet
		
			com.rc.app.framework.webapp.servlet.MakeImageServlet
		
	

	
		MakeImageServlet
		/sltMakeImage
	

	
	
		JspSupportServlet
		
			com.opensymphony.webwork.views.JspSupportServlet
		
		1
	
 
	
		action
		*.htm
	

	
		action
		*.action
	


	
 
	
		freemarker
		*.ftl
	
  
	
		60
	

	
		index.jsp
	

	
	
	
		404
		/web/exception/404.jsp
	

	
		500
		/web/exception/404.jsp
	

	
		java.lang.NullPointerException
		/web/exception/404.jsp
	

	
		
			com.opensymphony.xwork.config.ConfigurationException
		
		/web/exception/404.jsp
	

	
		
			freemarker.template.TemplateModelException
		
		/web/exception/404.jsp
	

	
		java.lang.NoClassDefFoundError
		/web/exception/404.jsp
	

	
		java.lang.NumberFormatException
		/web/exception/404.jsp
	


二 利用sping集成的quartz

定时任务实现类

package com.rc.portal.webapp.util;

import java.rmi.RemoteException;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.xml.rpc.ServiceException;

import md5.MD5Util;

import com._99bill.sandbox.apipay.services.gatewayOrderQuery.GatewayOrderQuery;
import com._99bill.sandbox.apipay.services.gatewayOrderQuery.GatewayOrderQueryServiceLocator;
import com.bill99.seashell.domain.dto.gatewayquery.GatewayOrderDetail;
import com.bill99.seashell.domain.dto.gatewayquery.GatewayOrderQueryRequest;
import com.bill99.seashell.domain.dto.gatewayquery.GatewayOrderQueryResponse;
import com.rc.commons.util.DateUtil;
import com.rc.portal.dao.QuaiqianOrderQueryDao;
import com.rc.portal.service.QuaiQianOrderQueryManage;
import com.rc.portal.vo.QuaiqianOrders;

public class KuaiqianTask {

	private QuaiQianOrderQueryManage quaiqianorderquery;
	private QuaiqianOrderQueryDao query;
	private String Errormess;
    //static int searchIndexFlag =0;//由doBuildIndex修改,其他方法不要修改此参数

	public String getErrormess() {
		return Errormess;
	}

	public void setErrormess(String errormess) {
		Errormess = errormess;
	}

	public QuaiQianOrderQueryManage getQuaiqianorderquery() {
		return quaiqianorderquery;
	}

	public void setQuaiqianorderquery(
			QuaiQianOrderQueryManage quaiqianorderquery) {
		this.quaiqianorderquery = quaiqianorderquery;
	}

	public QuaiqianOrderQueryDao getQuery() {
		return query;
	}

	public void setQuery(QuaiqianOrderQueryDao query) {
		this.query = query;
	}

	int count = 0;
    
	public void run() {
		  //定时器同时调用两次的问题需要查找原因  这种问题我感觉是服务器加载两次项目 生成了两个线程 。。 根本原因还是tomcat 问题。
//		  if(searchIndexFlag>0)return ;//保证同一时刻只有一个定时器运行,通过这种方式保证每次定时时间到时,只执行一个线程
//		  searchIndexFlag = 1;//锁定

		System.out.println("执行。。。");
		if (count == 0) {
			kuaiQianOrderToSysDatabase_one();
			count++;
			
		}

		else{
			try {

				System.out.println("服务器启动开始执行入库昨天数据。。");
				kuaiQianOrdertoSysDatabase();

			} catch (Exception e) {
				System.out.println("run返回异常。");
				e.printStackTrace();
			}
	 
		
		}
	}

	private void kuaiQianOrderToSysDatabase_one() {
		// TODO Auto-generated method stub

		Calendar calendar = Calendar.getInstance();
		Calendar upcalendar = Calendar.getInstance();
		CommonParam param = new CommonParam();

		Integer count = 0;
		try {
			count = query.getKquaiqinaOrdersCount();
		} catch (SQLException e2) {
			System.out.println("得到快钱订单数量失败。。");
			e2.printStackTrace();
		}
		if (count > 0) {
			List q = null;
			QuaiqianOrders qs = null;
			try {
				q = query.getlastDate();
				qs = q.get(0);

			} catch (SQLException e2) {
				System.out.println("得到最近的快钱订单时间失败。");
				e2.printStackTrace();
			}
			if (q != null) {
				
				
				Date d = qs.getRecorddate();
				calendar.setTime(d);
		
				upcalendar.setTime(new Date());
			
				int sysday = calendar.get(Calendar.DATE);
				int upday = upcalendar.get(Calendar.DATE);
				if (sysday < upday) {
					 upcalendar.add(Calendar.DATE, -1);
					 calendar.set(Calendar.HOUR_OF_DAY, 0);
					 calendar.set(Calendar.SECOND, 0);
					 calendar.set(Calendar.MINUTE, 0);
					 upcalendar.set(Calendar.HOUR_OF_DAY, 23);
					 upcalendar.set(Calendar.SECOND, 59);
					 upcalendar.set(Calendar.MINUTE, 59);
					 String startime = DateUtil.sdfLongTimePlus_s
					 .format(calendar.getTime());
					 String endtime = DateUtil.sdfLongTimePlus_s
					 .format(upcalendar.getTime());
                     
					 System.out.println("补数据"+startime);
                    
					 System.out.println("补数据"+endtime);
					
				//从配置文件中获取  签名字符串不正确.. 后续找找原因.		
					 // 人民币账号
					// 本参数用来指定接收款项的快钱用户的人民币账号
					String merchantAcctId =param.getString("merchantAcctId");// "1001213884201";
					// 客户编号所对应的密钥。。在账户邮箱中获取
					String key = param.getString("key");// "5UHQX2G65W4ECF5G";
					// 字符集 固定值:1 1代表UTF-8
					String inputCharsqet =param.getString("inputCharset");// "1";
					// 查询接口版本 固定值:v2.0注意为小写字母
					String versqion = param.getString("version");// "v2.0";
					// 签名类型 固定值:1 1代表MD5加密签名方式
					String signType =param.getString("signType");// "1";
					// 查询模式 固定值:1 1代表简单查询(返回基本订单信息)
					String queryMode =param.getString("queryMode");// "1";
					// 请求记录集页码 在查询结果数据总量很大时,快钱会将支付结果分多次返回。本参数表示商户需要得到的记录集页码。
					// 默认为1,表示第1页。
					String requestPage =param.getString("requestPage");//"1";
//					
//					 // 人民币账号
//					// 本参数用来指定接收款项的快钱用户的人民币账号
//					String merchantAcctId ="1001213884201";
//					// 客户编号所对应的密钥。。在账户邮箱中获取
//					String key ="5UHQX2G65W4ECF5G";
//					// 字符集 固定值:1 1代表UTF-8
//					String inputCharsqet ="1";
//					// 查询接口版本 固定值:v2.0注意为小写字母
//					String versqion = "v2.0";
//					// 签名类型 固定值:1 1代表MD5加密签名方式
//					String signType ="1";
//					// 查询模式 固定值:1 1代表简单查询(返回基本订单信息)
//					String queryMode ="1";
//					// 请求记录集页码 在查询结果数据总量很大时,快钱会将支付结果分多次返回。本参数表示商户需要得到的记录集页码。
//					// 默认为1,表示第1页。
//					String requestPage ="1";
					GatewayOrderQueryRequest rsq = new GatewayOrderQueryRequest();
					rsq.setInputCharset(inputCharsqet);
					rsq.setVersion(versqion);
					rsq.setSignType(Integer.valueOf(signType));
					rsq.setMerchantAcctId(merchantAcctId);
					rsq.setQueryType(1);
					rsq.setQueryMode(Integer.valueOf(queryMode));
////
//////					// 测试时间
////////
//					String startime = "20130617000000";
//					String  endtime = "20130617235959";

					rsq.setStartTime(startime);
					rsq.setEndTime(endtime);
					rsq.setRequestPage(requestPage);

					// 组合字符串。。必须按照此顺序组串
					String signMsgVal = "";
					signMsgVal = quaiqianorderquery.AppendParam(signMsgVal,
							"inputCharset", inputCharsqet);
					signMsgVal = quaiqianorderquery.AppendParam(signMsgVal,
							"version", versqion);
					signMsgVal = quaiqianorderquery.AppendParam(signMsgVal,
							"signType", signType);
					signMsgVal = quaiqianorderquery.AppendParam(signMsgVal,
							"merchantAcctId", merchantAcctId);
					signMsgVal = quaiqianorderquery.AppendParam(signMsgVal,
							"queryType", "1");
					signMsgVal = quaiqianorderquery.AppendParam(signMsgVal,
							"queryMode", queryMode);
					signMsgVal = quaiqianorderquery.AppendParam(signMsgVal,
							"startTime", startime);
					signMsgVal = quaiqianorderquery.AppendParam(signMsgVal,
							"endTime", endtime);
					signMsgVal = quaiqianorderquery.AppendParam(signMsgVal,
							"requestPage", requestPage);
					signMsgVal = quaiqianorderquery.AppendParam(signMsgVal,
							"orderId", "");

					signMsgVal = quaiqianorderquery.AppendParam(signMsgVal,
							"key", key);
					// 默认singtype=1 即按照md5 方式加密
					String signMsg = MD5Util.md5Hex(signMsgVal.getBytes())
							.toUpperCase();
					rsq.setSignMsg(signMsg);
					GatewayOrderQueryServiceLocator locator = new GatewayOrderQueryServiceLocator();
					GatewayOrderQuery gservice = null;
					GatewayOrderQueryResponse queryResponse = null;
					try {
						gservice = locator.getgatewayOrderQuery();
						queryResponse = gservice.gatewayOrderQuery(rsq);
					} catch (RemoteException e1) {
						System.out.println("获取locator对象失败。。");
						e1.printStackTrace();
					} catch (ServiceException e1) {
						System.out.println("获取locator对象失败。。");
						e1.printStackTrace();
					}
					if (queryResponse.getErrCode().equals(quaiqianorderquery.accountError)) {
						setErrormess("该账号不存在或已注销");
					}
					if (queryResponse.getErrCode().equals(quaiqianorderquery.charsetError)) {
						setErrormess("字符集输入不正确");
					}
					if (queryResponse.getErrCode().equals(
							quaiqianorderquery.currentPageNotExistError)) {
						setErrormess("当前记录的页码不存在");
					}
					if (queryResponse.getErrCode().equals(quaiqianorderquery.endTimeError)) {
						setErrormess("查询结束时间不正确");
					}
					if (queryResponse.getErrCode().equals(
							quaiqianorderquery.merchantAcctIdError)) {
						setErrormess("人民币账号格式不正确");
					}
					if (queryResponse.getErrCode().equals(
							quaiqianorderquery.orderForPayNotSuccessError)) {
						setErrormess("订单号对应的交易未成功");
					}
					if (queryResponse.getErrCode().equals(quaiqianorderquery.orderId)) {
						setErrormess("商户订单号格式不正确");
					}
					if (queryResponse.getErrCode().equals(
							quaiqianorderquery.orderNotExistError)) {
						setErrormess("商户订单号不存在");
					}
					if (queryResponse.getErrCode().equals(
							quaiqianorderquery.queryEndTimeGtTodayTime)) {
						setErrormess("查询结束时间晚于当前时间");
					}
					if (queryResponse.getErrCode().equals(
							quaiqianorderquery.queryExceptionError)) {
						setErrormess("查询过程异常,请稍后再试");
					}
					if (queryResponse.getErrCode()
							.equals(quaiqianorderquery.queryModeError)) {
						setErrormess("查询模式不正确或不存在");
					}
					if (queryResponse.getErrCode().equals(
							quaiqianorderquery.queryResultGtfileScopeError)) {
						setErrormess("查询结果超出能允许的文件范围");
					}
					if (queryResponse.getErrCode().equals(
							quaiqianorderquery.queryStartLtEndTimeError)) {
						setErrormess("开始时间不能再结束时间之后");
					}
					if (queryResponse.getErrCode().equals(quaiqianorderquery.singTypeError)) {
						setErrormess("签名类型不正确或不存在");
					}
					if (queryResponse.getErrCode()
							.equals(quaiqianorderquery.startTimeError)) {
						setErrormess("查询开始时间不正确");
					}
					if (queryResponse.getErrCode().equals(quaiqianorderquery.systemBusy)) {
						setErrormess("系统忙,请稍后再试");
					}
					if (queryResponse.getErrCode().equals(
							quaiqianorderquery.timeInnerNotDateError)) {
						setErrormess("本段时间内无交易记录");
					}
					if (queryResponse.getErrCode().equals(
							quaiqianorderquery.timeInnerNotSuccessDateError)) {
						setErrormess("本段时间内无成功交易记录");
					}
					if (queryResponse.getErrCode().equals(quaiqianorderquery.unKnownError)) {
						setErrormess("未知错误");
					}
					if (queryResponse.getErrCode().equals(quaiqianorderquery.versionError)) {
						setErrormess("网关版本号不正确或不存在");
					}
					if (queryResponse.getErrCode().equals(
							quaiqianorderquery.queryTimeOnlyIsThirtyDaysError)) {
						setErrormess("允许查询的时间最长为30天");
					}
					if (queryResponse.getErrCode()
							.equals(quaiqianorderquery.queryTypeError)) {
						setErrormess("查询方式不正确或不存在");
					}
					if (queryResponse.getErrCode().equals(quaiqianorderquery.singMsgError)) {
						setErrormess("签名字符串不匹配");
					}
					if (queryResponse.getErrCode().equals(
							quaiqianorderquery.singMsgPowerError)) {
						setErrormess("签名字符串不匹配,你无权查询");
					}
					// 一会改!=""
					if (!queryResponse.getErrCode().equals("")) {
						System.out.println("查找快钱订单数据出错..错误信息  " + getErrormess());
					} else {
						System.out.println("查找因服务器重启而没有同步至系统数据库中的快钱订单开始。。");
						java.util.List lg = query
								.getQuaiQianOrders(rsq, queryResponse, gservice);
						System.out.println("执行入库操作开始。。。");
						QuaiqianOrders qo = null;
						for (int i = 0; i < lg.size(); i++) {
							if (lg.get(i) != null) {

								qo = new QuaiqianOrders();
								GatewayOrderDetail g = lg.get(i);
								qo.setDealId(g.getDealId());
							
								
								try {
									
									qo.setDealTime(DateUtil.fromStringToDate("yyyyMMddHHmmss",g.getDealTime()));
									qo.setOrderTime(DateUtil.fromStringToDate("yyyyMMddHHmmss",g.getDealTime()));
								
								} catch (ParseException e1) {
									System.out.println("转换出错了");
									// TODO Auto-generated catch block
									e1.printStackTrace();
								}
								
								qo.setFee(g.getFee()/100);
								qo.setOrderAmount(g.getOrderAmount()/100);
								qo.setOrderId(g.getOrderId());
								qo.setOrdernum(0);
							
								qo.setPayType(g.getPayType());
								qo.setPayAmount(g.getPayAmount()/100);

								try {
									query.insertQuaiqianDate(qo);
								} catch (SQLException e) {
									System.out.println("插入数据失败。。");
									e.printStackTrace();
								}
							}
						}
					}

				}
				
		}
			}

		
	}

	public void kuaiQianOrdertoSysDatabase() {

		// TODO Auto-generated method stub
	System.out.println("开始执行当天数据的入库。。。");
		 Calendar calendar = Calendar.getInstance();
		 Calendar upcalendar = Calendar.getInstance();
		 CommonParam param = new CommonParam(); // 
		 calendar.setTime(new Date());
		 upcalendar.setTime(new Date());
		 calendar.add(Calendar.DATE, -1);
		 upcalendar.add(Calendar.DATE, -1);
		 calendar.set(Calendar.HOUR_OF_DAY, 0);
		 calendar.set(Calendar.MINUTE, 0);
		 calendar.set(Calendar.SECOND, 0);
		 upcalendar.set(Calendar.HOUR_OF_DAY, 23);
		 upcalendar.set(Calendar.SECOND, 59);
		 upcalendar.set(Calendar.MINUTE, 59);
		 System.out.println(calendar.getTime());
		 System.out.println(upcalendar.getTime());
		 String startime =
		 DateUtil.sdfLongTimePlus_s.format(calendar.getTime());
		
		 String endtime = DateUtil.sdfLongTimePlus_s
		 .format(upcalendar.getTime());
		 System.out.println(endtime);
		 System.out.println(startime);
		// 测试时间
//
//			String startime = "20130617000000";
//			String endtime = "20130617235959";

			//从配置文件中获取  签名字符串不正确.. 后续找找原因.	 原因已经找到 ,配置文件中的value值多了空格。。	
		 // 人民币账号
		// 本参数用来指定接收款项的快钱用户的人民币账号
		String merchantAcctId =param.getString("merchantAcctId");// "1001213884201";
		// 客户编号所对应的密钥。。在账户邮箱中获取
		String key = param.getString("key");// "5UHQX2G65W4ECF5G";
		// 字符集 固定值:1 1代表UTF-8
		String inputCharsqet =param.getString("inputCharset");// "1";
		// 查询接口版本 固定值:v2.0注意为小写字母
		String versqion = param.getString("version");// "v2.0";
		// 签名类型 固定值:1 1代表MD5加密签名方式
		String signType =param.getString("signType");// "1";
		// 查询模式 固定值:1 1代表简单查询(返回基本订单信息)
		String queryMode =param.getString("queryMode");// "1";
		// 请求记录集页码 在查询结果数据总量很大时,快钱会将支付结果分多次返回。本参数表示商户需要得到的记录集页码。
		// 默认为1,表示第1页。
		String requestPage =param.getString("requestPage");//"1";
		GatewayOrderQueryRequest rsq = new GatewayOrderQueryRequest();
		rsq.setInputCharset(inputCharsqet);
		rsq.setVersion(versqion);
		rsq.setSignType(Integer.valueOf(signType));
		rsq.setMerchantAcctId(merchantAcctId);
		rsq.setQueryType(1);
		rsq.setQueryMode(Integer.valueOf(queryMode));

		rsq.setStartTime(startime);

		rsq.setEndTime(endtime);

		rsq.setRequestPage(requestPage);

		// 组合字符串。。必须按照此顺序组串
		String signMsgVal = "";
		signMsgVal = quaiqianorderquery.AppendParam(signMsgVal, "inputCharset",
				inputCharsqet);
		signMsgVal = quaiqianorderquery.AppendParam(signMsgVal, "version",
				versqion);
		signMsgVal = quaiqianorderquery.AppendParam(signMsgVal, "signType",
				signType);
		signMsgVal = quaiqianorderquery.AppendParam(signMsgVal,
				"merchantAcctId", merchantAcctId);
		signMsgVal = quaiqianorderquery.AppendParam(signMsgVal, "queryType",
				"1");
		signMsgVal = quaiqianorderquery.AppendParam(signMsgVal, "queryMode",
				queryMode);
		signMsgVal = quaiqianorderquery.AppendParam(signMsgVal, "startTime",
				startime);
		signMsgVal = quaiqianorderquery.AppendParam(signMsgVal, "endTime",
				endtime);
		signMsgVal = quaiqianorderquery.AppendParam(signMsgVal, "requestPage",
				requestPage);
		signMsgVal = quaiqianorderquery.AppendParam(signMsgVal, "orderId", "");

		signMsgVal = quaiqianorderquery.AppendParam(signMsgVal, "key", key);
		// 默认singtype=1 即按照md5 方式加密
		String signMsg = MD5Util.md5Hex(signMsgVal.getBytes()).toUpperCase();
		rsq.setSignMsg(signMsg);

		GatewayOrderQueryServiceLocator locator = new GatewayOrderQueryServiceLocator();
		GatewayOrderQuery gservice = null;
		GatewayOrderQueryResponse queryResponse = null;
		try {
			gservice = locator.getgatewayOrderQuery();
			queryResponse = gservice.gatewayOrderQuery(rsq);
		} catch (RemoteException e1) {
			System.out.println("获取locator对象失败。。");
			e1.printStackTrace();
		} catch (ServiceException e1) {
			System.out.println("获取locator对象失败。。");
			e1.printStackTrace();
		}

		if (queryResponse.getErrCode().equals(quaiqianorderquery.accountError)) {
			setErrormess("该账号不存在或已注销");
		}
		if (queryResponse.getErrCode().equals(quaiqianorderquery.charsetError)) {
			setErrormess("字符集输入不正确");
		}
		if (queryResponse.getErrCode().equals(
				quaiqianorderquery.currentPageNotExistError)) {
			setErrormess("当前记录的页码不存在");
		}
		if (queryResponse.getErrCode().equals(quaiqianorderquery.endTimeError)) {
			setErrormess("查询结束时间不正确");
		}
		if (queryResponse.getErrCode().equals(
				quaiqianorderquery.merchantAcctIdError)) {
			setErrormess("人民币账号格式不正确");
		}
		if (queryResponse.getErrCode().equals(
				quaiqianorderquery.orderForPayNotSuccessError)) {
			setErrormess("订单号对应的交易未成功");
		}
		if (queryResponse.getErrCode().equals(quaiqianorderquery.orderId)) {
			setErrormess("商户订单号格式不正确");
		}
		if (queryResponse.getErrCode().equals(
				quaiqianorderquery.orderNotExistError)) {
			setErrormess("商户订单号不存在");
		}
		if (queryResponse.getErrCode().equals(
				quaiqianorderquery.queryEndTimeGtTodayTime)) {
			setErrormess("查询结束时间晚于当前时间");
		}
		if (queryResponse.getErrCode().equals(
				quaiqianorderquery.queryExceptionError)) {
			setErrormess("查询过程异常,请稍后再试");
		}
		if (queryResponse.getErrCode()
				.equals(quaiqianorderquery.queryModeError)) {
			setErrormess("查询模式不正确或不存在");
		}
		if (queryResponse.getErrCode().equals(
				quaiqianorderquery.queryResultGtfileScopeError)) {
			setErrormess("查询结果超出能允许的文件范围");
		}
		if (queryResponse.getErrCode().equals(
				quaiqianorderquery.queryStartLtEndTimeError)) {
			setErrormess("开始时间不能再结束时间之后");
		}
		if (queryResponse.getErrCode().equals(quaiqianorderquery.singTypeError)) {
			setErrormess("签名类型不正确或不存在");
		}
		if (queryResponse.getErrCode()
				.equals(quaiqianorderquery.startTimeError)) {
			setErrormess("查询开始时间不正确");
		}
		if (queryResponse.getErrCode().equals(quaiqianorderquery.systemBusy)) {
			setErrormess("系统忙,请稍后再试");
		}
		if (queryResponse.getErrCode().equals(
				quaiqianorderquery.timeInnerNotDateError)) {
			setErrormess("本段时间内无交易记录");
		}
		if (queryResponse.getErrCode().equals(
				quaiqianorderquery.timeInnerNotSuccessDateError)) {
			setErrormess("本段时间内无成功交易记录");
		}
		if (queryResponse.getErrCode().equals(quaiqianorderquery.unKnownError)) {
			setErrormess("未知错误");
		}
		if (queryResponse.getErrCode().equals(quaiqianorderquery.versionError)) {
			setErrormess("网关版本号不正确或不存在");
		}
		if (queryResponse.getErrCode().equals(
				quaiqianorderquery.queryTimeOnlyIsThirtyDaysError)) {
			setErrormess("允许查询的时间最长为30天");
		}
		if (queryResponse.getErrCode()
				.equals(quaiqianorderquery.queryTypeError)) {
			setErrormess("查询方式不正确或不存在");
		}
		if (queryResponse.getErrCode().equals(quaiqianorderquery.singMsgError)) {
			setErrormess("签名字符串不匹配");
		}
		if (queryResponse.getErrCode().equals(
				quaiqianorderquery.singMsgPowerError)) {
			setErrormess("签名字符串不匹配,你无权查询");
		}

	if (!queryResponse.getErrCode().equals(""))  {
			System.out.println("查找快钱订单数据出错..错误信息  " + getErrormess());
		} else {
			System.out.println("查找每一天的快钱订单开始。。");
			java.util.List lg = query.getQuaiQianOrders(
					rsq, queryResponse, gservice);
			System.out.println("执行入库操作开始。。。");
			QuaiqianOrders qo = null;
			for (int i = 0; i < lg.size(); i++) {
				qo = new QuaiqianOrders();
				GatewayOrderDetail g = lg.get(i);
				qo.setDealId(g.getDealId());
				try {
				
					qo.setDealTime(DateUtil.fromStringToDate("yyyyMMddHHmmss",g.getDealTime()));
					qo.setOrderTime(DateUtil.fromStringToDate("yyyyMMddHHmmss",g.getDealTime()));
				
				} catch (ParseException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				qo.setFee(g.getFee()/100);
				qo.setOrderAmount(g.getOrderAmount()/100);
				qo.setOrderId(g.getOrderId());
				qo.setOrdernum(0);
				
				qo.setPayType(g.getPayType());
				qo.setPayAmount(g.getPayAmount()/100);

				try {
					query.insertQuaiqianDate(qo);
				} catch (SQLException e) {
					System.out.println("插入数据失败。。");
					e.printStackTrace();
				}
			}
		}
		System.out.println("开始执行当天数据的入库end。。。");
	}

}


2. 配置applicationContext-timer_task.xml

 




        
         
       
         
   
        
        
        
            
            
                
            
            
            
                run
            
        
        
        
            
                
            
            
            
                0 0 0 * * ?
            
        
        
        
            
                
                    
                
            
        
	


3. web.xml 中记得加载这个sping文件


		contextConfigLocation
		
			/WEB-INF/classes/applicationContext-*.xml
		
	

	
		
			org.springframework.web.context.ContextLoaderListener
		
	

 

 现在由于某些原因 定时到达后执行两次任务  后续给上解决方案和原因。。。

你可能感兴趣的:(JAVA)