一、sapjco引入
1.1、sapjco介绍
- sapjco3.jar,连接SAP所需的jar包,linux与windows系统通用
- libsapjco3.so,linux系统下连接sap所需的动态链接库
- sapjeco3.dll,windows系统下连接sap所需的动态链接库
- 我使用的版本为sapjco3.0.10
<dependency>
<groupId>com.sap</groupId>
<artifactId>sapjco3</artifactId>
<version>3.0.10</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/sapjco3.jar</systemPath>
</dependency>
1.2、libsapjco3.so、sapjeco3.dll 放入resources文件夹
- 经过测试,链接库最佳放入地点就是放在resources文件夹下,这样可以不用配置<resources>标签就可package打包部署发布
二、IJ IDEA 开发时使用方式
2.1、Maven项目下的sapjco3.jar配置
- pom.xml下引入sapjco3.jar,sapjco3.jar可以放在任意目录,引入正确即可,例如我在项目主目录下新建个lib目录存放,dependency引入方式见1.1
- 引入成功之后,自动生效
三、Windows环境部署
SpringBoot开发打包的jar,想要java -jar xxx.jar在Windows下运行成功,需要把sapjco3.dll放入%JAVA_HOME%\bin的目录下才能直连SAP成功
3.1、sapjco3.dll放入jdk\bin目录
jdk安装目录:E:\Program Files\Java\jdk1.8.0_201
把sapjco3.dll复制到E:\Program Files\Java\jdk1.8.0_201\bin下
放入完毕后,重启项目即可
四、Linux环境部署
同windows一样,想要java -jar xxx.jar成功,需要配置链接库,但linux需要把libsapjco3.so,sapjco.jar均加载进入jdk目录才可
4.1、libsapjco3.so,sapjco.jar放入jdk
我的java安装目录为/usr/java/jdk1.8.0_111
libsapjco3.so放入目录 /usr/java/jdk1.8.0_111/jre/lib/amd64/server
sapjco3.jar放入目录 /usr/java/jdk1.8.0_111/jre/lib
4.2、配置环境变量
- 打开 /etc/profile 文件,配置如下参数,放置文件末尾即可
export CLASSPATH=/usr/java/jdk1.8.0_111/jre/libsapjco3.jar
export LD_LIBRARY_PATH=/usr/java/jdk1.8.0_111/jre/lib/amd64/server
- 执行 source /etc/profile 命令,使环境变量配置生效,然后重启项目即可
五、直连SAP代码开发
5.1、pom.xml配置
<!-- 引入sapjco3.jar -->
<dependency>
<groupId>com.sap</groupId>
<artifactId>sapjco3</artifactId>
<version>3.0.10</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/sapjco3.jar</systemPath>
</dependency>
<!-- 配置includeSystemScope为true,表示打包时,把本地的system的jar打入 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
5.2、代码示例
import com.sap.conn.jco.*;
import com.sap.conn.jco.ext.DestinationDataProvider;
import org.springframework.stereotype.Component;
import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
/**
* 与SAP连接配置
*
* @author lixn_90
*/
@Component
public class SAPConn {
private static final String ABAP_AS_POOLED = "ABAP_AS_WITH_POOL";
private static Properties connectProperties = new Properties();
static {
//SAP系统配置,以下配置均为SAP提供
//主机地址
connectProperties.setProperty(DestinationDataProvider.JCO_ASHOST, "127.0.0.1");
//系统编号
connectProperties.setProperty(DestinationDataProvider.JCO_SYSNR, "00");
//客户端
connectProperties.setProperty(DestinationDataProvider.JCO_CLIENT, "123");
//用户名
connectProperties.setProperty(DestinationDataProvider.JCO_USER, "TEST");
//密码
connectProperties.setProperty(DestinationDataProvider.JCO_PASSWD, "TEST_PWD");
//登入语言
connectProperties.setProperty(DestinationDataProvider.JCO_LANG, "EN");
//最大空闲连接数
connectProperties.setProperty(DestinationDataProvider.JCO_POOL_CAPACITY, "2");
//最大活动链接数
connectProperties.setProperty(DestinationDataProvider.JCO_PEAK_LIMIT, "20");
}
/**
* 创建SAP接口属性文件。
*
* @param name ABAP管道名称
* @param suffix 属性文件后缀
* @param properties 属性文件内容
*/
public static void createDataFile(String name, String suffix, Properties properties) {
File cfg = new File(name + "." + suffix);
try {
if (cfg.exists()) {
cfg.deleteOnExit();
}
FileOutputStream fos = new FileOutputStream(cfg, false);
properties.store(fos, "for tests only !");
fos.close();
} catch (Exception e) {
throw new RuntimeException("Unable to create the destination file " + cfg.getName(), e);
}
}
/**
* 获取SAP连接
*
* @return SAP连接对象
*/
public static JCoDestination connect() {
JCoDestination destination = null;
try {
createDataFile(ABAP_AS_POOLED, "jcoDestination", connectProperties);
destination = JCoDestinationManager.getDestination(ABAP_AS_POOLED);
destination.ping();
} catch (JCoException e) {
e.printStackTrace();
}
return destination;
}
/**
* 请求SAP
*
* @param param 请求内容
* @return SAP返回内容
*/
public static String requestSap(String param) {
//SAP返回结果内容
String sapReturnResult = "";
//SAP方法名
String functionName = "SAP_TEST_METHOD_01";
//SAP提供的请求参数Key
String requestKey = "IV_INPUT";
//SAP提供的返回结果Key
String resultKey = "CV_OUTPUT";
//SAP传入参数集合
Map<String, String> queryMap = new HashMap<>();
queryMap.put(requestKey, param);
try {
JCoDestination destination = SAPConn.connect();
JCoFunction function = destination.getRepository().getFunction(functionName);
for (Map.Entry<String, String> entry : queryMap.entrySet()) {
function.getImportParameterList().setValue(entry.getKey(), entry.getValue());
}
// 执行调用
function.execute(destination);
JCoParameterList exportParam = function.getExportParameterList();
//获取单条数据
sapReturnResult = exportParam.getString(resultKey);
} catch (JCoException e) {
e.printStackTrace();
}
return sapReturnResult;
}
/**
* 访问测试
*
* @param args
*/
public static void main(String[] args) {
String result = requestSap("test");
System.out.println("result = " + result);
}
}
六、注意事项
6.1、sapjco带版本号直接打入jar
includeSystemScope设置为true即可
<includeSystemScope>true</includeSystemScope>
6.2、sapjco不带版本号打入jar
- includeSystemScope设置为false,取消本地的system的jar打入
<includeSystemScope>false</includeSystemScope>
- 利用<resources>标签打入
<resources>
<resource>
<directory>${project.basedir}/lib</directory>
<targetPath>BOOT-INF/lib/</targetPath>
<includes>
<include>**/*.jar</include>
</includes>
</resource>
</resources>
6.3、打包libsapjco3.so、sapjco3.dll文件至jar
<resource>
<directory>${project.basedir}/lib</directory>
<targetPath>BOOT-INF</targetPath>
<filtering>true</filtering>
<includes>
<include>**/*.so</include>
<include>**/*.dll</include>
</includes>
</resource>
6.4、pom的resources疑问(未完美解决)
- 配置resource,会导致idea启动失败,但是打包的jar可运行
- 不配置resource,idea运行成功,但是打包的jar无法运行,.so文件不能打包到BOOT-INF目录下
<resources>
<resource>
<directory>${project.basedir}/lib</directory>
<targetPath>BOOT-INF/lib/</targetPath>
<includes>
<include>**/*.jar</include>
</includes>
</resource>
<resource>
<directory>${project.basedir}/lib</directory>
<targetPath>BOOT-INF</targetPath>
<filtering>true</filtering>
<includes>
<include>**/*.so</include>
<include>**/*.dll</include>
</includes>
</resource>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<targetPath>BOOT-INF/classes/</targetPath>
<filtering>true</filtering>
</resource>
</resources>
- 未曾完美解决,目前想要保留resources配置,就只能在idea开发启动时,注释掉
- package打包成jar时解开注释,打包后部署发布
- 所以目前最佳方式,就是把 libsapjco3.so、sapjeco3.dll 放入resources文件夹,不配置任何resource,使用springBoot默认配置打包