关闭

Java通过JNI调用Tuxedo方法

发表于:2010-1-11 10:40

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:未知    来源:51Testing软件测试网采编

#
java

  如果想要一个免费,不使用Weblogic WTC JOLT,实现Java调用Tuxedo,那最容易想到的就是通过JNI,让Java执行native代码,实现对Tuxedo的调用。

  环境:Suse 9  Tuxedo 8.1  Java1.4 $JAVA_HOME $TUXDIR

  实现的步骤如下:

  1. 编写带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 <stdio.h>
  #include "atmi.h"        /* TUXEDO  Header File */
  /* DO NOT EDIT THIS FILE - it is machine generated */
  #include <jni.h>
  /* 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);
  }
  (void) strcpy(sendbuf, "0123456789abcdef0123456789abcdef");
  /* Request the service TOUPPER, waiting for a reply */
  ret = tpcall("TOUPPER", (char *)sendbuf, 0, (char **)&rcvbuf, &rcvlen, (long)0);
  if(ret == -1) {
  (void) fprintf(stderr, "Can't send request to service TOUPPER\n");
  (void) fprintf(stderr, "Tperrno = %d\n", tperrno);
  tpfree(sendbuf);
  tpfree(rcvbuf);
  tpterm();
  exit(1);
  }
  (void) fprintf(stdout, "Returned string is: %s\n", rcvbuf);
  /* Free Buffers & Detach from System/T */
  tpfree(sendbuf);
  tpfree(rcvbuf);
  tpterm();
  return;
  }
  #ifdef __cplusplus
  }
  #endif
  #endif

21/212>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号