예제

본 장에서는 JTmax 서버 사용 예제에 대해 기술한다.

1. 개요

다음은 예제 실행을 위한 환경에 대한 설명이다.

  • JTmax 서버가 listen할 주소 : 192.168.11.43:6555

  • Tmax 구성 : 192.168.11.41:11000

  • 서비스 이름 : JTMAXSERVICE

  • 서비스 설명 : 동일한 버퍼를 반환하는 서비스이다.

2. Tmax 설정

Tmax의 환경 구성에 아래의 값을 추가한다.

*SERVICE
JTMAXSERVICE       SVRNAME = javagw

*GATEWAY
javagw         GWTYPE = JEUS_ASYNC, PORTNO = 11000,
               RGWADDR = "192.168.11.43",
               RGWPORTNO = 6555,
               CLOPT = "-r ",
               NODENAME = 노드명,
               CPC = 1

3. 클라이언트 사용

package test.jtmax.jg;

import tmax.webt.WebtBuffer;
import tmax.webt.WebtConnection;
import tmax.webt.WebtRemoteService;
import tmax.webt.WebtTransaction;

public class WebtClient {
private String ip;
private int port;
private String svcName;
private WebtConnection conn;

public WebtClient(String ip, int port, String svcName) {
   this.ip = ip;
   this.port = port;
    this.svcName = svcName;
    conn = new WebtConnection(this.ip,this.port);
 }

 public void tpcall()
 {
    WebtRemoteService service = new WebtRemoteService(svcName, conn);

    WebtBuffer sndBuf = service.createStringBuffer();
    sndBuf.setString("Echo String");
    WebtBuffer rcvBuf = service.tpcall(sndBuf);

    System.out.println(rcvBuf);
 }

 public void tpcalltran()
 {
    WebtTransaction tr = new WebtTransaction(conn);

    WebtRemoteService service = new WebtRemoteService(svcName, conn);
    WebtBuffer sndBuf = service.createStringBuffer();

    tr.begin();
    sndBuf.setString("Echo String");
    WebtBuffer rcvBuf = service.tpcall(sndBuf);
    System.out.println(rcvBuf);

    sndBuf.setString("Echo String2");
    rcvBuf = service.tpcall(sndBuf);
    System.out.println(rcvBuf);
    tr.commit();
 }

 public static void main(String[] args) {
    WebtClient client = new WebtClient("192.168.11.41",11000,"JTMAXSERVICE");
    client.tpcall();
    client.tpcalltran();
 }

}

4. JTmax 서버 사용

package test.jtmax.jg;

import java.io.IOException;

import javax.transaction.xa.XAResource;
import javax.transaction.xa.Xid;

import tmax.webt.WebtBuffer;

import com.tmax.nio.util.JTmaxEventHandler;
import com.tmax.nio.util.JTmaxResult;
import com.tmax.nio.util.JTmaxServer;

public class JTmaxSimpleServer implements JTmaxEventHandler{

 public JTmaxSimpleServer()
 {
 }

 public Xid[] recover() {
     return null;
 }

 public JTmaxResult serviceExecute(Xid xid, String serviceName,
 WebtBuffer rcvBuffer) {
     System.out.println("service execute : "+xid+","+serviceName+","+rcvBuffer);
     return new JTmaxResult(JTmaxResult.TPSUCCESS,0,rcvBuffer);
 }

 public JTmaxResult xaCommit(Xid xid, boolean isOnePhrase) {
     System.out.println("xa commit : "+xid+","+isOnePhrase);
     return new JTmaxResult(JTmaxResult.TPSUCCESS,XAResource.XA_OK,null);
 }

 public JTmaxResult xaPrepare(Xid xid) {
     System.out.println("xa prepare : "+xid);
     return new JTmaxResult(JTmaxResult.TPSUCCESS,XAResource.XA_OK,null);
 }

 public JTmaxResult xaRollback(Xid xid) {
     System.out.println("xa rollback : "+xid);
     return new JTmaxResult(JTmaxResult.TPSUCCESS,XAResource.XA_OK,null);
 }


 public static void main(String[] args)
 {
     JTmaxSimpleServer server = new JTmaxSimpleServer();
     server.startServer("jtmax",6555,10);
 }


private void startServer(String name, int port, int maxConnection) {
  JTmaxServer server = new JTmaxServer(name, port, maxConnection, this);
  try {
      server.startServer();
  }
  catch (IOException e) {
      e.printStackTrace();
      return;
  }

  try {
      System.in.read();
  }
  catch (IOException e) {
      e.printStackTrace();
  }

  try {
   server.stopServer();
  }
  catch (IOException e) {
      e.printStackTrace();
  }

 }

}