依赖
<dependency>
<groupId>com.sap</groupId>
<artifactId>sapjco3</artifactId>
<version>3.1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/sapjco3.jar</systemPath>
</dependency>
- systemPath 中指定自备jar
文件准备
工具包
import cn.morimatsu.sync.dto.SyncSapEquipmentDto;
import com.sap.conn.jco.*;
import com.sap.conn.jco.ext.DestinationDataProvider;
import lombok.Data;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.io.File;
import java.io.FileOutputStream;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
/**
* SAP工具
*/
@Component
@ConfigurationProperties(prefix = "jco")
@Data
public class SapUtils {
private static String jcoAshost;
private static String jcoClient;
private static String jcoUser;
private static String jcoPasswd;
private static String jcoSysnr;
private static String jcoLang;
private static String jcoPeakLimit;
private static String jcoPoolCapacity;
private static String FUNCTION_NAME = "XXXXX"; //设备信息查询接口名
private static String TAB_NAME = "XXXX"; //设备信息查询接口返回结果表名
public void setJcoAshost(String jcoAshost) {
SapUtils.jcoAshost = jcoAshost;
}
public void setJcoClient(String jcoClient) {
SapUtils.jcoClient = jcoClient;
}
public void setJcoUser(String jcoUser) {
SapUtils.jcoUser = jcoUser;
}
public void setJcoPasswd(String jcoPasswd) {
SapUtils.jcoPasswd = jcoPasswd;
}
public void setJcoSysnr(String jcoSysnr) {
SapUtils.jcoSysnr = jcoSysnr;
}
public void setJcoLang(String jcoLang) {
SapUtils.jcoLang = jcoLang;
}
public void setJcoPeakLimit(String jcoPeakLimit) {
SapUtils.jcoPeakLimit = jcoPeakLimit;
}
public void setJcoPoolCapacity(String jcoPoolCapacity) {
SapUtils.jcoPoolCapacity = jcoPoolCapacity;
}
/**
* @description: SAP链接
*/
public static JCoDestination getJcoConnection(String functionName) {
Properties connectProperties = new Properties();
connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, jcoAshost);//服务器
connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, jcoClient);//SAP集团
connectProperties.setProperty(DestinationDataProvider.JCO_USER, jcoUser);//SAP用户名
connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, jcoPasswd);//密码
connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, jcoSysnr);//系统编号
connectProperties.setProperty(DestinationDataProvider.JCO_LANG, jcoLang);//登录语言
//使用连接池
//最大活动连接数,0表示无限制
connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, jcoPeakLimit);
//空闲连接数,如果为0,则没有连接池效果,默认为1
connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, jcoPoolCapacity);
String rootPath = System.getProperty("user.dir");
File cfg = new File(rootPath + "/" + functionName + ".jcoDestination");
if (cfg.exists()) {
cfg.deleteOnExit();
}
try {
FileOutputStream fos = new FileOutputStream(cfg, false);
connectProperties.store(fos, "for tests only !");
fos.close();
} catch (Exception e) {
System.out.println("创建数据文件错误,错误消息:{}" + e.toString());
throw new RuntimeException("无法创建目标文件: " + cfg.getName(), e);
}
JCoDestination destination = null;
try {
destination = JCoDestinationManager.getDestination(functionName);
} catch (Exception e) {
e.printStackTrace();
}
return destination;
}
/**
* 设备信息查询
* @param minTime 修改日期-下限 与资产号 其中一项必填
* @param maxTime 修改日期-上限 与资产号 其中一项必填
* @param assetNumber 资产号
*/
public static List<SyncSapEquipmentDto> deviceInformationQuery(LocalDate minTime, LocalDate maxTime, String... assetNumber) {
List<SyncSapEquipmentDto> list = new ArrayList<>();
try {
JCoDestination jcoConnection = getJcoConnection(FUNCTION_NAME);
jcoConnection.ping();
JCoFunction function = jcoConnection.getRepository().getFunction(FUNCTION_NAME);
int i = 0;
//INPUT类型的参数赋值
JCoTable r_chg_date = function.getTableParameterList().getTable("XXXX"); //修改日期 所属表
r_chg_date.appendRow();
r_chg_date.setRow(i++);
r_chg_date.setValue("XXX", DateUtils.parseDate(minTime, DateUtils.DATE_FORMAT_YMD));//修改日期-下限
r_chg_date.setValue("XXX", DateUtils.parseDate(maxTime, DateUtils.DATE_FORMAT_YMD));//修改日期-上限
function.execute(jcoConnection);
final JCoParameterList tableParameterList = function.getTableParameterList();
//获取SAP返回的表数据,ET_DATA是表名
JCoTable et_data = tableParameterList.getTable(TAB_NAME);
// log.info("et_data="+et_data.toXML());
int numRows = et_data.getNumRows(); //返回数据行数
for (int j = 0; j < numRows; j++) {
//读取数据,第一行的下标是0
et_data.setRow(j);
SyncSapEquipmentDto dto = new SyncSapEquipmentDto();
dto.setGgxh(et_data.getString("GGXH"));
dto.setSdate(et_data.getDate("SDATE"));
list.add(dto);
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
}
配置文件
# 对接 SAP 配置
jco:
jcoAshost: localhost
jcoClient: 300
jcoUser: user
jcoPasswd: passwd
jcoSysnr: 00
jcoLang: ZH
jcoPeakLimit: 10
jcoPoolCapacity: 3
遇到的问题
RFC接口调用SAP如果有异常会通过com.sap.mw.jco.JCOKaTeX parse error: Expected 'EOF', got '#' at position 101: …LOGON_FAILURE: #̲#.#####,#### (1…Exception: (102) RFC_ERROR_COMMUNICATION: Connect to SAP gateway failed
组权限访问 server文件没更新.
(102) RFC_ERROR_COMMUNICATION:Connect to message server failed
C:\WINNT\system32\drivers\etc
端口号错误报错信息:
(103) RFC_ERROR_LOGON_FAILURE: ## 502 ########
超时:
(103) RFC_ERROR_LOGON_FAILURE: Timeout
执行函数,函数的问题
(104) RFC_ERROR_SYSTEM_FAILURE: Error in module RSQL of the database interface.执行函数
(104) RFC_ERROR_SYSTEM_FAILURE: An error occurred when receiving a complex parameter.
(106) JCO_ERROR_RESOURCE: Trying to access row values in a table which does not have any rows yet
返回的表没有值.那个表连第一行都没有,取不到
(106) JCO_ERROR_RESOURCE: Trying to access row values in a table which does not have any rows yet
语法错误
(104) RFC_ERROR_SYSTEM_FAILURE: Syntax error in program SAPMV50A
找不到行
(106) JCO_ERROR_RESOURCE: Trying to access row values in a table which does not ha:ve any rows yet
输入参数不能插入SAP函数输入字段中.
(122) JCO_ERROR_CONVERSION: Integer ‘4234243’ has to many digits at field PO_ITEM
原文地址:http://hi.baidu.com/gary_c/blog/calendar/200809/index/2
函数不能远程调用
(104) RFC_ERROR_SYSTEM_FAILURE: The function module “RP_CHECK_PERNR” cannot be used for ‘remote’ calls.