Example
This chapter provides JTmax server examples.
1. Overview
The following describes the environment for running the example.
-
JTmax Server Listening Address: 192.168.11.43:6555
-
Tmax Address: 192.168.11.41:11000
-
Service Name: JTMAXSERVICE
-
Service Description: A service that returns the same buffer.
2. Tmax Configuration
Add the following values to the Tmax configuration.
*SERVICE JTMAXSERVICE SVRNAME = javagw *GATEWAY javagw GWTYPE = JEUS_ASYNC, PORTNO = 11000, RGWADDR = "192.168.11.43", RGWPORTNO = 6555, CLOPT = "-r ", NODENAME = Node Name, CPC = 1
3. Using the Client
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. Using the JTmax Server
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(); } } }