Java读写报文函数的结构
1 定义存放报文的数据输出流和字节数组。为每一个入参定义一个字节数组;
2 调用拼接报文头的函数;
3 根据入参的类型分别调用不同的函数把入参放入相应的字节数组中;
4 按着报文头、参数长度、参数的顺序把字节数组放入数据输出流,再把输出流转换成报文字节数组;
5 连接Tuxedo,定义TypedCArray类型的Buffer,并分配空间;
6 把入参报文字节数组附给Buffer的carray属性,再把字节数组的实际长度附给Buffer的sendSize属性;
7 调用Tuxedo服务,接收返回的报文,中断与Tuxedo的连接;
8 从返回报文中读取出参个数;
9 判断交易是否执行成功,如果没有成功,读取错误信息,抛出异常;
10 计算数据区的起始位置,根据出参的类型调用相应的函数从报文中读取数据;
11 具体实例,请见附录I
附录I
C交易原型:
int trReadTrade(
char *strsql, //INPUT
struct Tickets *stTickets,
short *iCount, struct TotalStru *stTotalStru //OUTPUT
) ;
java函数:
public ArrayList readTrade(String strsql, Tickets tickets) throws testException {
ArrayList totalList = new ArrayList();
CommonPack pack = new CommonPack();
int iOutCnt = 0;
String tcode = "T03"; //交易码
Context ctx;
TuxedoConnectionFactory tcf;
TuxedoConnection myTux = null; // For now we get it via NEW until the Factory works
TypedCArray myData;
Reply myRtn;
// 此处开始组织报文
ByteArrayOutputStream bout = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(bout);
byte[] inbt ; //入参报文
byte[] headbt; //报文头
byte[] databt; //存放strsql
byte[] databt1; //存放Tickets结构
short PNum = 2; //按C语言的格式确定出参数
try {
// 产生交易报文头
headbt = tapiPub.MakeHead(tcode, SYS_MyOffice, SYS_Myself, SYS_CurrTime,
SYS_WorkDate, SYS_OptType, PNum);
// 把入参strsql放入byte数组
databt = strsql.getBytes();
// 把入参tickets放入byte数组
databt1 = pack.StructureWrite(tickets);
//组织报文(DataOutputStream类型),顺序:报文头+各参数长度+第一个入参+第二个参数+......+第n个参数
dout.write(headbt); //放入报文头
tapiPub.WriteParaLength(dout, databt.length); //放入strsql的长度
tapiPub.WriteParaLength(dout, databt1.length); //放入tickets的长度
dout.write(databt); //放入strsql
dout.write(databt1); //放入tickets
}
catch (Exception e) {
throw new testException(e.getMessage());
}
// 把DataOutputStream型报文转换成Tuxedo需要的Byte类型
inbt = bout.toByteArray();
log("toupper called, converting " );
try {
ctx = new InitialContext();
tcf =
(TuxedoConnectionFactory) ctx.lookup("tuxedo.services.TuxedoConnection");
} catch (NamingException ne) {
// Could not get the tuxedo object, throw TPENOENT
String ErrMsg = TPException.TPENOENT + "Could not get TuxedoConnectionFactory : " + ne;
throw new testException(ErrMsg);
}
try{
myTux = tcf.getTuxedoConnection();
}
catch(Exception e){
throw new testException(e.getMessage());
}
// 定义TypedCArray类型的Buffer,并分配空间,用于数据传递
myData = new TypedCArray(myMessage.MAX_DATA_SIZE.intValue());
// 把Byte类型的报文,附给Buffer的carray属性
myData.carray = inbt;
// 给Buffer的sendSize属性附上报文的实际长度
myData.sendSize = inbt.length;
log("About to call tpcall");
try {
// 调用Tuxedo服务,用Reply对象接收服务返回结果
myRtn=
myTux.tpcall("READTRADE",myData, ApplicationToMonitorInterface.TPNOTRAN);
} catch (TPReplyException tre) {
throw new testException(tre.getMessage());
} catch (TPException te) {
throw new testException(te.getMessage());
} catch (Exception ee) {
throw new testException(ee.getMessage());
}
log("tpcall successfull!");
// 接收服务返回的出参
myData = (TypedCArray) myRtn.getReplyBuffer();
myTux.tpterm(); // Closing the association with Tuxedo
byte outbt[]; // Byte类型的出参报文
outbt=myData.carray;
// 取出出参个数
iOutCnt = tapiPub.ReadOutPNum(outbt);
//判断交易是否成功,如果成功,从报文中取出参;如果失败,取出错误代码和错误信息,直接返回
if (tapiPub.IsSucess(outbt, iOutCnt) != 0) {
throw new testException(tapiPub.GetErrMsg(outbt, iOutCnt));
}
//此处解包
int iNum = 0; //出参序号
//报文数据区开始位置
int iPos = myMessage.TDATA_HEADER_SIZE.intValue() +
myMessage.USIGNED_SHORT_SIZE.intValue()*(iOutCnt+1);
//用 getIntFromMsg方法取出出参iCount
short iCount = transPub.getShortFromMsg(iNum, iPos, outbt, iOutCnt);
iPos += tapiPub.getOutParamLength(iNum, outbt, iOutCnt);
iNum++;
// 给出参ArrayList分配空间
for(int i = 0; i <(int)iCount; i++){
TotalStru totalvo = new TotalStru();
totalList.add(totalvo);
}
// 从报文中取出出参ArrayList
iPos = pack.StructureListRead (outbt, totalList, iPos);
iNum++;
return totalList;
}
wtc编程
最新推荐文章于 2023-01-09 21:53:33 发布