java使用jni连接tuxedo(linux)

208 阅读2分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

运行环境

本文章都是在Linux环境,redhat下操作。 我使用的环境: java环境:JDK 1.8 tuxedo客户端:tuxedo121300_64_Linux_01_x86.zip。下载地址:Oracle Tuxedo Downloads。。 C语言拼包,java发起交易。

1、java程序

新建TuxedoJNI.java文件

public class TuxedoJNI{
        static{
                System.load("/home/test/tuxedo/libtuxedo.so");
        }
        public native static int tuxedo_send();

        public static void main(String[] args)
        {
                TuxedoJNI test = new TuxedoJNI();
                test.tuxedo_send();
        }
}

编译java程序,生成TuxedoJNI.class

javac TuxedoJNI.java

2、生成C的头文件

使用javah命令,生成TuxedoJNI.h头文件,

当前目录下使用javah报错,找不到'TuxedoJNI'的类文件,可以加入-classpath ./指定当前目录。

javah -classpath ./ TuxedoJNI

生成的TuxedoJNI.h

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class TuxedoJNI */

#ifndef _Included_TuxedoJNI
#define _Included_TuxedoJNI
#ifdef __cplusplus
extern "C" {
#endif
/*

 * Class:     TuxedoJNI
 * Method:    tuxedo_send
 * Signature: ()I
   */
   JNIEXPORT jint JNICALL Java_TuxedoJNI_tuxedo_1send
     (JNIEnv *, jclass);

#ifdef __cplusplus
}
#endif
#endif

3、C程序

新建client.c

#include <stdio.h>
#include "atmi.h"               /* TUXEDO  Header File */
#include "TuxedoJNI.h"
#define DATALEN 2048
/*void main() */
JNIEXPORT jint JNICALL
Java_TuxedoJNI_tuxedo_1send(JNIEnv *env, jclass jc)
{
        char *sendbuf, *rcvbuf;
        long sendlen, rcvlen;
        int ret,iLen=0,lLen=0;

        putenv("WSNADDR=//192.168.1.11:8000");
        /* Attach to System/T as a Client Process */
        if (tpinit((TPINIT *) NULL) == -1) {
                (void) fprintf(stderr, "Tpinit failed\n");
                exit(1);
        }
    
        /* Allocate STRING buffers for the request and the reply */
    
        if((sendbuf = (char *) tpalloc("CARRAY", NULL, DATALEN+1)) == NULL) {
                (void) fprintf(stderr,"Error allocating send buffer\n");
                tpterm();
                exit(1);
        }
    
        if((rcvbuf = (char *) tpalloc("CARRAY", NULL, DATALEN+1)) == NULL) {
                (void) fprintf(stderr,"Error allocating receive buffer\n");
                tpfree(sendbuf);
                tpterm();
                exit(1);
        }
    
        /* orgnize the package */
    	sendlen = Pack_buf(sendbuf);//发包自己实现

        /* Request the service IPPSRV, waiting for a reply */
        ret = tpcall("zhcx", (char *)sendbuf, sendlen, (char **)&rcvbuf, &rcvlen, (long)0);
        if(ret == -1 && tperrno==TPESYSTEM) {
                tpterm();
                ret = tpcall("zhcx", (char *)sendbuf, sendlen, (char **)&rcvbuf, &rcvlen, (long)0);
        }
    
        if(ret == -1) {
                (void) fprintf(stderr, "Can't send request to service IPPSRV\n");
                (void) fprintf(stderr, "Tperrno = %d\n", tperrno);
                tpfree(sendbuf);
                tpfree(rcvbuf);
                tpterm();
                exit(1);
        }
        unPack_buf(rcvbuf,rcvlen); //收包自己实现

        /* Free Buffers & Detach from System/T */
        tpfree(sendbuf);
        tpfree(rcvbuf);
        tpterm();

}

使用makefile编译,生成libtuxedo.so

TUXDIR=/home/tuxedo/OraHome_1/tuxedo12.1.3.0.0
JNIINC=/home/ipp/test/jdk1.8.0_271/include
JNIMDINC=/home/ipp/test/jdk1.8.0_271/include/linux

CC      =  cc
CLIB    = -lx
#
# 本项目的所有模块
#
PROJECT = tuxedo
#
# 所有编译单元
#
all:tuxedo

tuxedo:cli.c
        buildclient -w -o libtuxedo.so -f " -I$(JNIINC) -I$(JNIMDINC) -fPIC -shared cli.c pack.c "  -l "-lc"
clean:
        -rm libtuxedo.so

4、执行程序测试

java -classpath ./ TuxedoJNI