ProtocolHandle--->WtcClient-->WtcConnect
一、WtcConnect:获取Tuxedo的连接:
public class WtcConnect { static Context ctx; static TuxedoConnectionFactory tcf; static { try { ctx = new InitialContext(); tcf = (TuxedoConnectionFactory) ctx. lookup("tuxedo.services.TuxedoConnection"); } catch (NamingException ne) { try { throw new WtcException(TPException.TPENOENT, "Could not get the tuxedo object: " + ne); } catch (WtcException e) { e.printStackTrace(); } } } public static TuxedoConnection getConnection() { TuxedoConnection conn = null; try { conn = tcf.getTuxedoConnection(); } catch (TPException e) { e.printStackTrace(); } return conn; } }
二、WtcClient类:
FldMessage---》fml32SetValue()---》TypedFML32---》tpCallFML32()----》TypedFML32----》fml32GetValue()---》FldMessage
1、FldMessage:
public class FldMessage { private Integer ver; private String reqsys; private String reqnode; private String txcode; private Integer reqno; private String encode; private String dynauth; private Integer mac; private String data; private String rspsys; private String rspnode; private Integer rspno; private Integer rspstat; }
2、tpCallFML32()
public boolean tpCallFML32() throws TPException, TPReplyException, Ferror { TuxedoConnection tuxConn; Reply tuxReply; XmlFmlCnv c = new XmlFmlCnv(); if (fml32SetValue() == false) { return false; } tuxConn = WtcConnect.getConnection();//获取连接 try { tuxReply = tuxConn.tpcall(this.service, sndFmlBuffer, 0);//调用服务 rcvFmlBuffer = (TypedFML32) tuxReply.getReplyBuffer();//获取返回数据 tuxConn.tpterm();//关闭连接 } catch (TPReplyException tre) { logger.error("tpcall threw TPReplyExcption " + tre); throw tre; } catch (TPException te) { logger.error("tpcall threw TPException " + te); throw te; } catch (Exception ee) { logger.error("tpcall threw exception: " + ee); throw new TPException(TPException.TPESYSTEM, "Exception: " + ee); } finally { tuxConn.tpterm(); } if (rcvFmlBuffer == null) { logger.error("响应数据报文为空"); return false; } if (fml32GetValue() == false) { return false; } return true; }
3、fml32SetValue()【 FldMessage---》fml32SetValue()---》TypedFML32】
sndFmlBuffer.Fchg(sndFmlBuffer.Fldid("PHF_VER"), 0, sndFldMsg.getVer());//调用的是:TypedFML32.Fchg(TypedFML32.Fldid("字段名称"), 0, "字段值");
4、fml32GetValue()【TypedFML32----》FldMessage】
Object obj = null; rcvFldMsg = new FldMessage(); TypedFML32 fml32 = new TypedFML32(new CnacexFld()); if (fldExist(rcvFmlBuffer, fml32.Fldid("PHF_VER"))) { obj = rcvFmlBuffer.Fget(fml32.Fldid("PHF_VER"), 0);//从TypedFML32中获取字段值 if (obj != null) rcvFldMsg.setVer(Integer.parseInt(obj.toString().trim())); } }
5、fldExist(TypedFML32 fml32, int filId)
private boolean fldExist(TypedFML32 fml32, int filId) { try { fml32.Fget(filId, 0); } catch (Ferror e) { if (e.getFerror() == e.FNOTPRES) { return false; } return true; } return true; }
三、ProtocolHandle类:
public class ProtocolHandle { private int requestNo; private ConfigData config = ConfigData.getInstance(); /** * 底层数据交换 */ public PackageModul handle(ProtocolCode code, PackageModul modul) { int reqNo = 0; synchronized (this) { if (requestNo++ > 1000000) requestNo = 0; reqNo = requestNo; } WtcClient wtcClient = new WtcClient(); // 组装包内容 CRC32 crc32 = new CRC32(); FldMessage request = new FldMessage(); request.setVer(config.getProtocolVersion()); request.setReqsys(config.getReqSysNo()); request.setTxcode(code.getKey()); request.setReqno(reqNo & 0x7FFFFFFF); // 组装body内容 String nameSpace = getRandomString(1); String context = XmlUtil.toXml(modul,nameSpace); int index = context.lastIndexOf("</"+nameSpace+":root>"); context = context.substring(0, index) + modul.getBody() + context.substring(index); //设置crc32校验码 try { crc32.update(context.getBytes("utf-8")); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } request.setMac((int) crc32.getValue() & 0x7FFFFFFF); //设置传送数据 request.setData(context); //设置要请求的服务 wtcClient.setService(code.getService()); wtcClient.setSndFldMsg(request); try { wtcClient.tpCallFML32(); } catch (TPReplyException e) { throw new ProtocolException(this, reqNo, e); } catch (TPException e) { throw new ProtocolException(this, reqNo, e); } catch (Ferror e) { throw new ProtocolException(this, reqNo, e); } FldMessage response = wtcClient.getRcvFldMsg(); if (response == null) throw new ProtocolException(this, reqNo, new NullPointerException()); String data = response.getData();//获取tuxedo返回的数据 response.getReqno(), data); crc32.update(data.getBytes()); PackageModul result = XmlUtil.fromXml(data, PackageModul.class); if (response.getRspstat() == 0) { // 分解BODY内容 int start = data.indexOf(Constants.BODY_START); int end = data.lastIndexOf(Constants.BODY_END) + Constants.BODY_END.length(); String body = null; if (start >= 0 && end >= 0) { body = data.substring(start, end); data = data.substring(0, start) + data.substring(end); } result.setBody(body); result.setFault(null); } result.setReqNo(reqNo); return result; } private String getRandomString(int length) { //length表示生成字符串的长度 String base = "abcdefghijklmnopqrstuvwxyz"; Random random = new Random(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < length; i++) { int number = random.nextInt(base.length()); sb.append(base.charAt(number)); } return sb.toString(); } }