轮询程序中使用的各种方法:spring调用;http调用;存储过程调用;
来源:互联网 发布:怎么在淘宝上买彩票了 编辑:程序博客网 时间:2024/05/21 09:29
- /**
- 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;
- }
- }
0 0
- 轮询程序中使用的各种方法:spring调用;http调用;存储过程调用;
- 轮询程序中使用的各种方法:spring调用;http调用;存储过程调用;
- 轮询程序中使用的各种方法:spring调用;http调用;存储过程调用;
- Spring Bean中方法调用/http请求调用/存储过程调用实现的工具类
- spring 中使用jdbctemplate调用Oracle的存储过程
- spring中对存储过程的调用
- SQL存储过程(CLR存储过程)或函数中调用.NET程序集中的方法
- spring调用存储过程
- Spring调用存储过程
- spring 调用存储过程
- spring调用存储过程
- java调用的各种存储过程
- Spring Data JPA 直接调用存储过程的方法
- 使用 Spring 框架调用 DB2 存储过程
- 使用Spring Jdbc (4)调用存储过程
- 使用Spring JdbcTemplate调用存储过程
- 使用Spring JdbcTemplate调用存储过程
- 使用Spring JdbcTemplate调用存储过程
- SQL SERVER复习提纲
- codeforces contest 349
- 判断一个字符是否为数字
- UML系列图——用例图(Use Case)
- 广播Broadcast,收发步骤
- 轮询程序中使用的各种方法:spring调用;http调用;存储过程调用;
- 初识PHP(5)
- 四大金融资产管理公司的起起伏伏
- 编译mrcp的教训
- 淘宝网商品SKU系统设计经验分享
- Spark-1.3.1与Hive整合实现查询分析
- OAF 系列教程 二 HelloWorld
- Elasticsearch 权威指南(中文版)
- linux grep命令