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/Linux 2.6.32 + Tuxedo 11gR1
应用服务器: Apache-tomcat-6.0.29 for Windows
开发工具: Myeclipse 8.5
实现步骤如下:
1)准备Tuxedo服务程序
2)修改配置ubbconfig
3)修改Jolt访问服务的jrepository文件
4)启动Tuxedo服务
5)创建Web project
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> /* TUXEDO Header File */
#include <userlog.h> /* TUXEDO Header File */
#if defined(__STDC__) || defined(__cplusplus)
tpsvrinit(int argc, char *argv[])
#else
tpsvrinit(argc, argv)
int argc;
char **argv;
#endif
{
/* Some compilers warn if argc and argv aren't used. */
argc = argc;
argv = argv;
/* userlog writes to the central TUXEDO message log */
userlog("Welcome to the simple server 2");
return(0);
}
#ifdef __cplusplus
extern "C"
#endif
void
#if defined(__STDC__) || defined(__cplusplus)
TOUPPER(TPSVCINFO *rqst)
#else
TOUPPER(rqst)
TPSVCINFO *rqst;
#endif
{
int i;
for(i = 0; i < rqst->len-1; i++)
rqst->data[i] = toupper(rqst->data[i]);
/* Return the transformed buffer to the requestor. */
tpreturn(TPSUCCESS, 0, rqst->data, 0L, 0);
}
编译服务程序,
buildserver -f simpserv.c -o simpserv -s TOUPPER
2.修改配置ubbconfig, 加入如下的组和服务
*GROUPS
JSLGRP LMID=simpapp1 GRPNO=101
JREPGRP LMID=simpapp1 GRPNO=102
*SERVERS
JSL SRVGRP=JSLGRP SRVID=1
CLOPT="-A -- -n //192.168.1.100:8850 -m 5 -M 5 -x 10"
JREPSVR SRVGRP=JREPGRP SRVID=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 –y ubbconfig
3.修改Jolt访问服务的jrepository文件,加入TOUPPER服务的定义如下(也可以采用RE.html配置):
add SVC/TOUPPER:vs=1:ex=1:bt=STRING:\
bp:pn=STRING:pt=string:pf=167772161:pa=rw:ep:
4.启动Tuxedo服务
服务端配置完毕。
5. 在Myeclipse下创建Web project,项目名为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>Jolt SimpApp Example</title>
</head>
<!--
-- Copyright (c) 1998-99 BEA Systems, Inc. All rights reserved
--
-- THIS IS SOURCE CODE PUBLISHED FOR DEMONSTRATION PURPOSES
-->
<body bgcolor=#FFFFFF>
<font face="Helvetica">
<h1>
<font color=#DB1260>
Jolt SimpApp Example
</font>
</h1>
<blockquote>
This examples demonstrates how a Java HTTP Servlet running in the
Weblogic Server services a POST request from a
<font face="Courier New" color=#00aaaa size=-1><FORM></font> in
this HTML file. The <font face="Courier New" size=-1>simpapp</font>
servlet invokes a service on the BEA TUXEDO Server that converts the
text you enter here into uppercase. The result is posted back inside
a servlet-generated html file.
<form name="simpapp" action="/simpapp/ simpappSrv " method="post">
<table bgcolor=#dddddd border=1>
<tr>
<td>Type some text here and click the Post button:
<!-- Note that "STRING" must be in upper case! -->
<input type="text" name="STRING"></td>
</tr>
<tr>
<td align=center><input type="submit" value="Post!">
</td></tr>
</table>
</form>
</blockquote>
<p>
Click here for <A HREF=PoolList.jhtml>session pools statistics</A>
</font>
</body>
</html>
原程序中一行如下,调试的时候报错,删掉了。
<input type="hidden" name="SVCNAME" value="TOUPPER">
错误如下:
严重: Servlet.service() for servlet SimpAppServlet threw exception
java.lang.NoSuchFieldError: SVCNAME
Servlet程序,SimpAppServlet.java,位于 d:\worksapce\simpapp\src\my\jolt\servlet
package my.jolt.servlet;
import bea.jolt.pool.servlet.*;
import bea.jolt.pool.ApplicationException;
import bea.jolt.pool.SessionPoolException;
import bea.jolt.pool.ServiceException;
import java.util.Properties;
import java.util.Hashtable;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;
/**
* This example demonstrates how a Servlet may connect to Tuxedo
* and call upon one of its services; it should be invoked from the
* <i>simpapp.html</i> file. The servlet creates a session pool
* manager at initialization, which is used to obtain a session when the
* <tt>doPost()</tt> method is invoked. This session is used to connect to a service
* in Tuxedo with a name described by the posted "SVCNAME" argument. In this
* example the service is called "TOUPPER", which transposes the posted
* "STRING" argument text into uppercase, and returns the result. This is
* returned to the client browser within some generated HTML.
* THIS IS SOURCE CODE PUBLISHED FOR DEMONSTRATION PURPOSES
*
* @author Copyright (c) 1999 BEA Systems, Inc. All rights reserved.
*/
public class SimpAppServlet extends HttpServlet {
/**
* Private variable to hold the pool manager object.
*/
private ServletSessionPoolManager b_mgr;
/**
* Initializes the servlet. The session pool manager and the
* simpapp session pool is established here. The properties of
* the session pool is specified through an external property
* file whose path comes from the servlet initial parameter "properties".
*
* @param config Servlet configuration
* @exception ServletException if the servlet fails
*/
public void init(ServletConfig config) throws ServletException {
super.init(config);
try {
// Create a session pool and get the session pool manager through
// a property file.
String path = config.getServletContext().getRealPath("/") +
"simpapp.properties";
Properties prop = ServletPoolManagerConfig.load(path);
if (prop == null)
throw new ServletException(path + " not found");
ServletPoolManagerConfig.startup(prop);
b_mgr = ServletPoolManagerConfig.getSessionPoolManager();
}
catch (Exception e) {
throw new ServletException(e.toString());
}
}
/**
* Destroys this servlet. The ServletSessionPoolManager
* resource is deallocated.
*/
public void destroy() {
b_mgr = null;
}
/**
* Implements the HttpServlet <tt>doPost()</tt> method.
* This method expects POSTed arguments for:
* <dl>
* <dt>"SVCNAME"<dd>The name of the service to be invoked in Tuxedo.
* <dt>"STRING"<dd>The text to be transposed to uppercase.
* </dl>
*
* <p> See the provided simpapp.html for the HTML form
* used to submit the data.
*/
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
ServletResult result;
ServletOutputStream out = resp.getOutputStream();
out.println("<html><head><title>Jolt SimpApp Example Response</title></head>");
out.println("<body><font face=\"Helvetica\">" +
"<h2><font color=#DB1260>" +
"This is the response from the SimpAppServlet:" +
"</font></h2>");
// Get the "simpapp" session pool
ServletSessionPool session = (ServletSessionPool)
b_mgr.getSessionPool("simpapp");
if (session == null) {
out.println("The servlet failed to obtain a SessionPool for simpapp. "+
"<br>"+
"Possibly the Tuxedo server is not running, "+
"or there is a configuration problem."+
"</font></body></html>");
out.close();
return;
}
//String svcnm[] = req.getParameterValues("SVCNAME");
// Invoke a service and get the result. Process the
// template with input and result if there is no error.
try {
//result = session.call(svcnm[0], req);
Result = session.call("TOUPPER");
// No error; present the result page.
out.println("The simpapp sevice was successfully called, and "+
"responded with the output string: ");
out.println("<p><center><font size=+1><b>"+
result.getValue("STRING", "")+
"</b></font></center><p><hr width=80%>");
}
catch (SessionPoolException e) {
// All sessions are busy.
out.println("Your request cannot be completed at this moment.\n"+
"<p>Diagnostic Message is: "+e.getMessage()+"<p>\n"+
"<font color=blue>Possible reasons:<br></font><ul>\n"+
"<li>No sessions are available\n"+
"<li>The session pool is suspended\n"+
"<li>The session pool is shutdown</ul>\n"+
"<p>Please resubmit your request later.");
}
catch (ServiceException e) {
// There is a Service Exception.
out.println("<dl><dt>Service exception</dt>"+
"<dd>Error message:"+e.getMessage()+
"<p>Error number:"+e.getErrno()+
"</dl>");
}
catch (ApplicationException e) {
// There is an application error.
result = (ServletResult) e.getResult();
out.println("<h3>Application error</h3>\n"+
"<p>Application code is "+result.getApplicationCode());
}
catch (Exception e) {
out.println("<h3>Unexpected exception</h3>"+
"<p>Exception is "+
"<br><font face=\"Courier New\" size=-1"+e+"</font>");
}
out.println("</font></body></html>\n");
out.close();
}
/**
* Implements the <tt>doGet</tt> HttpServlet method. This simply
* invokes the <tt>doPost</tt> method.
*/
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
doPost(req, resp);
}
}
注释下面的代码,
//String svcnm[] = req.getParameterValues("SVCNAME");
//result = session.call(svcnm[0], req);
加入如下代码,直接调用TOUPPER 服务,
Result = session.call("TOUPPER",req);
7.准备Jolt 连接池配置文件, simpapp.properties
#simpapp
#Fri Apr 16 00:43:30 PDT 1999
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,查看 service iptables 的状态,如果开启,要进行路由设置。
[tuxedo@dcard ~]$ service iptables status
用VI编辑配置文件:
[tuxedo@dcard ~]$ vi /etc/sysconfig/iptables
加入
-A INPUT -p tcp -m tcp --dport 8850:8899 -j ACCEPT
或
-A INPUT -p tcp -m tcp --dport 8850:8859 -j ACCEPT
在命令行下,重新启动 iptables服务就可以了。
[tuxedo@dcard ~]$ Service iptables restart