一个完整详细的项目Demojava连接RFC综合例子,java调用sap例子,泛微E8,E9调用SAPRFC例子_泛微sap-Java文档类资源-CSDN下载
最近使用java连接RFC,把这个例子保存,以后方便查看。
import java.io.File;
import java.io.FileOutputStream;
import java.util.Properties;
import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoDestinationManager;
import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoParameterList;
import com.sap.conn.jco.ext.DestinationDataProvider;
public class SAPConn {
private static final String ABAP_AS_POOLED = "ABAP_AS_WITH_POOL";
static{
Properties connectProperties = new Properties();
connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "***.**.**.201");//服务器
connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, "**"); //系统编号
connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "***"); //SAP集团
//connectProperties.setProperty(DestinationDataProvider.JCO_SAPROUTER,"/H/**.**.**.**/H/"); //外网使用路由,VPN后不使用
connectProperties.setProperty(DestinationDataProvider.JCO_USER, "****"); //SAP用户名
connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "***"); //密码
connectProperties.setProperty(DestinationDataProvider.JCO_LANG, "ZH"); //登录语言
connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "3"); //最大连接数
connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, "10"); //最大连接线程
createDataFile(ABAP_AS_POOLED, "jcoDestination", connectProperties);
}
/**
* 创建SAP接口属性文件。
* @param name ABAP管道名称
* @param suffix 属性文件后缀
* @param properties 属性文件内容
*/
private static void createDataFile(String name, String suffix, Properties properties){
File cfg = new File(name+"."+suffix);
if(cfg.exists()){
cfg.deleteOnExit();
}
try{
FileOutputStream fos = new FileOutputStream(cfg, false);
properties.store(fos, "for tests only !");
fos.close();
}catch (Exception e){
System.out.println("Create Data file fault, error msg: " + e.toString());
throw new RuntimeException("Unable to create the destination file " + cfg.getName(), e);
}
}
/**
* 获取SAP连接
* @return SAP连接对象
*/
public static JCoDestination connect(){
System.out.println("正在连接至SAP...");
JCoDestination destination =null;
try {
destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);
destination.ping();
System.out.println("已成功建立sap的连接");
} catch (JCoException e) {
System.out.println("Connect SAP fault, error msg: " + e.toString());
}
return destination;
}
//private static Logger log = Logger.getLogger(SAPConn.class); // 初始化日志对象
public static void main(String[] args) {
JCoFunction function = null;
JCoDestination destination = SAPConn.connect();
String sum="";//求和
try {
//调用Z_WS_TEST函数
function = destination.getRepository().getFunction("ZHRCN_IF_TIME_****");
JCoParameterList input = function.getImportParameterList();
//NUM1
input.setValue("NUM1", "10");
//NUM2
input.setValue("NUM2", "8");
function.execute(destination);
sum= function.getExportParameterList().getString("NUM");//调用接口返回值
System.out.println("求和为:" + sum);
}catch (Exception e) {
e.printStackTrace();
}
}
}
public void RFCRUN_0015(List<Map<String,Object>> list,Map<String, Object> pmap) {
JCoFunction function = null;
JCoDestination destination = SAPConn.connect();
JCoTable T_ACCDOCUMENT =null;
boolean check=false;
for(int m=0;m<list.size();m++) {
Map<String,Object> map=list.get(m);
try {
if(!check) {
//调用Z_WS_TEST函数
function = destination.getRepository().getFunction("ZHRCN_IF_***");
//获取传table的参数TableParameterList,ImportParameterList可debug查看参数是否有值
//T_ACCDOCUMENT = function.getTableParameterList().getTable("IT_***");
T_ACCDOCUMENT = function.getImportParameterList().getTable("IT_***");
check=true;
}
JSONObject json = new JSONObject();
T_ACCDOCUMENT.appendRow();//增加一行
for(Entry<String, Object> entry:map.entrySet()){
T_ACCDOCUMENT.setValue(entry.getKey(), entry.getValue());
json.put(entry.getKey(),entry.getValue());
}
T_ACCDOCUMENT.setValue("BEGDA",pmap.get("dtbg").toString());//dtbg
json.put("BEGDA", pmap.get("dtbg").toString());
logger.info(json.toString());
if((m+1)%500==0 || m==(list.size()-1)) {
check=false;
function.execute(destination);
String RTYPE= function.getExportParameterList().getString("RTYPE");//调用接口返回值
String RTMSG= function.getExportParameterList().getString("RTMSG");//调用接口返回值
logger.info(String.valueOf(m+1)+"----"+RTYPE+"----"+RTMSG);
//获取返回的table两参数,TableParameterList、ExportParameterList,可debug查参数
//JCoTable rsultTable = function.getTableParameterList().getTable("ET_P0015");
JCoTable rsultTable = function.getExportParameterList().getTable("ET_P0015");
boolean loopFlag = !rsultTable.isEmpty(); //判断 这张表中有木有数据
while(loopFlag){ //循环获取数据
logger.info(rsultTable.getString("PERNR")+"__"+
rsultTable.getString("BEGDA")+"__"+
rsultTable.getString("LGART")+"__"+
rsultTable.getString("BETRG")+"__"+
rsultTable.getString("TYPE")+"__"+
rsultTable.getString("MSG"));
loopFlag = rsultTable.nextRow(); // 移动到下一行
}
}
}catch (Exception e) {
e.printStackTrace();
}
}
}