Top Banner
[Type text] Hands-on Lab Session 19120 Develop, Deploy, and Monitor a Java EE 6 Application with Clustered GlassFish 3.1 Arun Gupta Java EE & GlassFish Guy blogs.oracle.com/arungupta, @arungupta Byron Nevins GlassFish Monitoring Lead blogs.oracle.com/foo
31

19120 Lab Instructions Java

Oct 03, 2014

Download

Documents

Edgar Campos
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 19120 Lab Instructions Java

[Type text]

Hands-on Lab Session 19120 Develop, Deploy, and Monitor a Java EE 6 Application with Clustered GlassFish 3.1 Arun Gupta Java EE & GlassFish Guy blogs.oracle.com/arungupta, @arungupta

Byron Nevins GlassFish Monitoring Lead blogs.oracle.com/foo

Page 2: 19120 Lab Instructions Java

2

Contents 1.0 Introduction ............................................................................................................................................................. 4

2.0 Software Requirements ........................................................................................................................................... 4

3.0 Download, Walk-through, and Build the Java EE 6 Application ........................................................................... 5

3.1 Get the Application ............................................................................................................................................. 5

3.2 Read the Application........................................................................................................................................... 6

3.3 Build the Application .......................................................................................................................................... 7

4.0 Create a 2-instance Cluster and Deploy the Application ........................................................................................ 8

4.1 Start the GlassFish Domain Administration Server ............................................................................................ 8

4.2 Start the Database Server .................................................................................................................................. 10

4.3 Create the Cluster using Admin Console .......................................................................................................... 10

4.4 Start the Cluster................................................................................................................................................. 11

4.5 Enable the JDBC Resource for the Cluster ....................................................................................................... 12

4.6 Deploy the Application to the Cluster .............................................................................................................. 12

4.7 Verify the Application Deployment .................................................................................................................. 13

5.0 Install and Configure the Load Balancer .............................................................................................................. 14

5.1 Install the Oracle iPlanet Web Server ............................................................................................................... 14

5.2 Install the GlassFish Load Balancer Configurator ............................................................................................ 16

5.3 Create, Export, Upload a Load Balancer Configuration ................................................................................... 19

5.4 Access the Application and Show Session Failover ......................................................................................... 21

6.0 Monitor the Application ........................................................................................................................................ 22

6.1 Customized Application Monitoring ................................................................................................................ 23

6.1.1 Use the JavaScript Listener ........................................................................................................................ 25

6.2 Automatic Monitoring Data .............................................................................................................................. 27

7.0 Troubleshooting Tips ............................................................................................................................................ 27

7.1 How to restart Oracle Web Server Administration Server ?............................................................................. 28

7.2 How to change Oracle Web Server password ? ................................................................................................ 28

7.3 How to restart GlassFish ? ................................................................................................................................ 28

7.4 Getting an error “failed to join group c1” ? ...................................................................................................... 28

7.5 Deployment gives “Command succeeded with Warning”................................................................................ 30

7.6 Session data not preserved ................................................................................................................................ 30

7.7 No monitoring data from the script ................................................................................................................... 30

8.0 Acknowledgements ............................................................................................................................................... 31

Page 3: 19120 Lab Instructions Java

3

9.0 References ............................................................................................................................................................. 31

Page 4: 19120 Lab Instructions Java

4

1.0 Introduction GlassFish Server Open Source Edition 3.1.1 is a fully open sourced, community-supported, Java EE 6 compliant application server. It offers a modular runtime based on OSGi and full-featured clustering with centralized administration of multiple clusters and high availability of stateful components. GlassFish also offers advanced features such as application versioning, application-scoped resources, non-intrusive RESTful management and monitoring. It also provides integration with NetBeans, Eclipse, and other popular IDEs. Oracle GlassFish Server is the commercially supported version of the open source edition and is ideally suited for applications requiring lightweight infrastructure with most up-to-date implementations of enterprise Java. This hands-on lab will:

Walk you through and deploy a typical Java EE 6 application using NetBeans and GlassFish.

Create a two-instance GlassFish cluster and front end with a Web server and a load balancer.

Demonstrate session replication when one of the instances fails.

Use the extensible monitoring infrastructure to generate application-specific monitoring data.

This lab will get you ready to put a Java EE 6 application into production. The latest version of this document is available at http://blogs.oracle.com/arungupta/resource/javaone2011/19120-lab-instructions.pdf. Please talk to us using GlassFish Forums (link in the References section).

2.0 Software Requirements This lab requires the following software:

1. JDK 1.7+ can be downloaded from http://www.oracle.com/technetwork/java/javase/downloads/java-se-jdk-7-download-432154.html.

Page 5: 19120 Lab Instructions Java

5

2. NetBeans 7.0.1+ (“All” or “JavaEE” bundle) can be downloaded from http://netbeans.org/downloads/index.html.

3. Oracle GlassFish Server 3.1.1 (zip bundle) can be downloaded from http://www.oracle.com/technetwork/java/javaee/downloads/ogs-3-1-1-downloads-439803.html.

4. Oracle Web Server 7.0.9+ can be downloaded from https://updates.oracle.com/download/12676928.html.

5. GlassFish Load Balancer Configurator 3.1.1 can be downloaded from http://www.oracle.com/technetwork/middleware/glassfish/downloads/glassfish-lbconfig-3-1-1-downloads-439811.html.

If you are doing this lab as part of the JavaOne 2011 Hands-on Lab session then:

All the software is already downloaded in the C:\Users\Lab\Downloads

directory.

The software 1 and 2 are pre-installed at their default locations, 3 is installed in the

C:\javaone2011 directory, and 4 – 5 will be installed as part of the lab exercise.

Oracle GlassFish Server is pre-configured in the NetBeans IDE.

3.0 Download, Walk-through, and Build the Java EE 6 Application This section will explain how to download the Java EE 6 Application, explain the different parts, and then build it. This section should take about 20 mins.

If you are interested in building a complete Java EE 6 application using NetBeans and GlassFish, then consider attending the hands-on lab #23421 at JavaOne 2011 with the title “Beginning Java EE 6”.

3.1 Get the Application Download the application from http://blogs.oracle.com/arungupta/resource/javaone2011/19120-app.zip and unzip.

Page 6: 19120 Lab Instructions Java

6

3.2 Read the Application In the NetBeans IDE, choose “File”, “Open Project…” (short cut key

= Ctrl+Shift+O), select the unzipped directory, and click on

“Open Project”. NetBeans may show red lines under some of the annotation in different classes. This can happen if Maven does not have all

the dependencies in the local repository. This will however get resolved once the project is built. The expanded project structure looks like as shown. This is a typical 3-tier application that shows the list of movies stored in a database and allows the user to select a preferred list. The three tiers use JSF for the UI, EJBs for the middle tier, and JPA/JavaDB for the database tier. The basic architecture of the application is shown below: The movie database is created using a singleton EJB

(DatabaseSingletonBean.java) and a JPA entity (Movie.java). A stateless EJB

(MovieSessionBean.java) is used to query the database and return the results that

are displayed in a JSF page (index.xhtml). This page also allows the user to specify

movie preferences and save the state using a stateful EJB

(MoviePreferencesBean.java).

The application also consists of the following supporting files:

index.xhtml Database

@Stateless

@Stateful

@Singleton

@Entity

Page 7: 19120 Lab Instructions Java

7

ClusterInstanceBean.java: Provides information about the instance on which

the application is deployed.

TestProbeLister.java: Shows all Monitoring Probes that are specific to this

application.

Web Pages/WEB-INF/beans.xml: Enable CDI injection for the beans in the WAR

file

WEB-INF/template.xhtml: Template file for the JSF pages and used in the

index.xhtml.

WEB-INF/web.xml: Contains <distributable/> element to ensure that the app

can run in a distributed environment and the sessions can be replicated in a clustered environment.

Other Sources/…/persistence.xml: JPA Persistence Unit that points to the

default JDBC resource in GlassFish, named jdbc/__default, and uses it for all

database communication.

Web Pages/resources/css/*: Stylesheets used by the template page.

19120-app.js (not shown in the diagram): JavaScript to capture monitoring

events.

The default JDBC resource is defined as a global resource and shared across all different applications. In addition, GlassFish allows to create an application-scoped

resource by bundling the resource definition in WEB-INF/glassfish-

resources.xml. These resources are then created / deleted with application

deployment and are not available for other applications. This hands-on lab does not

use glassfish-resources.xml though.

3.3 Build the Application In the NetBeans IDE, right-click on the project and select “Build” to see an output as: Packaging webapp

Assembling webapp [19120-app] in [D:\code\workspaces\arun\19120-app\target\19120-app]

Processing war project

Copying webapp resources [D:\code\workspaces\arun\19120-app\src\main\webapp]

Webapp assembled in [108 msecs]

Building war: D:\code\workspaces\arun\19120-app\target\19120-app.war

Warning: selected war files include a WEB-INF/web.xml which will be ignored

(webxml attribute is missing from war task, or ignoreWebxml attribute is specified as

'true')

[install:install]

Page 8: 19120 Lab Instructions Java

8

Installing D:\code\workspaces\arun\19120-app\target\19120-app.war to C:\Users\arungup.ST-

USERS\.m2\repository\org\glassfish\samples\javaone2011\19120-app\1.0-SNAPSHOT\19120-app-1.0-

SNAPSHOT.war

Installing D:\code\workspaces\arun\19120-app\pom.xml to C:\Users\arungup.ST-

USERS\.m2\repository\org\glassfish\samples\javaone2011\19120-app\1.0-SNAPSHOT\19120-app-1.0-

SNAPSHOT.pom

------------------------------------------------------------------------

BUILD SUCCESS

------------------------------------------------------------------------

Total time: 4.449s

Finished at: Tue Aug 23 15:52:53 PDT 2011

Final Memory: 14M/220M

------------------------------------------------------------------------

A similar output ensures that your Web application is successfully built. This generates a

19120-app.war file in the target directory of the application.

The first build of this application may take a few minutes as the Maven will download all the dependencies.

4.0 Create a 2-instance Cluster and Deploy the Application

This section will create a 2-instance GlassFish cluster on the local machine and deploy the previously built application to it. This section should take about 30 mins.

GlassFish has the concept of an administrative domain managed by a Domain Administrative Server (DAS). Multiple Server Instances are managed by a DAS to form a homogeneous cluster.

4.1 Start the GlassFish Domain Administration Server In a Command Prompt, give the following command (highlighted in bold): C:\javaone2011\glassfish3>.\bin\asadmin start-domain

Waiting for domain1 to start ......

Successfully started the domain : domain1

domain Location: C:\javaone2011\glassfish3\glassfish\domains\domain1

Log File: C:\javaone2011\glassfish3\glassfish\domains\domain1\logs\server.log

Admin Port: 4848

Page 9: 19120 Lab Instructions Java

9

Command start-domain executed successfully. This starts the DAS and shows the following output in the log file at C:\javaone2011\glassfish3\glassfish\domains\domain1\logs\server.lo

g:

[#|2011-09-12T12:19:48.128-0700|INFO|oracle-

glassfish3.1.1|javax.enterprise.system.core.com.sun.enterprise.v3.services.impl|_ThreadID=34

;_ThreadName=Thread-2;|Grizzly Framework 1.9.36 started in: 63ms - bound to

[0.0.0.0:4848]|#]

[#|2011-09-12T12:19:48.128-0700|INFO|oracle-

glassfish3.1.1|javax.enterprise.system.core.com.sun.enterprise.v3.services.impl|_ThreadID=30

;_ThreadName=Thread-2;|Grizzly Framework 1.9.36 started in: 47ms - bound to

[0.0.0.0:3700]|#]

[#|2011-09-12T12:19:48.190-0700|INFO|oracle-

glassfish3.1.1|javax.enterprise.system.core.com.sun.enterprise.v3.server|_ThreadID=1;_Thread

Name=Thread-2;|Oracle GlassFish Server 3.1.1 (12) startup time : Felix (4,883ms), startup

services(655ms), total(5,538ms)|#]

[#|2011-09-12T12:19:48.611-0700|INFO|oracle-

glassfish3.1.1|javax.enterprise.system.tools.admin.org.glassfish.server|_ThreadID=41;_Thread

Name=Thread-2;|JMXStartupService: Started JMXConnector, JMXService URL =

service:jmx:rmi://ARUNGUP-LAP.st-users.us.oracle.com:8686/jndi/rmi://LAB-

OAA9LDAV45C.us.oracle.com:8686/jmxrmi|#]

[#|2011-09-12T12:19:48.752-0700|INFO|oracle-

glassfish3.1.1|org.hibernate.validator.util.Version|_ThreadID=1;_ThreadName=Thread-

2;|Hibernate Validator 4.1.0.Final|#]

[#|2011-09-12T12:19:48.767-0700|INFO|oracle-

glassfish3.1.1|org.hibernate.validator.engine.resolver.DefaultTraversableResolver|_ThreadID=

1;_ThreadName=Thread-2;|Instantiated an instance of

org.hibernate.validator.engine.resolver.JPATraversableResolver.|#]

You may optionally start the command with the –v switch to display the log on the

console. This can be done by giving the asadmin stop-domain command to stop

the DAS and restarting by giving the command as above with the additional –v switch.

Accessing http://localhost:8080 in a browser window shows the output as shown. This ensures that the Oracle GlassFish Server DAS has started successfully.

Page 10: 19120 Lab Instructions Java

10

The GlassFish provides a web-based Administration Console, aka admin console, and is available at http://localhost:4848 after a successful start of the DAS. The admin console provides a graphical UI for all the administration tasks and is used for creating a cluster, adding instances to it, and deploying applications for our hands-on lab.

4.2 Start the Database Server In a Command Prompt, give the following command (highlighted in bold) to start the database server: C:\javaone2011\glassfish3>.\bin\asadmin start-database

Starting database in Network Server mode on host 0.0.0.0 and port 1527.

--------- Derby Network Server Information --------

Version: CSS10060/10.6.2.1 - (999685) Build: 999685 DRDA Product Id: CSS10060

-- listing properties --

derby.drda.traceDirectory=C:\javaone2011\glassfish3\glassfish\d...

. . .

Found support for locale: [zh_TW]

version: 10.6.2.1 - (999685)

------------------------------------------------------

Starting database in the background.

Log redirected to C:\javaone2011\glassfish3\glassfish\databases\derby.log.

Command start-database executed successfully.

4.3 Create the Cluster using Admin Console 4.3.1 Go to the admin console, click on “Clusters” in the right navigation tree, and then click the “New…” button. 4.3.2 Enter the cluster name as “c1”, click the “New…” button, specify the instance name as “i1”, click the “New…” button, and specify the instance name as “i2”.

Page 11: 19120 Lab Instructions Java

11

The values entered in the console look like as shown and click the “OK” button. This will create a cluster “c1” with two instances “i1” and “i2” on the local machine and will show the output as:

This indicates that the cluster is created and both the instances in the cluster are not running.

4.4 Start the Cluster Start the cluster by selecting the check box next to “c1” and clicking the “Start Cluster” button. The admin console prompts and click on the “OK” button to confirm. The Command Prompt shows the following output towards the end: [#|2011-09-12T12:31:41.533-0700|INFO|oracle-

glassfish3.1.1|ShoalLogger|_ThreadID=113;_ThreadName=GMS ViewWindowThread Group-c1;|GMS1025:

Adding Joined And Ready member: i1 group: c1 StartupState: GROUP_STARTUP |#]

[#|2011-09-12T12:31:41.533-0700|INFO|oracle-

glassfish3.1.1|ShoalLogger|_ThreadID=113;_ThreadName=GMS ViewWindowThread Group-c1;|GMS1092:

GMS View Change Received for group: c1 : Members in view for JOINED_AND_READY_EVENT(before

change analysis) are :

1: MemberId: i1, MemberType: CORE, Address: 144.25.210.248:9167:228.9.69.20:11632:c1:i1

2: MemberId: i2, MemberType: CORE, Address: 144.25.210.248:9156:228.9.69.20:11632:c1:i2

3: MemberId: server, MemberType: SPECTATOR, Address:

144.25.210.248:9132:228.9.69.20:11632:c1:server

|#]

[#|2011-09-12T12:31:41.533-0700|INFO|oracle-

glassfish3.1.1|ShoalLogger|_ThreadID=113;_ThreadName=GMS ViewWindowThread Group-c1;|GMS1016:

Page 12: 19120 Lab Instructions Java

12

Analyzing new membership snapshot received as part of event: JOINED_AND_READY_EVENT for

member: i2 of group: c1|#]

[#|2011-09-12T12:31:41.533-0700|INFO|oracle-

glassfish3.1.1|ShoalLogger|_ThreadID=113;_ThreadName=GMS ViewWindowThread Group-c1;|GMS1025:

Adding Joined And Ready member: i2 group: c1 StartupState: GROUP_STARTUP |#]

A similar output ensures that the cluster is successfully started.

Please see troubleshooting section if the instances are not able to join the cluster.

4.5 Enable the JDBC Resource for the Cluster The application uses “jdbc/__default” as the JDBC resource. This resource is not available to clusters by default and need to be configured by giving the following command using CLI (highlighted in bold): C:\javaone2011\glassfish3>.\bin\asadmin create-resource-ref --enabled=true --target c1

jdbc/__default

resource-ref jdbc/__default created successfully.

i2:

resource-ref jdbc/__default created successfully.

i1:

resource-ref jdbc/__default created successfully.

Command create-resource-ref executed successfully.

The value specified after the --target is the cluster name previously chosen.

4.6 Deploy the Application to the Cluster 4.6.1 In admin console, click on the “Applications” node, click on “Deploy…”, click on “Browse…”, and point to the generated “19120-app.war” file in

the unzipped application directory’s target

directory. 4.6.2 Make sure to select “Enabled” checkbox next to Availability. This check box ensures that the session replication is enabled for HTTP and Stateful Session Beans. The updated page looks

Page 13: 19120 Lab Instructions Java

13

like as shown. Select “c1” as the target for deployment in “Available Targets” as shown below and click on "Add>". Click on “OK” (at the top or bottom of the page).

4.7 Verify the Application Deployment

Click on “Clusters”, “c1” in the navigation tree, “Instances” tab, “i1” and it displays three HTTP ports. The first port is for the administration of this particular instance, the second is the HTTP port for accessing the application, and the third is the HTTPS port.

The deployed application can be accessed using http://<HOST>:<PORT>/19120-app/faces/index.xhtml where <HOST> is “localhost” and <PORT> is 28081 for the instance “i1” in our case. So our application on “i1” will be accessible at http://localhost:28081/19120-app/faces/index.xhtml. Similar our application on “i2” will be accessible at http://localhost:28080/19120-app/faces/index.xhtml.

Accessing this application on “i1” shows the output as: Notice the instance serving the application is displayed in the title bar and the footer. A similar output is shown when this application is accessed on the instance “i2” using the following URL http://localhost:28080/19120-app/faces/index.xhtml. The only difference is that the title bar and the footer shows the correct instance name as shown.

Page 14: 19120 Lab Instructions Java

14

5.0 Install and Configure the Load Balancer

This section will install Oracle Web Server as the Load Balancer and install/configure the GlassFish Load Balancer Plugin for it. This section should take about 40 mins.

5.1 Install the Oracle iPlanet Web Server

5.1.1 Unzip the file Oracle-iPlanet-Web-Server-

7.0.12-windows.zip from the

downloads directory in the

C:\javaone2011 directory.

Note that a top-level directory name needs to be explicitly specified for unzipping. Double-click on the “setup.exe” in the unzipped Oracle Web Server installation directory and click on “Yes” in “User Account Control” dialog box to begin the installation. The dialog box as shown is displayed. Click the “Next>” button. 5.1.2 Take the default installation directory and click on “Next >”. Click on “Yes” on “Create New Directory” dialog box.

5.1.3 Choose the “Express” install and click on “Next>”.

The Web server administrator password must contain at least 8 characters as this is

Page 15: 19120 Lab Instructions Java

15

a requirement for the Load Balancer Configurator which will be installed later. 5.1.4 Take the default user name, enter the password as “glassFish” and click the “Next>” button.

5.1.5 Confirm all the values and click on “Install Now>”. This will install the Oracle iPlanet Web Server and also start the Web Server Administration Server. On the next screen, select “Skip Registration” (only to save time otherwise preferred), click on “Next>”, and then click the “Finish” button.

The Oracle Web Server, like any other, uses a self-signed certificate for the administration server. Trying to access the administration server at http://localhost:8989 shows a warning message in the Firefox browser as shown.

The warning message may look different in other browsers. In this case, click on “I Understand the Risks”, click on “Add Exception…”, take the defaults, and click on “Confirm Security Exception”. This shows the login page for the Oracle iPlanet Web Server Admin Console. Enter the “User Name” as “admin” and “Password” as “glassFish” as shown, and click on “Login”.

Page 16: 19120 Lab Instructions Java

16

The following page in the browser ensures that the Web Server is successfully installed:

5.2 Install the GlassFish Load Balancer Configurator

The Load Balancer plugin must be installed using the GUI installer for the first time. Subsequent installations can utilized a generated installer script for headless (remote) installations.

The GUI installer need to run with the Administrator privileges as it creates a directory in the Oracle Web Server instance directory. Start a Command Prompt with the Administrator privileges. This can be easily done by going to the Windows “All Programs”, “Accessories”, right-clicking on “Command Prompt” and selecting “Run as administrator”.

Page 17: 19120 Lab Instructions Java

17

5.2.1 Run the GUI installer by giving the following command in a Command Prompt with Administrator privileges: java -jar c:\Users\Lab\Downloads\glassfish-lbconfigurator-

3_1_1.jar

The actual directory name may be different in your environment. 5.2.2 In the Load Balancer Configurator screen, click the “Next” button. 5.2.3 Select the Oracle iPlanet Web Server and click the “Next” button. 5.2.4 In the “LB Plugin Configuration” screen, specify the following values:

Webserver Instance Dir: C:\Program Files\Oracle\WebServer7\<INSTANCE-DIR>

Admin Username: admin

Admin Password: glassFish

Admin Host: localhost

Admin Port: 8989

The <INSTANCE-DIR> is typically named as https-

<MACHINE-NAME> and is called https-ARUNGUP-LAP.st-users.us.oracle.com

on my local machine and as https-LAB-OAA9LDAV45C.us.oracle.com on a lab test

machine. 5.2.5 Click on “Next” and “Next” to proceed with unzip of the package and see the output as shown. 5.2.6 Click on “Next” to proceed with the installation and see the output as shown.

Page 18: 19120 Lab Instructions Java

18

The detailed warning message can be seen by scrolling the window to the right.

The first WARNING message indicate that the C:\Program

Files\Oracle\WebServer7\glassfish-lbplugin\lib directory is added to the

PATH and the machine needs to be re-booted for the settings to take effect. If the Windows PATH environment variable has parameterized values then this directory is not added by the installer. This can be easily fixed by manually adding

C:\Program Files\Oracle\WebServer7\glassfish-lbplugin\lib to the

system environment variable in the Control Panel. The second WARNING message can be ignored as it tries to start the Administration Server which is already running in our case. 5.2.7 Click the “Next” button, review the post-installation procedures (we’ll perform these steps later so can skip as well), click the “Next” button. Click the “Done” button to complete the installation.

You can click the “Generate an automatic installation script” button to generate a script for future installations. This lab does not use or cover it though.

Please make sure to restart GlassFish DAS and Cluster and the Database server after restarting the machine. This can be easily done from the Command Prompt (in

the c:\javaone2011\glassfish3\glassfish3\bin directory) by giving the

following commands: asadmin start-domain

asadmin start-cluster c1

asadmin start-database

The Windows environment may prompt for a firewall warning to run these commands. Accept the warning to proceed with the command execution. And then you'll have to start the Oracle iPlanet Web Server Administration Server from an Administrator-privileged Command Prompt as: C:\Program Files\Oracle\WebServer7\admin-server\bin>startserv

The Oracle iPlanet Web Server 7.0 Administration Server service is starting..

The Oracle iPlanet Web Server 7.0 Administration Server service was started successfully.

Page 19: 19120 Lab Instructions Java

19

5.3 Create, Export, Upload a Load Balancer Configuration

5.3.1 Create a load balancer configuration named c1_lb within the DAS (Domain

Administration Server) for the cluster c1 by giving the command (highlighted in bold):

c:\javaone2011\glassfish3 >.\bin\asadmin create-http-lb --devicehost localhost --deviceport

8989 --target c1 c1_lb

Command create-http-lb executed successfully.

5.3.2 Export the DAS configuration to the file system by giving the command (highlighted in bold): c:\ javaone2011\glassfish3 >.\bin\asadmin export-http-lb-config --lbname c1_lb

loadbalancer.xml

Generated file location: [c:\ javaone2011\glassfish3\glassfish\domains\domain1\load-

balancer\loadbalancer.xml]

Command export-http-lb-config executed successfully.

The generated “loadbalancer.xml” in the glassfish\domains\domain1\load-

balancer directory file looks like:

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

<!DOCTYPE loadbalancer PUBLIC "-//Sun Microsystems Inc.//DTD Sun Java System Application

Server 9.1//EN" "glassfish-loadbalancer_1_3.dtd">

<loadbalancer>

<cluster name="c1" policy="round-robin">

<instance disable-timeout-in-minutes="30" enabled="true" listeners="http://ARUNGUP-

LAP.st-users.us.oracle.com:28080 https://ARUNGUP-LAP.st-users.us.oracle.com:28181" name="i2"

weight="100"/>

<instance disable-timeout-in-minutes="30" enabled="true" listeners="http://ARUNGUP-

LAP.st-users.us.oracle.com:28081 https://ARUNGUP-LAP.st-users.us.oracle.com:28182" name="i1"

weight="100"/>

<web-module context-root="/19120-app" disable-timeout-in-minutes="30" enabled="true"/>

<health-checker interval-in-seconds="10" timeout-in-seconds="30" url="/"/>

</cluster>

<property name="response-timeout-in-seconds" value="60"/>

<property name="reload-poll-interval-in-seconds" value="60"/>

<property name="https-routing" value="false"/>

<property name="preferred-failover-instance" value="true"/>

<property name="require-monitor-data" value="false"/>

<property name="rewrite-location" value="true"/>

<property name="number-healthcheck-retries" value="3"/>

<property name="active-healthcheck-enabled" value="false"/>

<property name="rewrite-cookies" value="false"/>

</loadbalancer>

Page 20: 19120 Lab Instructions Java

20

<!--

This file was generated on: [Wed Aug 24 14:41:00 PDT 2011].

-->

5.3.3 Copy the loadbalancer.xml file to the config directory of the http server instance.

Normally this would be done using ftp or sftp to a remote host, but since this demo runs

entirely on a single host, we’ll just copy the loadbalancer.xml file using the file system

command as: c:\javaone2011\glassfish3>copy glassfish\domains\domain1\load-balancer\loadbalancer.xml

"c:\Program Files\Oracle\WebServer7\https-ARUNGUP-LAP.st-users.us.oracle.com\config"

1 file(s) copied.

This command needs to be executed from an Administrator privileged Command

Prompt. The actual directory starting with https- may be different.

The GlassFish Load Balancer Configurator provides an ease-of-use feature which

allows loadbalancer.xml to be pushed over the wire to the Web server. This

requires to set up a SSL listener on Web server. This is not covered in the lab however more details can be found at http://download.oracle.com/docs/cd/E18930_01/html/821-2426/geobp.html.

5.3.4 Start the Oracle iPlanet Web Server local instance from an Administrator-privileged Command Prompt using the following command (highlighted in bold): C:\Program Files\Oracle\WebServer7\https-ARUNGUP-LAP.st-sers.us.oracle.com\bin>startserv.bat

The Oracle iPlanet Web Server 7.0 (https-ARUNGUP-LAP.st-users.us.oracle.com) service is

starting...

The Oracle iPlanet Web Server 7.0 (https-ARUNGUP-LAP.st-users.us.oracle.com) service was

started successfully.

Check the logs in the file C:\Program Files\Oracle\WebServer7\https-

ARUNGUP-LAP.st-users.us.oracle.com\logs\errors (with the directory name

starting with https- appropriately changed) and look for the output highlighted in bold:

C:\Program Files\Oracle\WebServer7\https-ARUNGUP-LAP.st-users.us.oracle.com\logs>more errors

[24/Aug/2011:14:50:28] info ( 8304): CORE1116: Oracle iPlanet Web Server 7.0.12 B07/04/2011

06:14

[24/Aug/2011:14:50:28] info ( 8304): reports: Initializing lbplugin BuildId: GlassFish 3.1

load-balancer plugin b05

[24/Aug/2011:14:50:28] info ( 8304): CORE5076: Using [Java HotSpot(TM) Server VM, Version

1.6.0_24] from [Sun Microsystems Inc.]

Page 21: 19120 Lab Instructions Java

21

[24/Aug/2011:14:50:28] config ( 8304): , name-trans-passthrough reports: init-passthrough

has not been called

[24/Aug/2011:14:50:30] config ( 8304): , name-trans-passthrough reports: init-passthrough

has not been called

[24/Aug/2011:14:50:30] config ( 8304): , name-trans-passthrough reports: init-passthrough

has not been called

[24/Aug/2011:14:50:30] warning ( 8304): reports: lb.configurator: XML_VALIDATOR_WARNING:

Cookies will not be rewritten by web server. All cookie updates will be handled by

application server. If you are using older version of application server, then failover will

not work.

[24/Aug/2011:14:50:30] warning ( 8304): reports: lb.configurator: Preferred failover

instance feature is enabled.

[24/Aug/2011:14:50:30] warning ( 8304): reports: lb.runtime: RNTM2019: Daemon

http://ARUNGUP-LAP.st-users.us.oracle.com:28080 has been intialized.

[24/Aug/2011:14:50:30] warning ( 8304): reports: lb.runtime: RNTM2019: Daemon

https://ARUNGUP-LAP.st-users.us.oracle.com:28181 has been intialized.

[24/Aug/2011:14:50:30] warning ( 8304): reports: lb.runtime: RNTM2019: Daemon

http://ARUNGUP-LAP.st-users.us.oracle.com:28081 has been intialized.

[24/Aug/2011:14:50:30] warning ( 8304): reports: lb.runtime: RNTM2019: Daemon

https://ARUNGUP-LAP.st-users.us.oracle.com:28182 has been intialized.

[24/Aug/2011:14:50:30] info ( 8304): HTTP3072: http-listener-1: http://ARUNGUP-LAP.st-

users.us.oracle.com:80 ready to accept requests

[24/Aug/2011:14:50:30] info ( 8304): CORE3274: successful server startup

This output ensures that the LB Plugin is loaded correctly. Accessing http://localhost:80 in a web page displays the output shown and confirms that the local instance has correctly started.

5.4 Access the Application and Show Session Failover 5.4.1 Access the application at http://localhost/19120-app/ to see the output as: As described earlier this page shows the instance serving the application in the title bar

and the footer, i1 in this case.

Page 22: 19120 Lab Instructions Java

22

5.4.2 Select a few check boxes to indicate your movie preferences and then click the “Submit” button to see an output such as shown.

This application is served by the instance i1. Let’s

stop it by giving the following command (highlighted in bold) from a Command Prompt: D:\tools\glassfish\3.1.1\ogs-glassfish3-

full\glassfish>.\bin\asadmin stop-instance i1

The instance, i1, is stopped.

Command stop-instance executed successfully.

5.4.3 Open a new tab in the browser and access the same URL (http://localhost/19120-

app/) there to see an output as shown. Notice that the application is now served by the

instance i2 and the session data saved on the

instance i1 is automatically failed over to it.

6.0 Monitor the Application

This section will show you to use GlassFish Monitoring Framework in-built and customized application monitoring.

Page 23: 19120 Lab Instructions Java

23

This section should take about 30 mins.

GlassFish comes with a fully-featured Monitoring Framework. Monitoring consists of event emitters, named Probes, and Listeners that receive these events from the Probes. A good way to look at it is that a Probe is simply a public non-static method that has a special annotation. A Probe Provider is a class that contains one or more Probes and which also has a special annotation at class-scope. The infrastructure does the magic of connecting Probes and Listeners at runtime. There are many things you can monitor in your application using the built-in probes that GlassFish provides – without doing anything to your app. For instance you can see when threads are getting created by the Web container to service clients.

6.1 Customized Application Monitoring In this application we are interested in using the Monitoring Framework to provide our own customized monitoring. There are 2 main reasons for using the Monitoring Framework rather than more obvious solutions like writing information to log files:

1. The Monitoring Framework ensures that the Probes have practically no overhead if there is no active listener registered. So the listeners can be attached and detached at will. There is no performance penalty when the listener is detached.

2. We can write interesting (and therefore probably complicated) code in the listener itself that is gathering the data. The listener may not even be part of the application itself. In fact we will run a JavaScript program as our listener.

Let’s take a look at the Monitoring code that is inside the App. The first thing we need to do at run-time is to get a reference to the globally available

object that registers Probe Providers. We use DatabaseSingletonBean.java for this

purpose. The bean is not a Probe Provider itself. But it is a nice handy place where we can inject the Probe Provider Registrar Object. Then we call one line of code to register

our Probe Provider, MovieSessionBean, with the Monitoring Framework.

Here is all of the code needed to do this from DatabaseSingletonBean: import org.glassfish.flashlight.provider.ProbeProviderFactory;

Page 24: 19120 Lab Instructions Java

24

@Resource private ProbeProviderFactory probeProviderFactory;

probeProviderFactory.getProbeProvider(MovieSessionBean.class);

Here the getProbeProvider method takes the class-bytes for

MovieSessionBean.class, finds all the Probes, and injects one line of code into the

end of each such probe method. That one line of code calls into the Monitoring Framework at runtime each time you call the method. The payload are the parameters passed into the probe method that are sent to the framework. The framework then calls all the registered listeners for that particular probe. The probe and their corresponding listeners need to have the exact same method signature to ensure the values are passed correctly.

The byte-code transformer will not do anything until a listener registers for the probe. The transformation is performed just-in-time.

We turned the MovieSessionBean into a Probe Provider by adding this one-line

annotation: @ProbeProvider(moduleProviderName = MODULE_PROVIDER_NAME, moduleName =

MODULE_NAME, probeProviderName = "MovieSessionBean")

The long variable names are really just identifying strings. We want all Probe Provider classes in the same application to share exactly the same first three identifying strings so we turned them into constants. This is modeled, exactly, along the lines of DTrace. Every Probe Provider has 3 names and every probe adds one name for a total of 4 names per probe.

This class has 2 Probes: @Probe(name = "getMovieStarted")

public void getMovieStarted(@ProbeParam("nano") long nano) {

}

@Probe(name = "getMovieFinished")

public void getMovieFinished(@ProbeParam("nano") long nano) {

}

The getMovies method is a likely place to check on timing. So the getMovies method

calls the getMovieStarted probe before the database access, and calls the

Page 25: 19120 Lab Instructions Java

25

getMovieFinished probe after the database access. It sends the current time, in

nanoseconds, as a parameter, to the probes. If there is no Listener attached then the only overhead is the 2 extra calls to get the current time. The application has a simple Servlet that shows every probe in the system that happens to have “19120” in its name. You can use this Servlet to verify that the Probes have been registered correctly. It also shows you all the Probes that have been created on behalf of the app by the containers. To see the output, access this URL in a browser: http://localhost/19120-app/Probes

And here is the output (truncated). Note the 4-string IDs for all Probes JavaOne:19120-app:MovieSessionBean:getMovieFinished (long nano)

JavaOne:19120-app:MovieSessionBean:getMovieStarted (long nano)

glassfish:ejb:bean__null_19120_app_DatabaseSingletonBean_:beanCreatedEvent (long beanId,

java.lang.String appName, java.lang.String modName, java.lang.String ejbName)

glassfish:ejb:bean__null_19120_app_DatabaseSingletonBean_:beanDestroyedEvent (long beanId,

java.lang.String appName, java.lang.String modName, java.lang.String ejbName)

glassfish:ejb:bean__null_19120_app_DatabaseSingletonBean_:containerEnteringEvent (long

beanId, java.lang.String appName, java.lang.String modName, java.lang.String ejbName)

6.1.1 Use the JavaScript Listener

The application’s directory has a java script named 19120-app.js. This script will

register itself as a listener for the 2 probes. In a real world scenario you could add all sorts of interesting complicated code in here. For example it could gather data, and save it in a local repository, generate dynamic charts and reports. In this case the script is listening to the start and finish probes. The script takes the start and finish times, calculate how long the database access took, and then maintain an average of the access time. And then it prints out the current call’s time and the average time.

Before invoking the JavaScript listener you may like to start the instance shutdown earlier by giving the following command (highlighted in bold):

Page 26: 19120 Lab Instructions Java

26

c:\javaone2011\glassfish3>.\bin\asadmin start-instance i1

Attempting to start i1.... Please look at the server log for more details.....

The instance, i1, was started on host localhost

Command start-instance executed successfully.

The JavaScript can be run by giving the following command: c:\javaone2011\19120-app>..\glassfish3\bin\asadmin run-script --target c1 19120-app.js

Listening to i1 on host localhost and port 24848

Listening to i2 on host localhost and port 24849

URL: http://localhost:24848/__monitoring-scripting-client/cometServlet

URL: http://localhost:24849/__monitoring-scripting-client/cometServlet

And then the following output is shown after selecting a few movies, clicking on Submit, and going through that a few times. [i1] Time to get all of the movies from the database for this one call = 10.693949 milliseconds

[i1] Time to get all of the movies from the database for this one call = 18.037415 milliseconds

[i1] Current Average Time to get all of the movies from the database = 14.365682 milliseconds

[i1] Current Average Time to get all of the movies from the database = 10.693949 milliseconds

[i1] Time to get all of the movies from the database for this one call = 84.91996 milliseconds

[i1] Time to get all of the movies from the database for this one call = 15.515721 milliseconds

[i1] Current Average Time to get all of the movies from the database = 32.29176125 milliseconds

[i1] Time to get all of the movies from the database for this one call = 26.142038 milliseconds

[i1] Current Average Time to get all of the movies from the database = 31.061816599999997 milliseconds

[i1] Time to get all of the movies from the database for this one call = 28.490124 milliseconds

[i1] Current Average Time to get all of the movies from the database = 30.633201166666666 milliseconds

[i1] Time to get all of the movies from the database for this one call = 9.940943 milliseconds

[i1] Current Average Time to get all of the movies from the database = 27.677164285714287 milliseconds

[i1] Current Average Time to get all of the movies from the database = 37.88377466666667 milliseconds

[i2] Time to get all of the movies from the database for this one call = 326.420717 milliseconds

[i2] Current Average Time to get all of the movies from the database = 326.420717 milliseconds

[i2] Time to get all of the movies from the database for this one call = 7.147516 milliseconds

[i2] Current Average Time to get all of the movies from the database = 166.7841165 milliseconds

[i2] Time to get all of the movies from the database for this one call = 5.458838 milliseconds

[i2] Current Average Time to get all of the movies from the database = 113.00902366666668 milliseconds

[i2] Time to get all of the movies from the database for this one call = 5.768917 milliseconds

[i2] Current Average Time to get all of the movies from the database = 86.198997 milliseconds

[i2] Time to get all of the movies from the database for this one call = 7.379094 milliseconds

[i2] Current Average Time to get all of the movies from the database = 70.43501640000001 milliseconds

Notice a few things about the power of this technique:

1. You can run the script from any computer. 2. The overhead of processing the data can be done on a different machine from the

server where the app is deployed. 3. You can do anything at all with the data – which is flowing directly from the app. 4. It will simultaneously listen to every instance in a cluster and can easily identify

which instance the data is coming from. This can be very handy when trouble-shooting problems with load balancing the same app running on multiple instances.

5. You can easily run the script against any one specific instance or DAS itself.

Page 27: 19120 Lab Instructions Java

27

6. When you exit the run-script command, the overhead goes away.

6.2 Automatic Monitoring Data

The Web Container has many Probes installed. These are automatically fired as you use your application. To get a taste of the power of Automatic Monitoring Data, run these commands: 1. The Monitoring data collection is turned off by default. We need to enable it for the

web-container by running this command (highlighted in bold): C:\javaone2011\glassfish3>.\bin\asadmin enable-monitoring --target c1 --

modules web-container=HIGH

Command enable-monitoring executed successfully.

2. Access the application URL in a browser. 3. Take a look at the number of times the application has run in each instance by running

this command (use the quotes because of the embedded space): C:\javaone2011\glassfish3>.\bin\asadmin get -m "c1.applications.19120-

app.server.Faces Servlet.requestcount-count"

4. Repeat from step 2 Here is a typical output. Notice that you can easily see how the load is being balanced.

i1:

i1.applications.19120-app.server.Faces Servlet.requestcount-count = 3

i2:

i2.applications.19120-app.server.Faces Servlet.requestcount-count = 5

The Web container collect several other statistics for the application that can be seen as:

asadmin get -m "c1.applications.19120-app.*”

7.0 Troubleshooting Tips

Page 28: 19120 Lab Instructions Java

28

7.1 How to restart Oracle Web Server Administration Server ?

1. Start an Administrator-privileged Command Prompt

2. Go to the Oracle Web Server installation directory (default location is C:\Program

Files\Oracle\WebServer7)

3. Give the command admin-server\bin\restartserv.bat.

7.2 How to change Oracle Web Server password ?

1. Start an Administrator-privileged Command Prompt

2. Go to the Oracle Web Server installation directory (default location is C:\Program

Files\Oracle\WebServer7)

3. Give the command bin/wadm reset-admin-password

7.3 How to restart GlassFish ? From the CLI

GlassFish is installed in the C:\javaone2011\glassfish3 directory. Give the following command to restart the DAS: .\bin\asadmin domain1 restart

In NetBeans IDE

Go to “Services” panel

Expand “Servers” node

Select the GlassFish node

Right-click and select “Restart”.

7.4 Getting an error “failed to join group c1” ? The machine is using a wireless network and see the error stack trace in “server.log” as:

Page 29: 19120 Lab Instructions Java

29

[#|2011-08-23T12:29:22.724-0700|SEVERE|oracle-

glassfish3.1.1|javax.org.glassfish.gms.org.glassfish.g

ms|_ThreadID=1;_ThreadName=Thread-2;|GMSAD1017: GMS failed to start. See stack trace for

additional information.

com.sun.enterprise.ee.cms.core.GMSException: failed to join group c1

at com.sun.enterprise.ee.cms.impl.base.GMSContextImpl.join(GMSContextImpl.java:181)

at

com.sun.enterprise.ee.cms.impl.common.GroupManagementServiceImpl.join(GroupManagementServ

iceImpl.java:382)

. . .

Caused by: java.io.IOException: can not find a first InetAddress

at

com.sun.enterprise.mgmt.transport.grizzly.GrizzlyNetworkManager.start(GrizzlyNetworkManag

er.java:376)

at com.sun.enterprise.mgmt.ClusterManager.<init>(ClusterManager.java:140)

... 27 more

This error occurs if the IP address of the current machine cannot be determined correctly. This typically occurs if the machine is configured to use a wireless network. In such a case you may have to specify a system property for each instance named “GMS-BIND-INTERFACE-ADDRESS-<clustername>” to the IP address of the machine where the instance is running. In our case, <clustername> is “c1” and all the instances are running on the localhost. So we’ll need to set the property “GMS-BIND-INTERFACE-ADDRESS-c1” to the IP address of the local machine. This can be done in web-based Admin Console as:

1. Click on the clustername “c1” 2. Click on “Instances” tab 3. Click on the instance name “i1” 4. Click on “Properties” tab 5. Click on “Add Property” button 6. Specify the new “Instance

Variable Name” as “GMS-BIND-INTERFACE-ADDRESS-c1” and the “Override Value” to IP address of the current machine.

7. Click on “Save”. The updated output will look like as shown.

8. Repeat steps 3-8 for each instance in the cluster.

Page 30: 19120 Lab Instructions Java

30

7.5 Deployment gives “Command succeeded with Warning” The application deployment gives the following warning message:

This error may occur if you try to re-deploy the application. This error can be ignored as the application deployment tries to create a SEQUENCE table for automatic generation of the primary keys even though it may exist.

7.6 Session data not preserved

This error occurs if you try to access the application on different instances (i1 and i2 in

our case) without a front-end load balancer and using different host names. Make sure to use the same host name to access the application on different instances. This issue does not occur when the multiple instances are front-ended with a load balancer as the same host name is passed to the underlying instances.

7.7 No monitoring data from the script Make sure you are accessing the app in a browser. You should see a few lines of output every time you do something with the app in a browser. If not, restart all the servers and try again. For example: asadmin stop-cluster c1

asadmin stop-domain

asadmin start-domain

asadmin start-cluster c1

Page 31: 19120 Lab Instructions Java

31

8.0 Acknowledgements

This hands-on lab was gracious reviewed by the following GlassFish community member:

Markus Eisele Thank you very much for taking time to provide the valuable feedback.

9.0 References 9.1 GlassFish 9.1.1 Community Website: http://glassfish.org 9.1.2 Oracle GlassFish Server: http://oracle.com/goto/glassfish 9.1.4 GlassFish Forum: http://www.java.net/forums/glassfish/glassfish 9.2 NetBeans: http://netbeans.org 9.3 Clustering in GlassFish 3.1: http://glassfish.java.net/public/clustering31.html 9.4 GlassFish Load Balancer Setup and Configuration video: http://www.youtube.com/playlist?list=PLFC8CB788718C4EAE 9.5 Configuring Web Servers for HTTP Load Balancing in GlassFish: http://download.oracle.com/docs/cd/E18930_01/html/821-2426/gchvt.html