用JNA实现的tuxedo client调用
时间:2015-01-27 19:18 来源: 我爱IT技术网 作者:小搜
这是client部分,server部分本人用不着所以就没做。
下面演示了同步调用和异步调用两种范例,在本人笔记本上测试成功
jna的文档里交代,jna的性能比原生库要差一些,先前我做过尝试,用lazarus封装了一下,结果在测试过程中发现容易崩溃,本人对c又不熟,无奈只能用jna直接调用tuxedo了。如果你对c比较熟悉的话,还是建议你用c库封装一下tuxedo库。
环境
os:ubuntu 10.11
tuxedo 11.1.1.2.0
我用的是ubuntu,如果是windows操作系统的话,只要把库名换掉就可以了。
做的比较粗糙,只是展示了一下客户端同步和异步调用而已。
package com.jna.demo;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.PointerByReference;
public class Demo2 {
private static final int C_MAX_IN_CHAR_LENGTH = 1024;
private static final int C_MAX_OUT_CHAR_LENGTH = 10240 *3;
public interface ATMILib extends Serializable, Library {
public static ATMILib instance = (ATMILib)Native.loadLibrary("tux", ATMILib.class);
public int tpinit(long tpinfo);
public int tpterm();
public void tpfree(Pointer ptr);
public Pointer tpalloc(final String types, final String subType, final int size);
public int gettperrno();
public int tpcall(String svc, Pointer data, int len, PointerByReference outData, IntByReference outLen, int flags);
public Pointer tpstrerror(final int errorno);
public void tuxputenv(final String envStr);
public int tpacall(String svc, Pointer data, long len, long flags);
public int tpgetrply(IntByReference callHandle, PointerByReference data, IntByReference len, long flags);
}
public static void testSyncCall(final byte[] sendBytes) throws UnsupportedEncodingException{
ATMILib.instance.tuxputenv("WSNADDR=//192.168.0.7:7117");
int ret = ATMILib.instance.tpinit(0);
System.out.println(String.format("tpinit=%d" ,new Object[]{ret}));
if (ret != 1){
System.exit(0);
}
Pointer sendBuf = ATMILib.instance.tpalloc("STRING", null, C_MAX_IN_CHAR_LENGTH);
sendBuf.clear(C_MAX_IN_CHAR_LENGTH);
for(int i=0; i<=sendBytes.length-1; i++){
byte bts = sendBytes[i];
sendBuf.setByte(i, bts);
}
Pointer recvBuf = ATMILib.instance.tpalloc("STRING", null, C_MAX_OUT_CHAR_LENGTH);
try{
IntByReference recvLen = new IntByReference();
PointerByReference pRecvBuf = new PointerByReference();
pRecvBuf.setValue(recvBuf);
ret = ATMILib.instance.tpcall("TOUPPER", sendBuf, sendBytes.length, pRecvBuf, recvLen, 0);
try{
int len = recvLen.getValue()-1;
System.out.println(String.format("tpcall = %d", new Object[]{ret}));
System.out.println(String.format("outlen = %d", new Object[]{len}));
recvBuf = pRecvBuf.getValue();
byte[] bytes =recvBuf.getByteArray(0, len);
System.out.println(new String(bytes, "gbk"));
}finally{
ATMILib.instance.tpfree(recvBuf);
}
}finally{
ATMILib.instance.tpfree(sendBuf);
}
}
public static void testASyncCall(final byte[] sendBytes) throws UnsupportedEncodingException{
String s = "abcdefghijklmnopqrstuvwxyz";
ATMILib.instance.tuxputenv("WSNADDR=//192.168.0.7:7117");
int ret = ATMILib.instance.tpinit(0);
System.out.println(String.format("tpinit=%d" ,new Object[]{ret}));
if (ret != 1){
System.exit(0);
}
Pointer sendBuf = ATMILib.instance.tpalloc("STRING", null, C_MAX_IN_CHAR_LENGTH);
try{
sendBuf.clear(C_MAX_IN_CHAR_LENGTH);
for(int i=0; i<=sendBytes.length-1; i++){
byte bts = sendBytes[i];
sendBuf.setByte(i, bts);
}
Pointer recvBuf = ATMILib.instance.tpalloc("STRING", null, C_MAX_OUT_CHAR_LENGTH);
try{
IntByReference recvLen = new IntByReference();
PointerByReference pRecvBuf = new PointerByReference();
pRecvBuf.setValue(recvBuf);
int sessionId = ATMILib.instance.tpacall("TOUPPER", sendBuf, sendBytes.length, 0);
long lStart = System.currentTimeMillis();
ret = -1;
do{
IntByReference pHandle = new IntByReference();
pHandle.setValue(sessionId);
ret = ATMILib.instance.tpgetrply(pHandle, pRecvBuf, recvLen, 0);
}while(ret < 0 && System.currentTimeMillis() - lStart > 0);
int len = recvLen.getValue()-1;
System.out.println(String.format("tpcall = %d", new Object[]{ret}));
System.out.println(String.format("outlen = %d", new Object[]{len}));
recvBuf = pRecvBuf.getValue();
byte[] bytes =recvBuf.getByteArray(0, len);
System.out.println(new String(bytes, "gbk"));
}finally{
ATMILib.instance.tpfree(recvBuf);
}
}finally{
ATMILib.instance.tpfree(sendBuf);
}
}
@SuppressWarnings("unused")
public static void main(String[] args) throws UnsupportedEncodingException {
byte[] bytes = "abcdefghijklmnopqrstuvwxyz".getBytes();
//testSyncCall(bytes);
testASyncCall(bytes);
}
}
经过本人测试不太稳定,JVM偶尔崩溃,不能用于生产,还得想别的办法呀。
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
