在上面说过如何在Weblogic中如何配置WTC服务,下面给出具体的Java代码的实现
package test;
import java.rmi.RemoteException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import weblogic.wtc.gwt.TuxedoConnection;
import weblogic.wtc.gwt.TuxedoConnectionFactory;
import weblogic.wtc.jatmi.Reply;
import weblogic.wtc.jatmi.TPException;
import weblogic.wtc.jatmi.TPReplyException;
import weblogic.wtc.jatmi.TypedString;
public class WTCCaller {
/**
* 函数功能:执行调用命令,返回结果
*
* @param serviceName
* 服务名,可能取值如下:在WTC中配置的服务,即Import中的配置
* @param command 发送的命令
* * @return 返回的结果值(即tuxedo返回的值)
* @throws TPException
* @throws TPReplyException
*/
public String execute(String serviceName, String command)
throws TPException, TPReplyException, RemoteException {
Context ctx;
TuxedoConnectionFactory tcf;
TuxedoConnection myTux;
TypedString myData;
Reply myRtn = null;
try {
ctx = new InitialContext();
tcf = (TuxedoConnectionFactory) ctx
.lookup("tuxedo.services.TuxedoConnection");
} catch (NamingException ne) {
String errorStr = "不能连接tuxedo服务,具体错误:" + ne;
throw new TPException(TPException.TPENOENT, errorStr);
}
myTux = tcf.getTuxedoConnection();
myData = new TypedString(command);
try {
myRtn = myTux.tpcall(serviceName, myData, 0);
} catch (TPReplyException tre) {
System.out.println("error occur");
throw tre;
} catch (TPException te) {
System.out.println("error occur");
te.printStackTrace();
throw te;
} catch (Exception ee) {
String errorStr = "调用tuxedo服务出错,具体错误:" + ee;
throw new TPException(TPException.TPESYSTEM, errorStr);
}
if (myRtn != null) {
myData = (TypedString) myRtn.getReplyBuffer();
myTux.tpterm();
}
//System.out.print(myData.toString());
return ("value:"+myData.toString());
}
}