轮询程序中使用的各种方法:spring调用;http调用;存储过程调用;

/**
package com.wonders.schedule.util;

import java.io.BufferedReader;

/**
 * @ClassName: ExecUtil
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author zhoushun
 * @date 2012-12-5 下午02:49:26
 * 
 */

public class ExecUtil {
	/** 
	* @Title: nativeExec 
	* @Description: TODO(任务调用 本地) 
	* @param @param t
	* @param @return    设定文件 
	* @return String    返回类型 
	* @throws 
	*/
	public static String nativeExec(TScheduleConfig t) {
		String method = t.getMethod();
		String param = t.getParam();
		String result = "";
		if (method != null && method.indexOf(".") > -1) {
			String className = (method.split("\\."))[0];
			String methodName = (method.split("\\."))[1];
			try {
				Thread.sleep(2000);
				ITaskService task = (ITaskService) SpringBeanUtil
						.getBean(className);
				Class<?> cls = task.getClass();
				Method[] methods = cls.getDeclaredMethods();
				String methodsName = "";
				for (Method m : methods) {
					methodsName += m.getName() + ",";
				}
				if (methodsName.indexOf(methodName) >= 0) {
					// 返回方法名为“testMethod”的一个 Method 对象,后面跟的是该方法参数
					Method callMethod = cls.getMethod(methodName,
							new Class[] { String.class });
					result = (String) callMethod.invoke(task,
							new Object[] { param });

				}
			} catch (Exception e) {
				e.printStackTrace();
				result = "0";
			}
		}

		return result;
	}

	/** 
	* @Title: webExec 
	* @Description: TODO(任务调用 网络) 
	* @param @param t
	* @param @return    设定文件 
	* @return String    返回类型 
	* @throws 
	*/
	public static String webExec(TScheduleConfig t) {
		String method = t.getMethod();
		String param = t.getParam();
		String result = "";
		try {
			URL url = null;
			HttpURLConnection http = null;

			try {
				Thread.sleep(2000);
				url = new URL(method);
				http = (HttpURLConnection) url.openConnection();
				http.setDoInput(true);
				http.setDoOutput(true);
				http.setUseCaches(false);
				http.setConnectTimeout(50000);
				http.setReadTimeout(50000);
				http.setRequestMethod("POST");
				// http.setRequestProperty("Content-Type",
				// "text/xml; charset=UTF-8");
				http.setRequestProperty("Content-Type",
						"application/x-www-form-urlencoded");
				http.connect();
				param = "¶m=" + param;

				OutputStreamWriter osw = new OutputStreamWriter(http
						.getOutputStream(), "utf-8");
				osw.write(param);
				osw.flush();
				osw.close();

				if (http.getResponseCode() == 200) {
					BufferedReader in = new BufferedReader(
							new InputStreamReader(http.getInputStream(),
									"utf-8"));
					String inputLine;
					while ((inputLine = in.readLine()) != null) {
						result += inputLine;
					}
					in.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
				result = "0";
			} finally {
				if (http != null)
					http.disconnect();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	// 返回值为String
	/** 
	* @Title: procedureExec 
	* @Description: TODO(任务调用 数据库) 
	* @param @param t
	* @param @return    设定文件 
	* @return String    返回类型 
	* @throws 
	*/
	@SuppressWarnings("unchecked")
	public static String procedureExec(TScheduleConfig t) {
		final String datasource = t.getDatasource();
		final String method = t.getMethod();
		final String param = t.getParam();
		JdbcTemplate jdbcTemplate = DbUtil.getJdbcTemplate(datasource);
		String result = "";
		try {
			Thread.sleep(2000);
			result = (String) jdbcTemplate.execute(
					new CallableStatementCreator() {
						public CallableStatement createCallableStatement(
								Connection con) throws SQLException {
							String storedProc = "{call " + method + "(?,?)}";// 调用的sql
							CallableStatement cs = con.prepareCall(storedProc);
							cs.setString(1, param);// 设置输入参数的值
							cs.registerOutParameter(2, OracleTypes.VARCHAR);// 注册输出参数的类型
							return cs;
						}
					}, new CallableStatementCallback() {
						public Object doInCallableStatement(CallableStatement cs)
								throws SQLException, DataAccessException {
							cs.execute();
							return cs.getString(2);// 获取输出参数的值
						}
					});
		} catch (Exception e) {
			e.printStackTrace();
			result = "0";
		}
		return result;
	}

	// 返回值为游标 遍历后的List<Map>
	/** 
	* @Title: procedureExec2 
	* @Description: TODO(任务调用数据库) 
	* @param @param t
	* @param @return    游标类型 转换为 list<Map>结构 
	* @return List<Map<String,Object>>    返回类型 
	* @throws 
	*/
	@SuppressWarnings("unchecked")
	public static List<Map<String, Object>> procedureExec2(TScheduleConfig t) {
		String datasource = "";
		final String method = t.getMethod();
		final String param = t.getParam();
		JdbcTemplate jdbcTemplate = DbUtil.getJdbcTemplate(datasource);
		List resultList = null;
		try {
			resultList = (List) jdbcTemplate.execute(
					new CallableStatementCreator() {
						public CallableStatement createCallableStatement(
								Connection con) throws SQLException {
							String storedProc = "{call " + method + "(?,?)}";// 调用的sql
							CallableStatement cs = con.prepareCall(storedProc);
							cs.setString(1, param);// 设置输入参数的值
							cs.registerOutParameter(2, OracleTypes.CURSOR);// 注册输出参数的类型
							return cs;
						}
					}, new CallableStatementCallback() {
						public Object doInCallableStatement(CallableStatement cs)
								throws SQLException, DataAccessException {
							List resultsMap = new ArrayList();
							cs.execute();
							ResultSet rs = (ResultSet) cs.getObject(2);// 获取游标一行的值
							while (rs.next()) {// 转换每行的返回值到Map中
								Map rowMap = new HashMap();
								rowMap.put("param1", rs.getString("param1"));
								rowMap.put("param2", rs.getString("param2"));
								resultsMap.add(rowMap);
							}
							rs.close();
							return resultsMap;
						}
					});
		} catch (Exception e) {
			e.printStackTrace();
		}
		/*
		 * for (int i = 0; i < resultList.size(); i++) { Map rowMap = (Map)
		 * resultList.get(i); String id = rowMap.get("id").toString(); String
		 * name = rowMap.get("name").toString(); System.out.println("id=" + id +
		 * ";name=" + name); }
		 */
		return resultList;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值