Wiki source code of Deployment-Tomcat Deployment

Version 16.1 by Pascal Robert on 2010/09/13 00:14

Show last authors
1 This article was written by Andrew Lindesay ([[http://www.lindesay.co.nz]]) around May 2005. It first appeared as LaTeX PDF and has been transcribed into this Wiki. You use the information contained in this document at your own risk. Please contact the author if you feel there may have been an error in the conversion to Wiki markup.
2
3 More information on tomcat deployment using Eclipse/WOLips, including information on how to deploy Project Wonder applications, is available [[here>>WONDER:Creating a wonder app to deploy as a servlet]]
4
5 |= Contents
6 | {{toc style="disc"}}{{/toc}}
7
8 = Abstract =
9
10 From WebObjects 5.2, it has been possible to derive a build product from a WebObjects application project that can be deployed into a J2EE servlet container. This article shows how it is possible to
11 deploy a WebObjects 5.2 application into a Tomcat environment and achieve a very similar topology to the "native" WebObjects deploy.
12
13 = Introduction =
14
15 This document was originally written assuming a Tomcat 5 deployment, but after some difficulties with web services and AXIS, I have modified this document to also cater for a Tomcat 3 deployment. This document covers both circumstances.
16
17 = Assumptions =
18
19 This article assumes the following:
20
21 * WebObjects 5.2 (likely to work fine with newer versions)
22 * Java 1.4
23 * Latest Tomcat release of version 5 (5.5.12 at the time of writing) or 3 (3.3.2 at the time of writing)
24 * Some sort of UNIX deployment.
25 * The reader has some conception of the concepts behind servlet technology.
26 * The reader is familiar with a standard WebObjects deployment topology which will be referred to as //wotaskd// deployment.
27
28 For the purposes of this document, it is assumed that Tomcat has been installed at a directory on the local disc called ##$TOMCATDIR##. It is also assumed that you will have another directory with the files required to configure and run an instance called ##$INSTDIR##. It is assumed also that you will have a directory called ##$JKDIR## with the Tomcat apache adaptor in it. In this article, some configuration files require the paths to be shown and these are tabulated below.
29
30 | ##$TOMCATDIR## | ##/opt/tomcat##
31 | ##$INSTDIR## | ##/opt/fooapp##
32 | ##$JKDIR## | ##/opt/modjk## |
33 In reality, these directories could be located anywhere.
34
35 == Objective ==
36
37 The objective of this article is to show that a WebObjects application can be deployed into a servlet container and keep some of the desirable attributes of a WebObjects deployment topology. Some of these traits are itemised below.
38
39 * Clustering over a number of hardware nodes to prevent system downtime from a single hardware failure incident.
40 * Clustering over a number of virtual machine //instances// on each hardware node to avoid downtime from a single software failure incident.
41 * Ability to make efficient use of lower cost server hardware rather than encouraging use of large, expensive servers.
42 * Make maximum use of memory availabile in each virtual machine as cache to minimise database traffic and lower stress on the database server.
43 * Ability to make sessions "sticky" to a given virtual machine //instance//, whilst being multiplexed through a single web server front-end adaptor.
44 * Ability to load-balance requests to instances which are operational.
45
46 A standard WebObjects deployment topology is shown in the figure below alongside what is to be achieved with Tomcat. A typical J2EE deployment may have a different topology from this.
47
48 [[image:wo-tomcat-deploy-topology.gif]]
49
50 = Servlet Build Products =
51
52 Creating a servlet build product from a WebObjects application project is covered in some depth by documentation that is supplied by Apple for WebObjects. This is not going to be repeated here, but here is a brief overview of the process.
53
54 * Include the ##JavaWOJSPServlet.framework## framework into your project.
55 * In the build settings, set the ##SERVLET//SINGLE//DIR//DEPLOY//##// value to ##YES## to create the most trouble-free form of deployment servlet.//
56 * Edit the ##SERVLET//DEPLOY//LICENSE## to contain your valid deployment license key if you need one for the version of WebObjects you are using.
57 * Edit the ##SERVLET//WEBAPPS//DIR## to point to ##$INSTDIR/webapps/## or some place where you want the build product to go.
58
59 Now when you choose a //Deployment// build, you will also get the servlet assembled. The end result is a directory structure similar to that shown in the figure below.
60
61 [[image:wo-tomcat-ssd-filelayout-3.gif]]
62
63 = Removing the DOCTYPE from .plist Files =
64
65 Many property-list files (often called plist files) have a document type at the top. This can refer to files on a MacOS-X machine or to files on Apple servers. In either case this can cause problems with deployments which are not on MacOS-X servers. The following script can be run with the argument of the ##WEB-INF## folder to remove these. The WebObjects application runs fine without this information in the plist files. This script can easily be incorporated one way or another as a step in the build process for your WebObjects project.
66
67 {{code}}
68
69 # [apl 3.may.2006]
70 # This will remove any DOCTYPE's from the top of plists so that
71 # they do not attempt to validate the DTD which is either
72 # extracted from /System or the internet over HTTP.
73
74 if [ -z $1 ]; then
75 echo "syntax: stripdocype.sh <directory>"
76 exit 1
77 fi
78
79 for PLISTFILE in `find $1 -name *.plist`
80 do
81 sed \
82 '/<!DOCTYPE [Deployment-Tomcat Deployment^>]*>/s/.*//' \
83 $PLISTFILE \
84 > /tmp/remove-plist-temp
85
86 cp /tmp/remove-plist-temp $PLISTFILE
87 done
88
89 {{/code}}
90
91 = Application Configuration with Tomcat =
92
93 A WebObjects project with servlet support has a file called ##web.xml.template## in it. By default, this is located in ##/Resources/Servlet Resources/WEB-INF## in your project. The ##web.xml.template## file is used as a template for creating ##web.xml## which is also known as the ##servlet deployment descriptor##. This deployment descriptor is used as the means of communicating settings to the application as well as the servlet container in which the application runs. This section covers some common changes to that file as well as a discussion around a means of general configuration of the application when it is running inside a servlet container.
94
95 == Data Source for Model Database Configuration ==
96
97 Some WebObjects engineers use the ##setConnectionDictionary(...)## method on a model to set the JDBC database connection parameters for the model. However, the servlet container has it's own ##data source## mechanism for supplying database information which will override any connection dictionary information which is set into the model. If you don't want this to happen, and you want your ##setConnectionDictionary(...)## to take effect, comment out the ##resource-ref## item with the title ##jdbc/DefaultDataSource## in the ##web.xml.template## file.
98
99 If you have more than one data source Resource defined in either your WEB-INF/web.xml or in your META-INF/Context.xml file, WebObjects will complain about there being more than one configuration with this error:
100
101 **##An exception occurred while trying to open a channel: com.webobjects.jdbcadaptor.JDBCAdaptorException: Found multiple data sources. Please map the EOModels to a data source explicitly!##**
102
103 You must set the name parameter of each data source Resource in the web.xml/Context.xml with the same name as your eomodel file. For example:
104
105 EOModel name: **MyModel**.eomodel
106
107 data source Resource definition:
108
109 {{code}}
110
111 <Resource
112 auth="Container"
113 description="MyApp Data Source"
114 HERE -> name="MyModel"
115 type="javax.sql.DataSource"
116 driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
117 url="jdbc:microsoft:sqlserver://serveraddress:1433;databaseName=TEST"
118 username="user"
119 password="pass"
120 maxActive="4"
121 maxWait="5000"
122 maxIdle="10"
123 />
124
125 {{/code}}
126
127 == Serving WebServerResources ==
128
129 To stop the web server resources (images, CSS files and other static data) from being served out of the java environment, you need to configure the ##context-param## with the name ##WOAppMode## to be ##Deployment## in the ##web.xml.template## file.
130
131 == Application Specific Configuration ==
132
133 This area of configuration covers items such as the following ficticious examples;
134
135 1. Email address of person to contact when system fails.
136 1. Frequency of polling some resource.
137 1. Optional connection information for databases.
138 1. GST rate for New Zealand.
139
140 In other words, these are application-specific configuration values. One way to configure your application specific parameters in a servlet container is to load your config into ##env-entry##s in your ##web.xml## file. Here is such an example of one such entry;
141
142 {{code value="xml"}}
143
144 <env-entry>
145 <env-entry-name>foo/nz.co.foo.FooAppMailFrom</env-entry-name>
146 <env-entry-value>bar@foo.co.nz</env-entry-value>
147 <env-entry-type>java.lang.String</env-entry-type>
148 </env-entry>
149
150 {{/code}}
151
152 You can retrieve these value inside the application with some code as shown below. See the LEConfig class from LEWOStuff, the WebObjects framework from Lindesay Electric for an example. LEWOStuff also comes with a tool to help load standard java properties files into the servlet deployment descriptor.
153
154 {{code}}
155
156 import javax.naming.*;
157
158 // ...later in the same class...
159
160 Object valueO = null;
161
162 try
163 {
164 InitialContext context = new InitialContext();
165 valueO = context.lookup("java:comp/env/foo/nz.co.foo.FooAppMailFrom");
166 }
167 catch(javax.naming.NamingException ne)
168 { /* handle gracefully */ }
169
170 {{/code}}
171
172 It is probably easiest to apply these settings in some automated fashion to the ##web.xml## file as part of a further automated build or deploy process.
173
174 == Application Binary ==
175
176 Put your application servlet build product at ##$INSTDIR## such that the following path exists.
177
178 {{noformat}}
179
180 $INSTDIR/webapps/FooApp/WEB-INF
181
182 {{/noformat}}
183
184 == Tomcat Server Configuration Files ==
185
186 You need to create a tomcat configuration file for each of the instances that you would like to have. The pattern is followed here of having an instance number proceeded by the lower case letter "i". Put the first server configuration file at the following location.
187
188 {{noformat}}
189
190 $INSTDIR/server_i1.xml
191
192 {{/noformat}}
193
194 Here is an example of how this file might look. There is no coverage of the individual settings here as the reader is expected to review the tomcat documentation to discover the specific meanings of these settings.
195
196 === Tomcat 5 ===
197
198 {{code value="xml"}}
199
200 <Server port="7071" shutdown="SHUTDOWN">
201 <Service name="Catalina">
202 <Connector port="8081" maxHttpHeaderSize="8192"
203 maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
204 enableLookups="false" acceptCount="100"
205 connectionTimeout="20000" disableUploadTimeout="true" />
206
207 <Connector port="9091" enableLookups="false" protocol="AJP/1.3" />
208
209 <Engine name="i1" defaultHost="appserver1.foo.co.nz" jvmRoute="i1">
210 <Host name="appserver1.foo.co.nz"
211 appBase="/home/fooapp/webapps"
212 unpackWARs="true" autoDeploy="false"
213 xmlValidation="false" xmlNamespaceAware="false">
214 <Context cookies="false" docBase="FooApp"
215 path="FooApp" reloadable="false">
216 <Manager distributable="false" />
217 </Context>
218 </Host>
219 </Engine>
220
221 </Service>
222 </Server>
223
224 {{/code}}
225
226 === Tomcat 3 ===
227
228 {{code value="xml"}}
229
230 <?xml version="1.0" encoding="ISO-8859-1"?>
231 <Server>
232 <ContextManager workDir="work">
233 <LoaderInterceptor11 useApplicationLoader="true"/>
234
235 <AutoDeploy source="modules" target="modules" redeploy="true"/>
236 <AutoWebApp dir="modules" host="DEFAULT" trusted="true"/>
237 <AutoWebApp dir="/home/fooapp/webapps" trusted="true" reloadable="false"/>
238
239 <SimpleMapper1/>
240
241 <SessionExpirer checkInterval="60"/>
242 <SessionIdGenerator randomClass="java.security.SecureRandom"/>
243
244 <WebXmlReader validate="false"/>
245 <ErrorHandler showDebugInfo="true"/>
246
247 <Jdk12Interceptor/>
248 <LoadOnStartupInterceptor/>
249 <Servlet22Interceptor/>
250
251 <SessionId cookiesFirst="false" noCookies="true"/>
252 <SimpleSessionStore maxActiveSessions="256"/>
253
254 <Http10Connector port="8081" secure="false"/>
255 <Ajp13Connector port="9091" tomcatAuthentication="false" shutdownEnable="true"/>
256 </ContextManager>
257 </Server>
258
259 {{/code}}
260
261 Assuming that there will be three instances in this example deploy, this entire file should be replicated and modified twice for the other two instances. For the other instances' server configuration files, change the "i1" (Tomcat 5 only) by modifying the numerical component and change the port numbers by making the last digit the instance number. For example, the ports ##7071##, ##8081## and ##9091## are used here. For "i2", use ##7072##, ##8082## and ##9092##.
262
263 You should have three files now present called ##server//i1.xml//##//, ##server##//##i2.xml## and ##server//i3.xml//##// in the directory ##$INSTDIR##.//
264
265 == Startup An Instance ==
266
267 To startup an instance, issue a command as follows. You should issue this command for each of the server configuration files. The ##$JAVA//HOME//##// shell environment variable should have been setup correctly before launching an instance.//
268
269 |= Tomcat 5 | ##$TOMCATDIR/bin/startup.sh config $INSTDIR/server//i1.xml//##// //
270 |= Tomcat 3 | ##$TOMCATDIR/bin/startup config $INSTDIR/server//i1.xml home $TOMCATDIR//##// //
271
272 === Tomcat 3 Environment Variables ===
273
274 To pass java environment variables to your application, set the ##TOMCAT//OPTS//##// shell environment variable before starting up the Tomcat 3 environment. An example of this would be as follows;//
275
276 {{noformat}}
277
278 TOMCAT_OPTS=-Dabc=xyz
279 export TOMCAT_OPTS
280
281 {{/noformat}}
282
283 == Check Availability ==
284
285 You can now see if your instance is up using the following URL.
286
287 {{noformat}}
288
289 http://appserver1.foo.co.nz:8081/FooApp/WebObjects/FooApp.woa
290
291 {{/noformat}}
292
293 Note that both the AJP (This is the protocol between the apache adaptor and the individual Tomcat instances.) and the regular HTTP engines are accessing the same running application. This means that a test straight onto the application via HTTP is actually testing the application that is being accessed via AJP. This behaviour provides an opportunity to be able to monitor specific instances of Tomcat over direct HTTP.
294
295 Check each of your instances.
296
297 == Problems ==
298
299 Under Tomcat 5, check the log file at ##$TOMCATHOME/logs/catalina.out## if you are unable to access your instance.
300
301 == Shutdown An Instance ==
302
303 To shutdown an instance, issue a command as follows.
304
305 === Tomcat 5 ===
306
307 {{noformat}}
308
309 $TOMCATDIR/bin/shutdown.sh -config $INSTDIR/server_i1.xml
310
311 {{/noformat}}
312
313 It is also possible to use the UNIX ##telnet## command to connect to the port described in the server configuration file's ##Server## tag and type the word supplied in the ##shutdown## attribute to take out the Tomcat instance.
314
315 === Tomcat 3 ===
316
317 {{noformat}}
318
319 TOMCATDIR/bin/shutdown -ajp13 -port 9091
320
321 {{/noformat}}
322
323 = Apache Adaptor Setup =
324
325 A final deployment usually involves apache feeding inbound requests to instances and handling situations where an instance has gone down and balancing load over the instances that are currently running. This job is undertaken by ##mod//jk//##// which is a module for apache written by the Tomcat group. This module communicates with the tomcat instances using a protocol called ##AJP##. This protocol carries all the information required to check instances are operational as well as relaying requests to the instances. It is assumed that the configuration as well as binaries for the ##mod##//##jk## apache module will be located at ##$JKDIR##.
326
327 == Compiling and Installing ==
328
329 Instructions for downloading and installing ##mod//jk//##// can be obtained from the Tomcat website. The ##mod##//##jk.so## binary should be located at the following path.
330
331 {{noformat}}
332
333 $JKDIR/mod_jk.so
334
335 {{/noformat}}
336
337 Add a line to your system's apache httpd configuration file like this:
338
339 {{noformat}}
340
341 Include /opt/modjk/apache.conf
342
343 {{/noformat}}
344
345 Edit this file to look like this:
346
347 {{noformat}}
348
349 LoadModule jk_module /opt/modjk/mod_jk.so
350 AddModule mod_jk.c
351
352 JkLogFile /opt/modjk/mod_jk.log
353 JkLogLevel info
354
355 JkWorkerProperty worker.list=i1,i2,i3,loadbalancer
356 JkWorkerProperty worker.i1.type=ajp13
357 JkWorkerProperty worker.i1.port=9091
358 JkWorkerProperty worker.i1.host=appserver1.foo.co.nz
359 JkWorkerProperty worker.i1.lbfactor=1
360 JkWorkerProperty worker.i2.type=ajp13
361 JkWorkerProperty worker.i2.port=9092
362 JkWorkerProperty worker.i2.host=appserver1.foo.co.nz
363 JkWorkerProperty worker.i2.lbfactor=1
364 JkWorkerProperty worker.i3.type=ajp13
365 JkWorkerProperty worker.i3.port=9093
366 JkWorkerProperty worker.i3.host=appserver1.foo.co.nz
367 JkWorkerProperty worker.i3.lbfactor=1
368 JkWorkerProperty worker.loadbalancer.type=lb
369 JkWorkerProperty worker.loadbalancer.sticky_session=1
370 JkWorkerProperty worker.loadbalancer.local_worker_only=1
371 JkWorkerProperty worker.loadbalancer.balance_workers=i1,i2,i3
372
373 JkMount /FooApp/* loadbalancer
374
375 {{/noformat}}
376
377 Again, the details of the exact settings will not be covered in this article, but this should provide a simple guide to setting up this file to provide for a multi-instance deploy under Tomcat.
378
379 == Restart Apache ==
380
381 Now restart apache with the following command:
382
383 {{noformat}}
384
385 sudo apachectl restart
386
387 {{/noformat}}
388
389 == Test Application ==
390
391 Now you can test your application using a URL such as this one.
392
393 {{noformat}}
394
395 http://www.foo.co.nz/FooApp/WebObjects/FooApp.woa
396
397 {{/noformat}}
398
399 All three instances that you have setup should take some of the inbound requests.
400
401 == Choosing the Right Instance ==
402
403 ##mod//jk//##// ensures that requests that have started a session will be directed to the correct instance where the session originated. This behaviour is known as sticky sessions. It does not appear to be possible to nominate the instance from the servlet container in another way.//