SAP接口编程 之 JCo3.0系列(04) : 会话管理
在SAP接口编程之 NCo3.0系列(06) : 会话管理 这篇文章中,对会话管理的相关知识点已经说得很详细了,请参考。现在用JCo3.0来实现。
1. JCoContext
如果SAP中多个函数需要在一个session中运行,需要JCoContext来提供保证。如果在同一个线程中,大体模式这样:
JCoContext.begin(sapDestination);
fm1.execute(sapDestination);
fm2.execute(sapDestination);
JCoContext.end(destination);
begin()和end()之间的函数execute之后,SAP不会释放连接,确保同一个session之中。
第二种情况:如果不同的函数不在同一个线程中,需要由开发人员实现SessionReferenceProvider接口,在类中提供session id。逻辑跟nco3.0也是一样的。JCo3.0提供了一个示例代码,但是搞的太复杂,我弄了一个简单的,方便理解。
2. SAP函数
我们要使用的函数是从标准系统函数INCREMENT_COUNTER
,GET_COUNTER拷贝而来的。在SAP系统中INCREMENT_COUNTER
,GET_COUNTER在同一个function group中,共享一个变量count(计数器),每次运行INCREMENT_COUNTER
, count就会加一,GET_COUNTER函数
可以获得这个count。因为这两个函数不能被远程调用,所以我们将这两个函数拷贝出另外两个函数ZINCREMENT_COUNTER和ZGET_COUNTER。
3. 同一线程中执行函数
首先我们把两个函数定义在一个类RfcFunctions中:
package jco3.demo6;
import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.JCoFunction;
public class RfcFunctions
{
public static int runGetCounter(JCoDestination dest) throws JCoException
{
JCoFunction counterFM = dest.getRepository().getFunction("ZGET_COUNTER");
counterFM.execute(dest);
int counter = (int) counterFM.getExportParameterList().getValue("GET_VALUE");
return counter;
}
public static void runIncrement(JCoDestination dest) throws JCoException
{
JCoFunction increment = dest.getRepository().getFunction("ZINCREMENT_COUNTER");
increment.execute(dest);
}
}
然后编写测试类进行测试:
package jco3.demo6;
import com.sap.conn.jco.JCoContext;
import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoDestinationManager;
import com.sap.conn.jco.JCoException;
public class TestSessionSameThread
{
public static void main(String[] args) throws JCoException, InterruptedException
{
// get JCoDestination object instance
JCoDestination destination = JCoDestinationManager.getDestination("ECC");
// make sure the two functions will be executed in the same session
JCoContext.begin(destination);
// Before increment
System.out.println("Before execution of ZINCREMENT_COUNTER:");
System.out.println("Counter:" + RfcFunctions.runGetCounter(destination));
// Run incrementCounter five times
for(int i = 0; i < 5; i++){
RfcFunctions.runIncrement(destination);
System.out.println("Add:" + (i + 1));
}
// After increment
System.out.println("After execution of ZINCREMENT_COUNTER:");
System.out.println("Counter:" + RfcFunctions.runGetCounter(destination));
// release the connection
JCoContext.end(destination);
}
}
代码很直观,就不多说了。函数执行前,counter的值为0,运行函数5次之后,counter的值为5。如果我们注释掉JCoContext.begin(destination);和JCoContext.end(destination);,可以对比出不同的效果。
4. 不同线程中执行函数
如果在不同的线程中执行不同的函数,需要开发者提供session id。我准备将两个函数放在不同的线程中:
- 在JVM的主线程中调用
ZGET_COUNTER,查看counter的结果。 - 在另外一个线程中运行
ZINCREMENT_COUNTER,两个线程通过JCoContext,保持在同一个session ID下。
4.1 实现JCoSessionReference接口
JCoSessionRefence实现类的主要作用是提供session ID:
package jco3.session;
import java.util.concurrent.atomic.AtomicInteger;
import com.sap.conn.jco.ext.JCoSessionReference;
public class JCoSessionRefenceImpl implements JCoSessionReference
{
private AtomicInteger atomInt = new AtomicInteger(0);
private String id = "session"+String.valueOf(atomInt.addAndGet(1));
public void contextFinished()
{
}
public void contextStarted()
{
}
@Override
public String getID()
{
/**
* We need to override getID() method
*/
return id;
}
}
4.2 实现SessionReferenceProvider接口
在SessionReferenceProvider接口的实现类中,改写getCurrentSessionReference()方法,获取上面定义的JCoSessionRefence,从而获得session ID。其他方法保持不动。
package jco3.session;
import com.sap.conn.jco.ext.JCoSessionReference;
import com.sap.conn.jco.ext.SessionException;
import com.sap.conn.jco.ext.SessionReferenceProvider;
public class SessionReferencProviderImpl implements SessionReferenceProvider
{
@Override
public JCoSessionReference getCurrentSessionReference(String scopeType)
{
/**
* We need to override getCurrentSessionReference() method
*/
JCoSessionRefenceImpl sessionRef = new JCoSessionRefenceImpl();
return sessionRef;
}
@Override
public boolean isSessionAlive(String sessionID)
{
return false;
}
public void jcoServerSessionContinued(String sessionID) throws SessionException
{
}
public void jcoServerSessionFinished(String sessionID)
{
}
public void jcoServerSessionPassivated(String sessionID) throws SessionException
{
}
public JCoSessionReference jcoServerSessionStarted() throws SessionException
{
return null;
}
}
4.3 注册 SessionReferenceProvider接口
注册SessionReferenceProvider接口的实现类,这样JCoDestination就有状态管理功能了。
package jco3.session;
import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoDestinationManager;
import com.sap.conn.jco.JCoException;
import com.sap.conn.jco.ext.Environment;
import com.sap.conn.jco.ext.SessionReferenceProvider;
public class DestinationProvider
{
public static JCoDestination getDestination() throws JCoException
{
// create an instance of SessionReferenceProvider
// and register in environment
SessionReferenceProvider provider = new SessionReferencProviderImpl();
Environment.registerSessionReferenceProvider(provider);
JCoDestination destination = JCoDestinationManager.getDestination("ECC");
return destination;
}
}
4.4 在单独线程中执行ZINCREMENT_COUNTER
定义WorkingThread, 从Thread类继承,在这个线程中执行函数ZINCREMENT_COUNTER 5次。
package jco3.demo6;
import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoException;
public class WorkingThread extends Thread
{
private boolean doneSignal;
private JCoDestination destination;
// constructor
public WorkingThread(JCoDestination destination, boolean doneSignal)
{
this.destination = destination;
this.doneSignal = doneSignal;
}
public boolean hasDone()
{
return doneSignal;
}
@Override
public void run()
{
/**
* run method of runIncrement() for five times
*/
for (int i = 0; i < 5; i++){
try {
RfcFunctions.runIncrement(this.destination);
System.out.println("Run " + (i+1) + " times.");
} catch (JCoException e) {
e.printStackTrace();
}
}
this.doneSignal = true;
}
}
doneSignal用于标识该线程是否结束。线程本身结束,是run()方法运行完毕。
4.5 测试多线程函数调用
好了,最后来测试在多线程中函数调用:
package jco3.demo6;
import com.sap.conn.jco.JCoContext;
import com.sap.conn.jco.JCoDestination;
import com.sap.conn.jco.JCoException;
import jco3.session.DestinationProvider;
public class TestSAPSessionMultiThread
{
public static void main(String[] args) throws JCoException, InterruptedException
{
/**
* Run ZINCREMENT_COUNTER & ZGET_COUNTER functions in
* different threads in a stateful way.
*
* The SAP will keep a session id which was created in
* JCoSessionReferenceImpl class
* and used in SessionReferenceProviderImpl class.
*
* Before using, SessionReferenceProviderImpl class should be
* registered using Environment.registerSessionReferenceProvider() method.
*/
// get JCoDestination object instance
JCoDestination destination = DestinationProvider.getDestination();
// make sure the two functions will be executed in the same session
JCoContext.begin(destination);
// Before increment
System.out.println("Before execution of ZINCREMENT_COUNTER:");
System.out.println("Counter:" + RfcFunctions.runGetCounter(destination));
// start a new Thread in which function ZINCREMENT_COUNTER
// will be executed for five times
WorkingThread workingThread = new WorkingThread(destination, false);
workingThread.start();
// wait and switch thread
Thread.sleep(1000);
// After increment
if (workingThread.hasDone() == true){
System.out.println("After execution of ZINCREMENT_COUNTER:");
System.out.println("Counter:" + RfcFunctions.runGetCounter(destination));
}
// release the connection
JCoContext.end(destination);
}
}
与前面同一个线程中代码的主要区别是:
定义一个WorkingThread类的实例,然后启动线程:
WorkingThread workingThread = new WorkingThread(destination, false);
workingThread.start();
然后通过Thread.sleep(), 将线程切换到workingThread中执行,执行完毕再回到主线程显示结果。
原文链接:http://www.jianshu.com/p/2ce28196483c
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
SAP接口编程 之 JCo3.0系列(04) : 会话管理的更多相关文章
- SAP接口编程 之 JCo3.0系列(01):JCoDestination
SAP接口编程 之 JCo3.0系列(01):JCoDestination 字数2101 阅读103 评论0 喜欢0 JCo3.0是Java语言与ABAP语言双向通讯的中间件.与之前1.0/2.0相比 ...
- SAP接口编程 之 JCo3.0系列(02) : JCo Client Programming
SAP接口编程 之 JCo3.0系列(02) : JCo Client Programming 字数545 阅读52 评论0 喜欢1 JCo3.0调用SAP函数的过程 大致可以总结为以下步骤: 连接至 ...
- SAP接口编程 之 JCo3.0系列(03) : Table参数
Table参数作为export parameter BAPI_COMPANYCODE_GETDETAIL是一个适合演示的函数,没有import paramter参数,调用后COMPANYCODE_GE ...
- SAP接口编程 之 JCo3.0系列(05) : Exception Handling
JCO3.0的Exception,常用的Exception如下: JCoException 继承自java.lang.Exception,是JCo3中Exception的基类. JCoRuntimeE ...
- TCP系列04—连接管理—3、TCP连接的半打开和半关闭
在前面部分我们我们分别介绍了三次握手.四次挥手.同时打开和同时关闭,TCP连接还有两种场景分别是半打开(Half-Open)连接和半关闭(Half-Close)连接.TCP是一个全双工(Full-Du ...
- Spring Boot 2.0系列文章(七):SpringApplication 深入探索
关注我 转载请务必注明原创地址为:http://www.54tianzhisheng.cn/2018/04/30/springboot_SpringApplication/ 前言 在 Spring B ...
- LXD 2.0 系列(四):资源控制
LXD 提供了各种资源限制.其中一些与容器本身相关,如内存配额.CPU 限制和 I/O 优先级.而另外一些则与特定设备相关,如 I/O 带宽或磁盘用量限制.-- Stéphane Graber 本文导 ...
- Java 集合系列 04 LinkedList详细介绍(源码解析)和使用示例
java 集合系列目录: Java 集合系列 01 总体框架 Java 集合系列 02 Collection架构 Java 集合系列 03 ArrayList详细介绍(源码解析)和使用示例 Java ...
- java接口,接口的特性,接口实现多态,面向接口编程
package cn.zy.cellphone; /**接口是一种引用数据类型.使用interface声明接口,形式 * 形式:public interface 接口名称{} * 接口不能拥有构造方法 ...
随机推荐
- get/post方式调用http接口
http://www.cnblogs.com/java-pan/tag/HTTP/ http://www.cnblogs.com/snoopylovefiona/p/4730242.html(可做参考 ...
- js作用域链与this
this的绑定与function和对象的定义位置无关,是由函数调用时的执行环境所决定的. scope chain是由函数定义时的位置决定的与函数调用时的执行环境无关.
- 选择轮廓(select_shape)
选择轮廓(select_shape) Halcon是一款运用广泛的图像识别和处理软件.在肤浅的接触中,它的轮廓选择算子(select_shape)给予我很深的印象.结果是往往几行代码就能够产生 ...
- 验证码识别--type5
验证码识别--type5 每一种验证码都是由人设计出来.在设计过程中,可能由于多个方面的原因,造成了这样或那样的可以被利用的漏洞.验证码识别,首先需要解决的问题就是发现这些漏洞--然后利用漏洞解决问题 ...
- 四个好看的CSS样式表格
文章来源 http://www.cnphp6.com/archives/58020 1. 单像素边框CSS表格 这是一个非经常常使用的表格样式. 源码: 2. 带背景图的CSS样式表格 和上面差点儿相 ...
- 【Web】简谈如何监听浏览器的关闭
> 参考的优秀文章 beforeunload实现关闭离开的提示 想起以前做的一个小系统,一个企业内部小型的测试系统,让考生在给定时间内完成考试,如果考生中退出,那么下次进来可以利用剩余的考试时间 ...
- OpenLayers学习记录(1)
1.部署自己的服务器 首先下载openlayers的源码.解压后里面有很多内容.我们只需要拷贝目录下的OpenLayer.js.根目录下的lib目录.根目录下的img目录 theme目录 到你网站的o ...
- mfc unicode下一些容易容易搞混的基本类型
经理要求以后的项目都使用unicode,整理以下看起来让人懵逼的基本类型... 其实在mfc中最容易让人发狂的就是关于字符的操作了. 两种常见基本C++char:(当然C++11新增char_16t. ...
- VC++ 禁止WebBrowser网页跳转时发出的声音和禁止网页上的文字被选择
转载:http://blog.csdn.net/cometnet/article/details/51082091 #include <urlmon.h> #ifndef FEATURE_ ...
- Python3基础 type获取变量的类型
镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...