1. 程式人生 > >java rfc 拉取資料

java rfc 拉取資料

1. 連線rfc

 1 package com.simpro.sapapi.util;
 2  
 3 import java.io.File;
 4 import java.io.FileOutputStream;
 5 import java.util.Properties;
 6  
 7 import org.apache.log4j.Logger;
 8 import org.simpro.util.ConfigUtils;
 9 
10 import com.sap.conn.jco.JCoDestination;
11 import com.sap.conn.jco.JCoDestinationManager;
12 import com.sap.conn.jco.JCoException; 13 import com.sap.conn.jco.ext.DestinationDataProvider; 14 15 16 /** 17 * 與SAP連線配置 18 * @author jay 19 */ 20 public class SAPConn { 21 private static final String ABAP_AS_POOLED = "ABAP_AS_WITH_POOL"; 22 static{ 23 Properties connectProperties = new
Properties(); 24 25 26 connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "10.1.199.10");//伺服器 27 connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, "00"); //系統編號 28 connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "300"); //
SAP集團 29 // connectProperties.setProperty(DestinationDataProvider.JCO_USER, "SD04"); //SAP使用者名稱 30 // connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "654321"); //密碼 31 connectProperties.setProperty(DestinationDataProvider.JCO_USER, "abap07"); //SAP使用者名稱 32 connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "Qq123456"); //密碼 33 connectProperties.setProperty(DestinationDataProvider.JCO_LANG, "zh"); //登入語言 34 connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "3"); //最大連線數 35 connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, "10"); //最大連線執行緒 36 37 createDataFile(ABAP_AS_POOLED, "jcoDestination", connectProperties); 38 } 39 40 /** 41 * 建立SAP介面屬性檔案。 42 * @param name ABAP管道名稱 43 * @param suffix 屬性檔案字尾 44 * @param properties 屬性檔案內容 45 */ 46 private static void createDataFile(String name, String suffix, Properties properties){ 47 File cfg = new File(name+"."+suffix); 48 if(cfg.exists()){ 49 cfg.deleteOnExit(); 50 } 51 try{ 52 FileOutputStream fos = new FileOutputStream(cfg, false); 53 properties.store(fos, "for tests only !"); 54 fos.close(); 55 }catch (Exception e){ 56 log.error("Create Data file fault, error msg: " + e.toString()); 57 throw new RuntimeException("Unable to create the destination file " + cfg.getName(), e); 58 } 59 } 60 61 /** 62 * 獲取SAP連線 63 * @return SAP連線物件 64 */ 65 public static JCoDestination connect(){ 66 JCoDestination destination =null; 67 try { 68 destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED); 69 } catch (JCoException e) { 70 log.error("Connect SAP fault, error msg: " + e.toString()); 71 } 72 return destination; 73 } 74 private static Logger log = Logger.getLogger(SAPConn.class); // 初始化日誌物件 75 }

2,封裝讀取資料  util, 本util只針對於 rfc  入參形式為 table 形式  其他的晚點寫。   

 1 package com.simpro.sapapi.util;
 2 
 3 import java.util.ArrayList;
 4 import java.util.HashMap;
 5 import java.util.List;
 6 import java.util.Map;
 7 
 8 import org.hibernate.mapping.Array;
 9 
10 import com.sap.conn.jco.JCoDestination;
11 import com.sap.conn.jco.JCoException;
12 import com.sap.conn.jco.JCoFunction;
13 import com.sap.conn.jco.JCoMetaData;
14 import com.sap.conn.jco.JCoParameterList;
15 import com.sap.conn.jco.JCoRecord;
16 import com.sap.conn.jco.JCoRuntimeException;
17 import com.sap.conn.jco.JCoTable;
18 import com.simpro.sapapi.SapRequest;
19 
20 public class SapRfcUtil{
21     
22     private static JCoFunction function = null;
23     private static JCoDestination destination =null;
24 
25     private SapRfcUtil(JCoFunction function) {
26         super();
27         this.function = function;
28     }
29     
30     public SapRfcUtil() {
31         super();
32         // TODO Auto-generated constructor stub
33     }
34 
35     public static SapRfcUtil newInstance(){
36         return new SapRfcUtil();
37     }
38     
39     public  SapRfcUtil setFunction(String functionName){
40         try {
41             destination = SAPConn.connect();
42             function = destination.getRepository().getFunction(functionName);
43         } catch (JCoException e) {
44             // TODO Auto-generated catch block
45             e.printStackTrace();
46         }
47         return this;
48     }
49     
50     public  SapRfcUtil setParameterTable(String tableName,String[] values){
51         JCoTable codes = function.getImportParameterList().getTable(tableName);
52         for(int i = 0;i<values.length;i++){
53             codes.appendRow();
54             codes.setRow(i);
55             codes.setValue(0, values[i]);
56         }
57         return this;
58     }
59     
60     
61     public  List<Map> execute(String[] OutParameters){
62         try {
63             function.execute(destination);
64         } catch (JCoException e) {
65             // TODO Auto-generated catch block
66             e.printStackTrace();
67         }
68          // 獲取RFC返回的欄位值
69         JCoParameterList exportParamList = function.getExportParameterList();
70         JCoTable table = exportParamList.getTable(0);
71         List<Map>  list = new ArrayList();
72         for (int i = 0; i < table.getNumRows(); i++)  {
73              Map map = new HashMap();
74             for(int j = 0;j<OutParameters.length;j++){
75                 //為行設定值;
76                 table.setRow(i);
77                 try {
78                 map.put(OutParameters[j], table.getString(OutParameters[j]));
79             } catch (JCoRuntimeException e1) {
80                 map.put("message", "【"+OutParameters[j]+"】 該輸出引數錯誤!");
81                 list.add(map);
82                 return list;
83             }
84             }
85             list.add(map);
86         }
87         return list;
88     }
89 }

3, test 使用方法。

1 List<Map> list = SapRfcUtil.newInstance()
2         .setFunction("ZFI_GET_UKM_MASSDATA")     //rfc 函式名
3         .setParameterTable("I_PARTNERS", new String[]{"0000000001","0000100109"})  //rfc 入參table 型別
4         .setParameterTable("I_SGMNTS", new String[]{"Z001"})
5         .execute(new String[]{"PARTNER","DESCRIP","CREDIT_SGMNT","AMOUNT","CREDIT_LIMIT","CREDIT_LIMIT_USEDW"}); //返回值的欄位。
6         System.out.println(list.toString());

4 結果: