旺旺CTO 2011-09-30
Java中使用Jolt访问Tuxedo服务–Tomcat环境部署测试
最近在学习基于Tuxedo的系统架构,网上讨论最多的,比较流行的3层架构是基于Weglogic+Tuxedo+DB的模式,关于这类模式的文章也比较多,可以参见链接:
http://blog.csdn.net/liwei_cmg/article/details/769150
一般来说,Java可用使用3种联机方式访问Tuxedo的服务:
1.WTC用于Weblogic与Tuxedo的互访,可以实现双向的调用。
2.JOLT用于Tomcat,Weblogic,Websphere和其他应用服务器访问Tuxedo,为单向调用。
3.CORBA(网上有介绍,自己没实践过)
作为学习了解Java如何通过Jolt调用Tuxedo服务,以及如何使用Jolt的链接池技术,本文没有使用Weblogic作为应用服务器,而是直接在Tomcat应用服务器中配置部署了Html+Servlet来调用Tuxedo的服务。
实现环境:
服务端:GUN/Linux2.6.32+Tuxedo11gR1
应用服务器:Apache-tomcat-6.0.29forWindows
开发工具:Myeclipse8.5
实现步骤如下:
1)准备Tuxedo服务程序
2)修改配置ubbconfig
3)修改Jolt访问服务的jrepository文件
4)启动Tuxedo服务
5)创建Webproject
6)准备Servlet和html程序
7)准备Jolt连接池配置文件
8)配置web.xml文件
9)部署Web项目simpapp
10)Linux服务器防火墙设置
1.准备Tuxedo服务程序
这里我们还是用examples中的simpserv.c程序,以及TOUPPER服务,比较容易。
//simpserv.c
#include<stdio.h>
#include<ctype.h>
#include<atmi.h>/*TUXEDOHeaderFile*/
#include<userlog.h>/*TUXEDOHeaderFile*/
#ifdefined(__STDC__)||defined(__cplusplus)
tpsvrinit(intargc,char*argv[])
#else
tpsvrinit(argc,argv)
intargc;
char**argv;
#endif
{
/*Somecompilerswarnifargcandargvaren'tused.*/
argc=argc;
argv=argv;
/*userlogwritestothecentralTUXEDOmessagelog*/
userlog("Welcometothesimpleserver2");
return(0);
}
#ifdef__cplusplus
extern"C"
#endif
void
#ifdefined(__STDC__)||defined(__cplusplus)
TOUPPER(TPSVCINFO*rqst)
#else
TOUPPER(rqst)
TPSVCINFO*rqst;
#endif
{
inti;
for(i=0;i<rqst->len-1;i++)
rqst->data[i]=toupper(rqst->data[i]);
/*Returnthetransformedbuffertotherequestor.*/
tpreturn(TPSUCCESS,0,rqst->data,0L,0);
}
编译服务程序,
buildserver-fsimpserv.c-osimpserv-sTOUPPER
2.修改配置ubbconfig,加入如下的组和服务
*GROUPS
JSLGRPLMID=simpapp1GRPNO=101
JREPGRPLMID=simpapp1GRPNO=102
*SERVERS
JSLSRVGRP=JSLGRPSRVID=1
CLOPT="-A---n//192.168.1.100:8850-m5-M5-x10"
JREPSVRSRVGRP=JREPGRPSRVID=1
CLOPT="-A---W–P/home/tuxedo/udataobj/jolt/repository/jrepository"
JSL为Java通过Jolt访问Tuxedo的监听服务,
//192.168.1.100:8850为Tuxedo服务器的地址和端口,在Java客户端要用。
(注意:Jolt不通过WSL来访问Tuxedo服务。)
编译ubbconfig配置文件,
tmloadcf–yubbconfig
3.修改Jolt访问服务的jrepository文件,加入TOUPPER服务的定义如下(也可以采用RE.html配置):
addSVC/TOUPPER:vs=1:ex=1:bt=STRING:\
bp:pn=STRING:pt=string:pf=167772161:pa=rw:ep:
4.启动Tuxedo服务
服务端配置完毕。
5.在Myeclipse下创建Webproject,项目名为simpapp,位于d:\worksapce\simpapp
创建新的src包,my.jolt.servlet,位于d:\worksapce\simpapp\src
6.准备Servlet和html程序
程序来自Tuxedo的examples,调试的时候进行了修改。
网页程序simp.html,d:\worksapce\simpapp\WebRoot\simp.html
<html>
<head>
<title>JoltSimpAppExample</title>
</head>
<!--
--Copyright(c)1998-99BEASystems,Inc.Allrightsreserved
--
--THISISSOURCECODEPUBLISHEDFORDEMONSTRATIONPURPOSES
-->
<bodybgcolor=#FFFFFF>
<fontface="Helvetica">
<h1>
<fontcolor=#DB1260>
JoltSimpAppExample
</font>
</h1>
<blockquote>
ThisexamplesdemonstrateshowaJavaHTTPServletrunninginthe
WeblogicServerservicesaPOSTrequestfroma
<fontface="CourierNew"color=#00aaaasize=-1><FORM></font>in
thisHTMLfile.The<fontface="CourierNew"size=-1>simpapp</font>
servletinvokesaserviceontheBEATUXEDOServerthatconvertsthe
textyouenterhereintouppercase.Theresultispostedbackinside
aservlet-generatedhtmlfile.
<formname="simpapp"action="/simpapp/simpappSrv"method="post">
<tablebgcolor=#ddddddborder=1>
<tr>
<td>TypesometexthereandclickthePostbutton:
<!--Notethat"STRING"mustbeinuppercase!-->
<inputtype="text"name="STRING"></td>
</tr>
<tr>
<tdalign=center><inputtype="submit"value="Post!">
</td></tr>
</table>
</form>
</blockquote>
<p>
Clickherefor<AHREF=PoolList.jhtml>sessionpoolsstatistics</A>
</font>
</body>
</html>
原程序中一行如下,调试的时候报错,删掉了。
<inputtype="hidden"name="SVCNAME"value="TOUPPER">
错误如下:
严重:Servlet.service()forservletSimpAppServletthrewexception
java.lang.NoSuchFieldError:SVCNAME
Servlet程序,SimpAppServlet.java,位于d:\worksapce\simpapp\src\my\jolt\servlet
packagemy.jolt.servlet;
importbea.jolt.pool.servlet.*;
importbea.jolt.pool.ApplicationException;
importbea.jolt.pool.SessionPoolException;
importbea.jolt.pool.ServiceException;
importjava.util.Properties;
importjava.util.Hashtable;
importjava.io.IOException;
importjavax.servlet.*;
importjavax.servlet.http.*;
/**
*ThisexampledemonstrateshowaServletmayconnecttoTuxedo
*andcallupononeofitsservices;itshouldbeinvokedfromthe
*<i>simpapp.html</i>file.Theservletcreatesasessionpool
*manageratinitialization,whichisusedtoobtainasessionwhenthe
*<tt>doPost()</tt>methodisinvoked.Thissessionisusedtoconnecttoaservice
*inTuxedowithanamedescribedbytheposted"SVCNAME"argument.Inthis
*exampletheserviceiscalled"TOUPPER",whichtransposestheposted
*"STRING"argumenttextintouppercase,andreturnstheresult.Thisis
*returnedtotheclientbrowserwithinsomegeneratedHTML.
*THISISSOURCECODEPUBLISHEDFORDEMONSTRATIONPURPOSES
*
*@authorCopyright(c)1999BEASystems,Inc.Allrightsreserved.
*/
publicclassSimpAppServletextendsHttpServlet{
/**
*Privatevariabletoholdthepoolmanagerobject.
*/
privateServletSessionPoolManagerb_mgr;
/**
*Initializestheservlet.Thesessionpoolmanagerandthe
*simpappsessionpoolisestablishedhere.Thepropertiesof
*thesessionpoolisspecifiedthroughanexternalproperty
*filewhosepathcomesfromtheservletinitialparameter"properties".
*
*@paramconfigServletconfiguration
*@exceptionServletExceptioniftheservletfails
*/
publicvoidinit(ServletConfigconfig)throwsServletException{
super.init(config);
try{
//Createasessionpoolandgetthesessionpoolmanagerthrough
//apropertyfile.
Stringpath=config.getServletContext().getRealPath("/")+
"simpapp.properties";
Propertiesprop=ServletPoolManagerConfig.load(path);
if(prop==null)
thrownewServletException(path+"notfound");
ServletPoolManagerConfig.startup(prop);
b_mgr=ServletPoolManagerConfig.getSessionPoolManager();
}
catch(Exceptione){
thrownewServletException(e.toString());
}
}
/**
*Destroysthisservlet.TheServletSessionPoolManager
*resourceisdeallocated.
*/
publicvoiddestroy(){
b_mgr=null;
}
/**
*ImplementstheHttpServlet<tt>doPost()</tt>method.
*ThismethodexpectsPOSTedargumentsfor:
*<dl>
*<dt>"SVCNAME"<dd>ThenameoftheservicetobeinvokedinTuxedo.
*<dt>"STRING"<dd>Thetexttobetransposedtouppercase.
*</dl>
*
*<p>Seetheprovidedsimpapp.htmlfortheHTMLform
*usedtosubmitthedata.
*/
publicvoiddoPost(HttpServletRequestreq,HttpServletResponseresp)
throwsServletException,IOException
{
ServletResultresult;
ServletOutputStreamout=resp.getOutputStream();
out.println("<html><head><title>JoltSimpAppExampleResponse</title></head>");
out.println("<body><fontface=\"Helvetica\">"+
"<h2><fontcolor=#DB1260>"+
"ThisistheresponsefromtheSimpAppServlet:"+
"</font></h2>");
//Getthe"simpapp"sessionpool
ServletSessionPoolsession=(ServletSessionPool)
b_mgr.getSessionPool("simpapp");
if(session==null){
out.println("TheservletfailedtoobtainaSessionPoolforsimpapp."+
"<br>"+
"PossiblytheTuxedoserverisnotrunning,"+
"orthereisaconfigurationproblem."+
"</font></body></html>");
out.close();
return;
}
//Stringsvcnm[]=req.getParameterValues("SVCNAME");
//Invokeaserviceandgettheresult.Processthe
//templatewithinputandresultifthereisnoerror.
try{
//result=session.call(svcnm[0],req);
Result=session.call("TOUPPER");
//Noerror;presenttheresultpage.
out.println("Thesimpappsevicewassuccessfullycalled,and"+
"respondedwiththeoutputstring:");
out.println("<p><center><fontsize=+1><b>"+
result.getValue("STRING","")+
"</b></font></center><p><hrwidth=80%>");
}
catch(SessionPoolExceptione){
//Allsessionsarebusy.
out.println("Yourrequestcannotbecompletedatthismoment.\n"+
"<p>DiagnosticMessageis:"+e.getMessage()+"<p>\n"+
"<fontcolor=blue>Possiblereasons:<br></font><ul>\n"+
"<li>Nosessionsareavailable\n"+
"<li>Thesessionpoolissuspended\n"+
"<li>Thesessionpoolisshutdown</ul>\n"+
"<p>Pleaseresubmityourrequestlater.");
}
catch(ServiceExceptione){
//ThereisaServiceException.
out.println("<dl><dt>Serviceexception</dt>"+
"<dd>Errormessage:"+e.getMessage()+
"<p>Errornumber:"+e.getErrno()+
"</dl>");
}
catch(ApplicationExceptione){
//Thereisanapplicationerror.
result=(ServletResult)e.getResult();
out.println("<h3>Applicationerror</h3>\n"+
"<p>Applicationcodeis"+result.getApplicationCode());
}
catch(Exceptione){
out.println("<h3>Unexpectedexception</h3>"+
"<p>Exceptionis"+
"<br><fontface=\"CourierNew\"size=-1"+e+"</font>");
}
out.println("</font></body></html>\n");
out.close();
}
/**
*Implementsthe<tt>doGet</tt>HttpServletmethod.Thissimply
*invokesthe<tt>doPost</tt>method.
*/
publicvoiddoGet(HttpServletRequestreq,HttpServletResponseresp)
throwsServletException,IOException
{
doPost(req,resp);
}
}
注释下面的代码,
//Stringsvcnm[]=req.getParameterValues("SVCNAME");
//result=session.call(svcnm[0],req);
加入如下代码,直接调用TOUPPER服务,
Result=session.call("TOUPPER",req);
7.准备Jolt连接池配置文件,simpapp.properties
#simpapp
#FriApr1600:43:30PDT1999
poolname=simpapp
appaddrlist=//192.168.1.100:8500
failoverlist=//192.168.1.100:8500
minpoolsize=1
maxpoolsize=3
userrole=tester
apppassword=appPass
username=guest
userpassword=myPass
将配置文件放在D:\tomcat\webapps\simpapp\simpapp.properties
8.配置web.xml文件,加入SimpAppServlet的配置
此文件在D:\tomcat\webapps\simpapp\WebRoot\WEB-INF\web.xml,
<servlet>
<description>ddddddd</description>
<display-name>SimpAppServlet</display-name>
<servlet-name>SimpAppServlet</servlet-name>
<servlet-class>my.jolt.servlet.SimpAppServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SimpAppServlet</servlet-name>
<url-pattern>/simpappSrv</url-pattern>
</servlet-mapping>
9.启动Tomcat,将Web项目simpapp部署到Tomcat,测试通过。
10.如果客户,服务在两台不同的机器上,这个例子中就用了Windows和Linux两台机器。出现调用失败的情况,可能是Linux的防火墙需要设置,将8500-8599的端口开放就可以了。也可以只开发8500之后的5-10个端口也是可以的,设置如下:
以ROOT身份登录Linux,查看serviceiptables的状态,如果开启,要进行路由设置。
[tuxedo@dcard~]$serviceiptablesstatus
用VI编辑配置文件:
[tuxedo@dcard~]$vi/etc/sysconfig/iptables
加入
-AINPUT-ptcp-mtcp--dport8850:8899-jACCEPT
或
-AINPUT-ptcp-mtcp--dport8850:8859-jACCEPT
在命令行下,重新启动iptables服务就可以了。
[tuxedo@dcard~]$Serviceiptablesrestart