JAVA调用Tuxedo服务——使用JNI封装Tuxedo客户端的一个例子
2008-07-23 21:41
686 查看
据俺了解:目前java调用tuxedo服务只有通过jolt而且必须使用weblogic,大家因此比较郁闷。但JNI可以帮助我们实现绕开jolt,用java直接访问tuxedo服务。总结综合了网上前辈的各种办法后,下面是简单的一个例子。
1. 使用VC开发tuxedo客户端程序,封装成DLL形式。
2. 在Java中定义JNI调用,并撰写使用该JNI调用的相关代码。
3. 根据定义好的JNI调用生成相应的C语言头文件。
4. 利用生成的头文件,实现Windows平台上的代码(导入第1步实现的DLL函数,封装这些函数成符合JNI规范的DLL)
5. 编译代码,再生成封装后的DLL文件。
6. 执行Java程序中JNI调用的代码。
1. 开发客户端,编译后链接成DLL
// ClientDLL.cpp was generated by BuildTuxedo on 07/20/08 @ 15:14:40 ***** DO NOT EDIT *****
#include <tmenv.h>
#include <xa.h>
#include <atmi.h>
#include "ClientDLL.h"
#include <stdio.h>
#include <string.h>
#include <userlog.h>
#include <windows.h>
extern "C" _declspec(dllexport) char* send_rqst(const char* to_send){
char *sendbuf, *rcvbuf;
long sendlen, rcvlen;
int ret;
char* result = NULL;
//设置超时时间5秒
tpbegin(5,0);
//Sleep(8000);
if (tpinit((TPINIT *) NULL) == -1) {
(void) fprintf(stderr, "Tpinit failed/n");
return result;
}
sendlen = sizeof(to_send)*2;
//userlog("%d/n",sendlen);
if((sendbuf = (char *) tpalloc("STRING", NULL, sendlen+1)) == NULL) {
(void) fprintf(stderr,"Error allocating send buffer/n");
tpterm();
return result;
}
if((rcvbuf = (char *) tpalloc("STRING", NULL, sendlen+1)) == NULL) {
(void) fprintf(stderr,"Error allocating receive buffer/n");
tpfree(sendbuf);
tpterm();
return result;
}
//(void) strcpy(sendbuf, to_send);
for(int i =0;i<sendlen;i++)
sendbuf[i]=to_send[i];
//userlog("leng = %d/n",sizeof(sendbuf)*2);
ret = tpcall("GETINFO", (char *)sendbuf, 0, (char **)&rcvbuf, &rcvlen, (long)0);
if(ret == -1) {
(void) fprintf(stderr, "Can't send request to service GETINFO/n");
(void) fprintf(stderr, "Tperrno = %d/n", tperrno);
tpfree(sendbuf);
tpfree(rcvbuf);
if(tperrno == 13){
//超时
//strcpy(result,"TPETIME");
result = "TPETIME";
}
else if(tperrno == 11){
//应用程序级错误,比如没有找到对应的卡号
//strcpy(result,"TPESVCFAIL");
result = "TPESVCFAIL";
}
else if(tperrno == 10){
//tuxedo服务错误
//strcpy(result,"TPESVCERR");
result = "TPESVCERR";
}
tpcommit(0);
tpterm();
return result;
}
(void) fprintf(stdout, "Returned string is: %s/n", rcvbuf);
//根据rcvbuf构造result,需要在外面delete
result = new char[sizeof(rcvbuf)*2];
for(int j=0;j<(sizeof(rcvbuf)*2);j++)
result[j]=rcvbuf[j];
/* Free Buffers & Detach from System/T */
tpfree(sendbuf);
tpfree(rcvbuf);
//结束事务
tpcommit(0);
tpterm();
return result;
}
2. 在Java中定义JNI调用,并撰写使用该JNI调用的相关代码。public class JNIWrapper {
static native String[] _dll_send_rqst(String to_send);
static {
try {
System.loadLibrary("DataBaseDLLWrapper");
} catch (Exception e) {
System.out.println("DataBaseDLLWrapper.dll load errer");
}
}
static String[] send_rqst(String to_send) {
return _dll_send_rqst(to_send);
}
}
1. 使用VC开发tuxedo客户端程序,封装成DLL形式。
2. 在Java中定义JNI调用,并撰写使用该JNI调用的相关代码。
3. 根据定义好的JNI调用生成相应的C语言头文件。
4. 利用生成的头文件,实现Windows平台上的代码(导入第1步实现的DLL函数,封装这些函数成符合JNI规范的DLL)
5. 编译代码,再生成封装后的DLL文件。
6. 执行Java程序中JNI调用的代码。
1. 开发客户端,编译后链接成DLL
// ClientDLL.cpp was generated by BuildTuxedo on 07/20/08 @ 15:14:40 ***** DO NOT EDIT *****
#include <tmenv.h>
#include <xa.h>
#include <atmi.h>
#include "ClientDLL.h"
#include <stdio.h>
#include <string.h>
#include <userlog.h>
#include <windows.h>
extern "C" _declspec(dllexport) char* send_rqst(const char* to_send){
char *sendbuf, *rcvbuf;
long sendlen, rcvlen;
int ret;
char* result = NULL;
//设置超时时间5秒
tpbegin(5,0);
//Sleep(8000);
if (tpinit((TPINIT *) NULL) == -1) {
(void) fprintf(stderr, "Tpinit failed/n");
return result;
}
sendlen = sizeof(to_send)*2;
//userlog("%d/n",sendlen);
if((sendbuf = (char *) tpalloc("STRING", NULL, sendlen+1)) == NULL) {
(void) fprintf(stderr,"Error allocating send buffer/n");
tpterm();
return result;
}
if((rcvbuf = (char *) tpalloc("STRING", NULL, sendlen+1)) == NULL) {
(void) fprintf(stderr,"Error allocating receive buffer/n");
tpfree(sendbuf);
tpterm();
return result;
}
//(void) strcpy(sendbuf, to_send);
for(int i =0;i<sendlen;i++)
sendbuf[i]=to_send[i];
//userlog("leng = %d/n",sizeof(sendbuf)*2);
ret = tpcall("GETINFO", (char *)sendbuf, 0, (char **)&rcvbuf, &rcvlen, (long)0);
if(ret == -1) {
(void) fprintf(stderr, "Can't send request to service GETINFO/n");
(void) fprintf(stderr, "Tperrno = %d/n", tperrno);
tpfree(sendbuf);
tpfree(rcvbuf);
if(tperrno == 13){
//超时
//strcpy(result,"TPETIME");
result = "TPETIME";
}
else if(tperrno == 11){
//应用程序级错误,比如没有找到对应的卡号
//strcpy(result,"TPESVCFAIL");
result = "TPESVCFAIL";
}
else if(tperrno == 10){
//tuxedo服务错误
//strcpy(result,"TPESVCERR");
result = "TPESVCERR";
}
tpcommit(0);
tpterm();
return result;
}
(void) fprintf(stdout, "Returned string is: %s/n", rcvbuf);
//根据rcvbuf构造result,需要在外面delete
result = new char[sizeof(rcvbuf)*2];
for(int j=0;j<(sizeof(rcvbuf)*2);j++)
result[j]=rcvbuf[j];
/* Free Buffers & Detach from System/T */
tpfree(sendbuf);
tpfree(rcvbuf);
//结束事务
tpcommit(0);
tpterm();
return result;
}
2. 在Java中定义JNI调用,并撰写使用该JNI调用的相关代码。public class JNIWrapper {
static native String[] _dll_send_rqst(String to_send);
static {
try {
System.loadLibrary("DataBaseDLLWrapper");
} catch (Exception e) {
System.out.println("DataBaseDLLWrapper.dll load errer");
}
}
static String[] send_rqst(String to_send) {
return _dll_send_rqst(to_send);
}
}
public class JavaApp { public static final void main(String args[]) { String a = "81704177"; String[] b = JNIWrapper.send_rqst(a); System.out.println(b[0]); } }
4.利用生成的头文件,实现Windows平台上的代码5. 最重要的一部分 DataBaseDLLWrapper.dll的代码丢了,555555555555555555
相关文章推荐
- JAVA调用Tuxedo服务——使用JNI封装Tuxedo客户端的一个例子
- Java使用JNI调用VC的DLL的一个小例子
- Linux平台Java调用so库-JNI使用例子
- Linux平台使用JNI的例子 Java调用so
- Linux平台Java调用so库-JNI使用例子
- Linux平台使用JNI的例子 Java调用so
- Linux平台Java调用so库-JNI使用例子
- Linux平台Java调用so库-JNI使用例子
- Linux平台Java调用so库-JNI使用例子
- Samba 服务使用的端口和协议(是一组TCP UDP协议的组合,主要使用CIFS协议,有一个Java例子)
- jni使用--java native 调用c++ boost regex库例子
- Android NDK 使用JNI调用java代码,通用代码封装
- Linux平台Java调用so库-JNI使用例子
- Linux平台Java调用so库-JNI使用例子
- Linux平台Java调用so库-JNI使用例子
- Linux平台Java调用so库-JNI使用例子
- Linux平台Java调用so库-JNI使用例子
- java使用jolt调用tuxedo服务
- 一个初级例子讲述C和CPP在JNI中调用安卓Java代码
- Linux平台Java调用so库-JNI使用例子