java使用jni连接tuxedo(linux)

运行环境

本文章都是在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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值