Skip navigation

amyroh

10 posts

With GlassFish 3.1.2 release around the corner, I've decided to update the outdated blog to cover how to front GlassFish 3.1.2 with Apache HTTP Server.  The instructions have changed since v3 and it might lead to some confusion to the users who might be looking at the old blog post. 

GlassFish, Apache HTTP Server, and mod_jk

GlassFish 3.1.2 (as well as 3.1.1) supports AJP protocol for each network listener using the jk-enabled attribute as described below.  The instructions are the same for 3.1.1 and 3.1.2.

1.  Install Apache HTTP Server and mod_jk.

2.  Configure Apache

apache/conf/workers.properties

# Define 1 real worker using ajp13
worker.list=worker1
# Set properties for worker1 (ajp13)
worker.worker1.type=ajp13
worker.worker1.host=localhost
worker.worker1.port=8009

apache/conf/httpd.conf 

LoadModule jk_module /apache/modules/mod_jk-1.2.25-httpd-2.2.4.so
JkWorkersFile /apache/conf/worker.properties
# Where to put jk logs
JkLogFile /apache/logs/mod_jk.log
# Set the jk log level [debug/error/info]
JkLogLevel debug
# Select the log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
# JkOptions indicate to send SSL KEY SIZE,
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
# JkRequestLogFormat set the request format
JkRequestLogFormat "%w %V %T"
# Send all jsp requests to GlassFish
JkMount /*.jsp worker1
# Send all webapp requests to GlassFish
JkMount /webapp/* worker1

3.  Start Apache HTTP Server.

4.  Start GlassFish Server and deploy your webapp.

5.  Create a jk-enabled network listener either using asadmin command or the admin console.

asadmin> create-network-listener --protocol http-listener-1 --listenerport 8009 --jkenabled true jk-connector

6.  The jk-configuration-file attribute of the network listener can be used to configure non-default attributes for the Connector.  Use asadmin set to configure the jk-configuration-file attribute of the  network listener to the fully-qualified filename of the jk-configuration-file.  This step is optional.

asadmin> set server-config.network-config.network-listeners.network-listener.jk-connector.jk-configuration-file=domain-dir/config/glassfish-jk.properties   

7.  Increase the maximum threads in the http-thread-pool pool if necessary.

asadmin> set configs.config.server-config.thread-pools.thread-pool.http-thread-pool.max-thread-pool-size=value

The same GlassFish configuration can be used for mod_proxy_ajp also.  GlassFish 3.1.2 will be using Grizzly for ajp implementation.

Load balance using GlassFish, Apache HTTP Server, and mod_jk

GlassFish, Apache HTTP Server, and mod_jk can be used for load balancing and the following describes how to load balance using two instances.

1.  Install Apache HTTP Server and mod_jk.

2.  Configure Apache

apache/conf/workers.properties

worker.list=loadbalancer
worker.worker1.type=ajp13
worker.worker1.host=localhost
worker.worker1.port=8009
worker.worker1.lbfactor=1
worker.worker1.socket_keepalive=1
worker.worker1.socket_timeout=300
worker.worker2.type=ajp13
worker.worker2.host=localhost
worker.worker2.port=8010
worker.worker2.lbfactor=1
worker.worker2.socket_keepalive=1
worker.worker2.socket_timeout=300
worker.loadbalancer.type=lb
worker.loadbalancer.balance_workers=worker1,worker2

apache/conf/httpd.conf 

LoadModule jk_module /apache/modules/mod_jk-1.2.25-httpd-2.2.4.so
JkWorkersFile /apache/conf/worker.properties
# Where to put jk logs
JkLogFile /apache/logs/mod_jk.log
# Set the jk log level [debug/error/info]
JkLogLevel debug
# Select the log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
# JkOptions indicate to send SSL KEY SIZE,
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
# JkRequestLogFormat set the request format
JkRequestLogFormat "%w %V %T"
# Send all jsp requests to GlassFish
JkMount /*.jsp loadbalancer
# Send all webapp requests to GlassFish
JkMount /webapp/* loadbalancer

3.  Start Apache HTTP Server.

4.  Start GlassFish Server and deploy your webapp.

5.  Create a jk-enabled network listener targeted to the cluster either using asadmin create-network-listener command or the admin console. 

asadmin> create-network-listener --protocol http-listener-1 --target cluster1 --listenerport ${AJP_PORT} --jkenabled true jk-connector

6.  Create the jvmRoute property targeted to the cluster using the create-jvm-options command.

asadmin> create-jvm-options --target cluster1 "-DjvmRoute=\${AJP_INSTANCE_NAME}"  

7.  Define the AJP_PORT and AJP_INSTANCE_NAME properties for each of the instances of the cluster using the create-system-properties command.  The port values need to match the values specified in the  workers.properties.

asadmin> create-system-properties --target instance1 AJP_PORT=8009
asadmin> create-system-properties --target instance1 AJP_INSTANCE_NAME=instance1
asadmin> create-system-properties --target instance2 AJP_PORT=8010
asadmin> create-system-properties --target instance2 \AJP_INSTANCE_NAME=instance2
asadmin> create-system-properties --target instance3 AJP_PORT=8011

That's it!  Send questions to users@glassfish.java.net.

FAQ -

1.  "No class name for worker.worker1 worker" in the server.log. 

Make sure you have the workers.properties file and configured correctly as JkWorkersFile in httpd.conf

2.  "threadpool.max_threads_too_low" in the server.log.

Try increasing maximum threads in the http-thread-pool pool via asadmin> set configs.config.server-config.thread-pools.thread-pool.http-thread-pool.max-thread-pool-size=value or use jk-configuration-file for Connector.maxThreads.

              3.  "WARNING JK properties file ...               does not exist"

This is a warning message that you have jk-configuration-file defined     but the file does not exist.

3.  How should I write the jk-configuration-file?

See Properties.load for key/element examples.

4.  How do I create a jk-enabled connector with a new thread pool with a minimum size of 5 and mazimum size of 150 (Apache MaxClients) for example?

asadmin> create-threadpool --minthreadpoolsize 5 --maxthreadppplsize 150  jk-thread-pool
asadmin> create-network-listener --protocol http-listener-1 --listenerport 8009 --jkenabled true --threadpool jk-thread-pool jk-connector

 

5.  Truncated downloads

See http://java.net/jira/browse/GLASSFISH-18446

GlassFish V3 has improved the way to front GlassFish with Apache HTTP Server. Unlike the V2 way where users had to copy tomcat-ajp.jar and commons-*.jar, you can just enable mod_jk in V3 using the network-listener's attribute "jk-enabled" without copying any additional jars into its lib directory. You can also create jk-connectors under different virtual-servers (not just default virtual-server "server" in V2) using the network-listener's "jk-enabled" attribute.

Here are instructions.

(1) Install Apache HTTP Server and mod_jk

(2) Configure workers.properties and httpd.conf

For example, apache2/config/workers.properties

# Define 1 real worker using ajp13
worker.list=worker1
# Set properties for worker1 (ajp13)
worker.worker1.type=ajp13
worker.worker1.host=localhost
worker.worker1.port=8009

apache2/conf/httpd.conf

LoadModule jk_module /Users/Amy/apache2/modules/mod_jk-1.2.25-httpd-2.2.4.so
JkWorkersFile /Users/Amy/apache2/conf/worker.properties
# Where to put jk logs
JkLogFile /Users/Amy/apache2/logs/mod_jk.log
# Set the jk log level [debug/error/info]
JkLogLevel debug
# Select the log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
# JkOptions indicate to send SSL KEY SIZE,
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
# JkRequestLogFormat set the request format
JkRequestLogFormat "%w %V %T"
# Send everything for context /examples to worker named worker1 (ajp13)
JkMount /examples/* worker1

(3) Start Apache HTTP Server

(4) Enable mod_jk using the following command

asadmin create-http-listener --listenerport 8009 --listeneraddress 0.0.0.0 --defaultvs serverjk-connector

asadmin set configs.config.server-config.network-config.network-listeners.network-listener.jk-connector.jk-enabled=true


(5) If you are using the glassfish-jk.properties file and not referencing it in httpd.conf, point to it using the following command:

asadmin create-jvm-options -Dcom.sun.enterprise.web.connector.enableJK.propertyFile=domain-dir/config/glassfish-jk.properties

(6) Restart GlassFish

The network-listener attribute "jk-enabled" is availablefrom V3 b53 or later.

Deployment specific web application configuration usingcontext.xml in GlassFish was addeda couple of months ago but related questions keep coming up on mailing lists and forumso I've decided to blog about it so it's easy to point out. ;-)

Similar to Tomcatyou can define your context.xml as follows.

1. Global context.xmlglassfish/domains/domain1/config/context.xml which will be loaded by *all* webapps.

For example, global context.xml can define global environment entry to be shared across all webapps as follows.

<Context>
<Environment name="global_environment" type="java.lang.String" value="global_environment_value"/>
</Context>


2. Virtual server context.xml specified by virtual-server property "contextXmlDefault" in domain.xmlwill be loaded by all webapps of this virtual server.

For examples, you can define contextXmlDefault for a virtual server in your domain.xml as follows.

<virtual-server id="server" ... >
<property name="contextXmlDefault" value="config/context.xml.default"/>
</virtual-server>


3. Webapp specific context.xml will be loaded by this webapp only.

You can define /META-INF/context.xml in a WAR file for this webapp specific environment and resource definitions.

<Context>
<Environment name="webapp-env" type="java.lang.String" value="webapp-env-value"/>
<Resource name="jdbc/__default" auth="Container" type="javax.sql.DataSource" description="Default Database"/>
</Context>


GlassFish V2 Beta 2 or later should have this feature.

Cheers,
Amy  
We have heard some comments from the community regarding the need to configure AJP Connector in GlassFish. I have gone ahead and expose the AJP Connector parameters in GlassFish so it's easy to configure the connector using a properties file.

Quick blog on how to configure AJP Connector in GlassFish


(1) First, install mod_jk - see Jeanfrancois's blog for more details


(2) Copy three necessary jars from tomcat and jakarta-commons

 * tomcat-ajp.jar      
 * commons-logging.jar
 * commons-modeler.jar

to $GLASSFISH_HOME/lib/.


(3) Enable mod_jk by adding :

$GLASSFISH_HOME/bin/asadmin create-jvm-options -Dcom.sun.enterprise.web.connector.enableJK=8009


(4) Create a glassfish-jk.properties file


(5) Let GlassFish know where you'd like to load glassfish-jk.properties from by adding :

$GLASSFISH_HOME/bin/asadmin create-jvm-options -Dcom.sun.enterprise.web.connector.enableJK.propertyFile=/home/amyroh/glassfish_home/domains/domain1/config/glassfish-jk.properties


(6) Restart GlassFish.

You can configure your AJP Connector starting GlassFish V2 build 13 (will be promoted on 8/16) or any nightly builds after 8/12/06.  
The Java(TM) Servlet 2.4 and JavaServer(TM) Pages (JSP) 2.0 and 2.1 API jars are now available from the java.net Maven repository. You can find more about the java.net maven repository here. If your project is maven based, instructions are here and check out maven-repository-importerfor ant based projects.  
amyroh

Roller 2.1 on GlassFish Blog

Posted by amyroh Feb 20, 2006

Deploying Roller 2.1 on GlassFish.


The Roller 2.1 switched its security system to the Acegi security framework and moved away from container managed authentication.  This allows deploying the Roller on GlassFish without having to add a custom JDBC Realm.

Please refer to The Roller InstallationGuide for complete installation and configuration steps for the Roller Weblogger Project.

 

STEP 1: Prerequisites

Download and install JDK 5 and MySQL.

STEP 2: Download\Install GlassFish

1. DownloadGlassFish

(
b38 or later for ClassCastException: net.sf.acegisecurity.providers.UsernamePasswordAuthenticationToken issue fix)

2. Run:
java -Xmx256m -jarfilename.jar

3. cd glassfish

4. Run:
ant -f setup.xml

See the GlassFish Quick Start Guide for basic steps to start the server and deploy an application.

STEP 3: Download\Install Roller

Step 3.1 : Download Roller

  1. Download Roller 2.1. 
    (The official Roller 2.1 release should be out verysoon.  For now, you'll have to get it from its subversion repository and build it yourself). 

  2. Unpack the downloaded TAR file.
% cp roller.tgz <temp_dir>/roller
% cd <temp_dir>/roller
% tar xzvf roller.tgz

On Windows: Use winzip to extract roller.zip into <temp_dir>\roller.

Step 3.2 : Minor changes to Roller

Roller web.xml is currently broken since it has a 2.3 DTD, yet it still contains 2.4 elements.  (Discussion is happening on the roller list about this so I think the changes should be in soon) Also, I had to make some changes to jsp files (mainly in login-redirect.jsp and taglibs.jsp) for jstl 1.1 taglib uri upgrade.

You can download working version of roller.war here.

STEP 4: Create Roller tables in your database

Now you need to create a new database, create a user with appropriate privileges, and use an SQL script to create the database tables required to run Roller. To do this, login to your database and run one of the Roller database creation scripts located in Roller's WEB-INF/dbscripts directory.

  • WEB-INF/dbscripts/mysql/creatdb.sql - creates tables for MySQL
  • WEB-INF/dbscripts/derby/createdb.sql - creates tables for Derby
  • WEB-INF/dbscripts/postgresql/creatdb.sql - creates tables for PostgreSQL

The examples below show you how you might do this using MySQL, assuming your Roller user will have username scott and password tiger. For more information on MySQL, refer to the MySQL Reference Manual.

Make sure you enable UTF-8 support inMySQL (see page Setting Up UTF8 on MySQL for details).

UNIX example

 % cd <temp_dir>/roller/WEB-INF/dbscripts/mysql
% mysql -u root -p
password: *****
mysql> create database roller;
mysql> grant all on roller.* to scott@'%' identified by 'tiger';
mysql> grant all on roller.* to scott@localhost identified by 'tiger';
mysql> use roller;
mysql> source createdb.sql
mysql> quit

Windows example, from an MS-DOS or Command Prompt window:

 C> cd <temp_dir>\roller\WEB-INF\dbscripts\mysql
C> mysql -u root -p
password: *****
mysql> create database roller;
mysql> grant all on roller.* to scott@'%' identified by 'tiger';
mysql> grant all on roller.* to scott@'localhost' identified by 'tiger';
mysql> use roller;
mysql> source createdb.sql
mysql> quit

Notes:

MySQL users, don't forget to call "flush privileges" and make sure that your MySQLinstallation hasn't set the skip-networkingoption. Connector/J can only access MySQL via TCP/IP. The MySQL command line tool however doesn't use TCP/IP sockets by default. To check whether your connect works, use anything other than "localhost" as host, for example:

 mysql roller -h 127.0.0.1 -u scott -ptiger

STEP 5: JDBC connection

Step 5.1 : JDBC driver jar

Download MySQL Connector/J and copy the jar into glassfish_install_dir/lib
(ex. glassfish_install_dir/glassfish/lib/mysql-connector-java-3.1.12-bin.jar).

You can also add the jar to the classpath via Admin GUI
(Admin GUI -> Application Server -> JVM settings -> Path Settings -> Classpath Suffix) or domain.xml edit (<java-config classpath-prefix="...." classpath-suffix="" ....> )

Step 5.2 : JDBC Connection Pool Setting

Create JDBC Connection Pool (Admin GUI -> Application Server -> Resources -> JDBC -> Connection Pools)

jdbc_connection_pool.JPG

<jdbc-connection-pool allow-non-component-callers="false"
datasource-classname="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource">
<property name="user" value="scott"/>
<property name="port" value="3306"/>
<property name="password" value="tiger"/>
<property name="databaseName" value="roller"/>
<property name="serverName" value="localhost"/>
</jdbc-connection-pool>

Step 5.3 : JDBC Resource Setting


Create JDBC Resource (Admin GUI -> Application Server -> Resources -> JDBC -> JDBC Resources)
<jdbc-resource enabled="true"
jndi-name="jdbc/rollerdb" object-type="user" pool-name="MySQL"/>

STEP 6: Deploy Roller to GlassFish

Step 6.1: sun-web.xml 

Create sun-web.xml file to <temp_dir>/roller/WEB-INF directory.

sun-web.xml
 
<?xml version="1.0" encoding="UTF-8"?>

<!--
Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved.
Use is subject to license terms.
-->

<!DOCTYPE sun-web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server 8.1 Servlet 2.4//EN" "http://www.sun.com/software/appserver/dtds/sun-web-app_2_4-1.dtd">

<sun-web-app>

<session-config>
<session-manager>
</session-manager>
</session-config>

<resource-ref>
<res-ref-name>jdbc/rollerdb</res-ref-name>
<jndi-name>jdbc/rollerdb</jndi-name>
</resource-ref>

<class-loader delegate="false"/>

</sun-web-app>
<class-loader delegate="false"/> is to address "ClassNotFoundException: org.hibernate.hql.ast.HqlToken" reported.  See Jan's blog for more details.

Step 6.2: Create war file

% cd <temp_dir>/roller% jar cvf ../roller.war *

Step 6.3: Deploy roller.war to GlassFish

Deploy <temp_dir>/roller.war to GlassFish
(Admin GUI -> Application Server -> Applications -> Web Applications -> Deploy)

Step 6.4: Security permission

Add security permission in glassfish_install_dir\glassfish\domains\domain1\server1\config\server.policy.

// permission for Roller: file upload, log setting, etc :
grant codeBase "file:${com.sun.aas.installRoot}/domains/domain1/applications/j2ee-modules/roller/-" {
permission java.security.AllPermission; };
grant codeBase "file:${com.sun.aas.installRoot}/domains/domain1/generated/jsp/j2ee-modules/roller/-" {
permission java.security.AllPermission; };

Although the above security permission works, more specific and restrictive settings might be appropriate.

Now you should be able to check out your Roller app by accessing http://localhost:8080/roller and register your first admin user.  Check out the Roller UserGuidefor more info.http://blogs.sun.com/roller/resources/akihito/roller_on_sun_java_system_application_server_7.html

Thanks to Allen Gilliland from the Roller team for helping me with Roller questions.

References:  Roller Installation Guide and Roller Installation Guide on SJSAS7SE
Ask the Experts Transcript

Read the transcript of the Nov. 14-18 Ask the Experts session on Project GlassFish.  
Last week I gave a presentation on Project GlassFish for the Los Angeles Java Users Group. It was my first time with Los Angeles Java Users Group. Got to meet some cool Java folks.

Folks have been asking for the presentation slides so I put it up on the GlassFish "Documents and Settings" section. You can download it here.  
amyroh

Got Fish? Blog

Posted by amyroh Jul 27, 2005
FishEye is up and running for GlassFish.

FishEye delivers a unified view of your source repository that provides easy navigation, powerful search, historical reporting, configurable file annotation and diff views, change-set analysis, RSS feeds, and integration with your issue tracker.

GlassFish just started to support FishEyefor its CVS repository and folks are finding this very useful.

You can easily traverse directory hierarchies, see the latest changes, search based on combinations of author, date, tag, branch, file, and comment, view diffs between arbitrary revisions with configurable context and whitespace exclusion, easily navigate between diff blocks and extended diff view shows actual changes within a line. Moreover, RSS Feeds keep you in touch with the action from any point in the repository.

You can checkout FishEye running on GlassFish here

One observation from a GlassFish developer. "Go to http://fisheye.cenqua.com/java.net/viewrep/cvs, click on the "Feeds" link in the upper right corner and you'll get the RSS feeds for the checkins made in recent past! This, combined with the good browser like Firefox makes me subscribe to "live bookmarks" makes me not look for the e-mails for checkin logs."

Pretty cool tool with nice coincidence in the names between GlassFish andFishEye.

Which build tools do you currently use in your projects and find it useful?  
It has been pretty exciting after Sun's Open Source Application Server project GlassFish announcement on Monday morning at JavaOne. James Gosling wore our cute GlassFish t-shirt on stage during the General Session on Tuesday. I was glad to see that many developers came to our booth and shared excitement of the news and interests. We had a well attended GlassFish BOF on Monday night to introduce the project on how to get involved and also to get some inputs from the community.

If you were at the BOF, you might remember the question regarding whether GlassFish has a plan to support the JK connector. It was answered that GlassFish has great performance (and benchmark to prove it) and that there will not be any need to plug in any other connector. One audience member stated that you might want the JK connector for other purposes than performance.

I vaguely remembered the feature being requested through java forums and implementing it. I wasn't too sure during the BOF if the feature did in fact make it. It's been a while and I didn't want to prematurely give out wrong information. Enabling the JK connector in the Application Server wasn't a popular feature request. The webtiermodule of the Application Server builds on top of Tomcat and Tomcat supports the JK Connector. Since GlassFish doesn't ship with the JK connector, you'll need to download tomcat-jk.jar from Tomcat distribution and specify port number using sysproperty in processLauncher.xml to define the port number. The feature hasn't been extensively tested or documented yet.

I searched back for my cvs check in for the feature and found this. It was a little over a year ago and I should have remembered it but my memory seemed to have failed me on this. So check it out and let us know if you have questions on how to enable it or anything else you might want to know, dev@glassfish.java.net is listening.

How many of you plug in a web server with your application server and what are some of the reasons?