码迷,mamicode.com
首页 > 编程语言 > 详细

java rfc 拉取数据

时间:2018-12-29 11:14:11      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:map   tab   最大   服务器   null   nts   nal   taf   pass   

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 结果:

技术分享图片

 

java rfc 拉取数据

标签:map   tab   最大   服务器   null   nts   nal   taf   pass   

原文地址:https://www.cnblogs.com/suibingbing/p/10193763.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!