如果想要一个免费,不使用Weblogic WTC JOLT,实现Java调用Tuxedo,那最容易想到的就是通过JNI,让Java执行native代码,实现对Tuxedo的调用。
环境:Suse 9 Tuxedo 8.1 Java1.4 $JAVA_HOME $TUXDIR
实现的步骤如下 编写带native方法的Java类
/**
* This is a demo class for test JNI
*
* @author STEELE
*
*/
public class Hello {
static {
try {
// "hello" means the library of native method
System.loadLibrary("hello");
} catch (UnsatisfiedLinkError e) {
System.err.println("Cannot load hello library:“n " + e.toString());
}
}
public Hello() {
}
// This is a native method without args and return
public native void SayHello();
}
2.编译为.class文件
javac Hello.java
3.生成.h文件
javah Hello
4.编写对应的C文件,我一般直接拷贝一个.h文件为.c,之后修改,感觉比较不会出错
#include
#include "atmi.h" /* TUXEDO Header File */
/* DO NOT EDIT THIS FILE - it is machine generated */
#include
/* Header for class Hello */
#ifndef _Included_Hello
#define _Included_Hello
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: Hello
* Method: SayHello
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_Hello_SayHello
(JNIEnv * env, jobject arg)
{
char *sendbuf, *rcvbuf;
long sendlen, rcvlen;
int ret;
printf("THIS IS JNI Say Hello“n");
/* Attach to System/T as a Client Process */
if (tpinit((TPINIT *) NULL) == -1) {
(void) fprintf(stderr, "Tpinit failed“n");
exit(1);
}
sendlen = 32;
/* Allocate STRING buffers for the request and the reply */
if((sendbuf = (char *) tpalloc("STRING", NULL, sendlen+1)) == NULL) {
(void) fprintf(stderr,"Error allocating send buffer“n");
tpterm();
exit(1);
}
if((rcvbuf = (char *) tpalloc("STRING", NULL, sendlen+1)) == NULL) {
(void) fprintf(stderr,"Error allocating receive buffer“n");
tpfree(sendbuf);
tpterm();
exit(1);
}