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

来源:互联网 发布:怎么在淘宝上买彩票了 编辑:程序博客网 时间:2024/05/21 09:29
[java] view plain copy
print?
  1. /** 
  2. package com.wonders.schedule.util; 
  3.  
  4. import java.io.BufferedReader; 
  5.  
  6. /** 
  7.  * @ClassName: ExecUtil 
  8.  * @Description: TODO(这里用一句话描述这个类的作用) 
  9.  * @author zhoushun 
  10.  * @date 2012-12-5 下午02:49:26 
  11.  *  
  12.  */  
  13.   
  14. public class ExecUtil {  
  15.     /**  
  16.     * @Title: nativeExec  
  17.     * @Description: TODO(任务调用 本地)  
  18.     * @param @param t 
  19.     * @param @return    设定文件  
  20.     * @return String    返回类型  
  21.     * @throws  
  22.     */  
  23.     public static String nativeExec(TScheduleConfig t) {  
  24.         String method = t.getMethod();  
  25.         String param = t.getParam();  
  26.         String result = "";  
  27.         if (method != null && method.indexOf(".") > -1) {  
  28.             String className = (method.split("\\."))[0];  
  29.             String methodName = (method.split("\\."))[1];  
  30.             try {  
  31.                 Thread.sleep(2000);  
  32.                 ITaskService task = (ITaskService) SpringBeanUtil  
  33.                         .getBean(className);  
  34.                 Class<?> cls = task.getClass();  
  35.                 Method[] methods = cls.getDeclaredMethods();  
  36.                 String methodsName = "";  
  37.                 for (Method m : methods) {  
  38.                     methodsName += m.getName() + ",";  
  39.                 }  
  40.                 if (methodsName.indexOf(methodName) >= 0) {  
  41.                     // 返回方法名为“testMethod”的一个 Method 对象,后面跟的是该方法参数  
  42.                     Method callMethod = cls.getMethod(methodName,  
  43.                             new Class[] { String.class });  
  44.                     result = (String) callMethod.invoke(task,  
  45.                             new Object[] { param });  
  46.   
  47.                 }  
  48.             } catch (Exception e) {  
  49.                 e.printStackTrace();  
  50.                 result = "0";  
  51.             }  
  52.         }  
  53.   
  54.         return result;  
  55.     }  
  56.   
  57.     /**  
  58.     * @Title: webExec  
  59.     * @Description: TODO(任务调用 网络)  
  60.     * @param @param t 
  61.     * @param @return    设定文件  
  62.     * @return String    返回类型  
  63.     * @throws  
  64.     */  
  65.     public static String webExec(TScheduleConfig t) {  
  66.         String method = t.getMethod();  
  67.         String param = t.getParam();  
  68.         String result = "";  
  69.         try {  
  70.             URL url = null;  
  71.             HttpURLConnection http = null;  
  72.   
  73.             try {  
  74.                 Thread.sleep(2000);  
  75.                 url = new URL(method);  
  76.                 http = (HttpURLConnection) url.openConnection();  
  77.                 http.setDoInput(true);  
  78.                 http.setDoOutput(true);  
  79.                 http.setUseCaches(false);  
  80.                 http.setConnectTimeout(50000);  
  81.                 http.setReadTimeout(50000);  
  82.                 http.setRequestMethod("POST");  
  83.                 // http.setRequestProperty("Content-Type",  
  84.                 // "text/xml; charset=UTF-8");  
  85.                 http.setRequestProperty("Content-Type",  
  86.                         "application/x-www-form-urlencoded");  
  87.                 http.connect();  
  88.                 param = "¶m=" + param;  
  89.   
  90.                 OutputStreamWriter osw = new OutputStreamWriter(http  
  91.                         .getOutputStream(), "utf-8");  
  92.                 osw.write(param);  
  93.                 osw.flush();  
  94.                 osw.close();  
  95.   
  96.                 if (http.getResponseCode() == 200) {  
  97.                     BufferedReader in = new BufferedReader(  
  98.                             new InputStreamReader(http.getInputStream(),  
  99.                                     "utf-8"));  
  100.                     String inputLine;  
  101.                     while ((inputLine = in.readLine()) != null) {  
  102.                         result += inputLine;  
  103.                     }  
  104.                     in.close();  
  105.                 }  
  106.             } catch (Exception e) {  
  107.                 e.printStackTrace();  
  108.                 result = "0";  
  109.             } finally {  
  110.                 if (http != null)  
  111.                     http.disconnect();  
  112.             }  
  113.         } catch (Exception e) {  
  114.             e.printStackTrace();  
  115.         }  
  116.         return result;  
  117.     }  
  118.   
  119.     // 返回值为String  
  120.     /**  
  121.     * @Title: procedureExec  
  122.     * @Description: TODO(任务调用 数据库)  
  123.     * @param @param t 
  124.     * @param @return    设定文件  
  125.     * @return String    返回类型  
  126.     * @throws  
  127.     */  
  128.     @SuppressWarnings("unchecked")  
  129.     public static String procedureExec(TScheduleConfig t) {  
  130.         final String datasource = t.getDatasource();  
  131.         final String method = t.getMethod();  
  132.         final String param = t.getParam();  
  133.         JdbcTemplate jdbcTemplate = DbUtil.getJdbcTemplate(datasource);  
  134.         String result = "";  
  135.         try {  
  136.             Thread.sleep(2000);  
  137.             result = (String) jdbcTemplate.execute(  
  138.                     new CallableStatementCreator() {  
  139.                         public CallableStatement createCallableStatement(  
  140.                                 Connection con) throws SQLException {  
  141.                             String storedProc = "{call " + method + "(?,?)}";// 调用的sql  
  142.                             CallableStatement cs = con.prepareCall(storedProc);  
  143.                             cs.setString(1, param);// 设置输入参数的值  
  144.                             cs.registerOutParameter(2, OracleTypes.VARCHAR);// 注册输出参数的类型  
  145.                             return cs;  
  146.                         }  
  147.                     }, new CallableStatementCallback() {  
  148.                         public Object doInCallableStatement(CallableStatement cs)  
  149.                                 throws SQLException, DataAccessException {  
  150.                             cs.execute();  
  151.                             return cs.getString(2);// 获取输出参数的值  
  152.                         }  
  153.                     });  
  154.         } catch (Exception e) {  
  155.             e.printStackTrace();  
  156.             result = "0";  
  157.         }  
  158.         return result;  
  159.     }  
  160.   
  161.     // 返回值为游标 遍历后的List<Map>  
  162.     /**  
  163.     * @Title: procedureExec2  
  164.     * @Description: TODO(任务调用数据库)  
  165.     * @param @param t 
  166.     * @param @return    游标类型 转换为 list<Map>结构  
  167.     * @return List<Map<String,Object>>    返回类型  
  168.     * @throws  
  169.     */  
  170.     @SuppressWarnings("unchecked")  
  171.     public static List<Map<String, Object>> procedureExec2(TScheduleConfig t) {  
  172.         String datasource = "";  
  173.         final String method = t.getMethod();  
  174.         final String param = t.getParam();  
  175.         JdbcTemplate jdbcTemplate = DbUtil.getJdbcTemplate(datasource);  
  176.         List resultList = null;  
  177.         try {  
  178.             resultList = (List) jdbcTemplate.execute(  
  179.                     new CallableStatementCreator() {  
  180.                         public CallableStatement createCallableStatement(  
  181.                                 Connection con) throws SQLException {  
  182.                             String storedProc = "{call " + method + "(?,?)}";// 调用的sql  
  183.                             CallableStatement cs = con.prepareCall(storedProc);  
  184.                             cs.setString(1, param);// 设置输入参数的值  
  185.                             cs.registerOutParameter(2, OracleTypes.CURSOR);// 注册输出参数的类型  
  186.                             return cs;  
  187.                         }  
  188.                     }, new CallableStatementCallback() {  
  189.                         public Object doInCallableStatement(CallableStatement cs)  
  190.                                 throws SQLException, DataAccessException {  
  191.                             List resultsMap = new ArrayList();  
  192.                             cs.execute();  
  193.                             ResultSet rs = (ResultSet) cs.getObject(2);// 获取游标一行的值  
  194.                             while (rs.next()) {// 转换每行的返回值到Map中  
  195.                                 Map rowMap = new HashMap();  
  196.                                 rowMap.put("param1", rs.getString("param1"));  
  197.                                 rowMap.put("param2", rs.getString("param2"));  
  198.                                 resultsMap.add(rowMap);  
  199.                             }  
  200.                             rs.close();  
  201.                             return resultsMap;  
  202.                         }  
  203.                     });  
  204.         } catch (Exception e) {  
  205.             e.printStackTrace();  
  206.         }  
  207.         /* 
  208.          * for (int i = 0; i < resultList.size(); i++) { Map rowMap = (Map) 
  209.          * resultList.get(i); String id = rowMap.get("id").toString(); String 
  210.          * name = rowMap.get("name").toString(); System.out.println("id=" + id + 
  211.          * ";name=" + name); } 
  212.          */  
  213.         return resultList;  
  214.     }  
  215. }  

0 0
原创粉丝点击