Code listings for "Clustering and High Availability Made Simple with GlassFish (Part 1)," Java Magazine, January/February 2012


Listing 1
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<!DOCTYPE html>
<html>
<head>
    <title>Hello World!</title>
</head>
<body>
<f:view>
<p>
    The current time is:
    <em>
        <h:outputText value="#{clock.now}"/>
    </em>
    <a href="index.jsp">(reload)</a>
</p>
</f:view>
</body>
</html>


Listing 2
package clockee.beans;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Named;
import java.util.Date;

@Named
@ApplicationScoped
public class Clock {

    public String getNow() {
        return new Date().toString();
    }
}


Listing 3
$ asadmin list-domains
domain1 running
Command list-domains executed successfully.
$ asadmin create-cluster my-first-cluster
Command create-cluster executed successfully.
$ asadmin list-clusters
my-first-cluster not running
Command list-clusters executed successfully.


Listing 4
$ asadmin create-local-instance --cluster my-first-cluster local-instance-1
Rendezvoused with DAS on localhost:4848.
Port Assignments for server instance local-instance-1: 
JMX_SYSTEM_CONNECTOR_PORT=28686
JMS_PROVIDER_PORT=27676
HTTP_LISTENER_PORT=28080
ASADMIN_LISTENER_PORT=24848
JAVA_DEBUGGER_PORT=29009
IIOP_SSL_LISTENER_PORT=23820
IIOP_LISTENER_PORT=23700
OSGI_SHELL_TELNET_PORT=26666
HTTP_SSL_LISTENER_PORT=28181
IIOP_SSL_MUTUALAUTH_PORT=23920
Command create-local-instance executed successfully.
$ asadmin create-local-instance --cluster my-first-cluster local-instance-2
Rendezvoused with DAS on localhost:4848.
Using DAS host localhost and port 4848 from existing das.properties for node
localhost-domain1. To use a different DAS, create a new node using 
create-node-ssh or create-node-config. Create the instance with the new node 
and correct host and port:
asadmin --host das_host --port das_port create-local-instance --node node_name instance_name.
Port Assignments for server instance local-instance-2: 
JMX_SYSTEM_CONNECTOR_PORT=28687
JMS_PROVIDER_PORT=27677
HTTP_LISTENER_PORT=28081
ASADMIN_LISTENER_PORT=24849
JAVA_DEBUGGER_PORT=29010
IIOP_SSL_LISTENER_PORT=23821
IIOP_LISTENER_PORT=23701
OSGI_SHELL_TELNET_PORT=26667
HTTP_SSL_LISTENER_PORT=28182
IIOP_SSL_MUTUALAUTH_PORT=23921
Command create-local-instance executed successfully.


Listing 5
$ asadmin list-instances -1
NAME              HOST       PORT   PID  CLUSTER           STATE         
local-instance-1  localhost  24848  -1   my-first-cluster   not running  
local-instance-2  localhost  24849  -1   my-first-cluster   not running  
Command list-instances executed successfully.


Listing 6
$ asadmin start-cluster my-first-cluster
Command start-cluster executed successfully.
$ asadmin list-instances -1
NAME              HOST       PORT   PID    CLUSTER           STATE     
local-instance-1  localhost  24848  16547  my-first-cluster   running  
local-instance-2  localhost  24849  16548  my-first-cluster   running  
Command list-instances executed successfully.


Listing 7
$ asadmin list-nodes -1
NODE NAME           TYPE     NODE HOST   INSTALL DIRECTORY                REFERENCED BY
localhost-domain1   CONFIG   localhost   /usr/local/Cellar/glassfish/3.1/libexec   local-instance-1, local-instance-2  
Command list-nodes executed successfully.


Listing 8
$ asadmin stop-instance local-instance-1
The instance, local-instance-1, is stopped.
Command stop-instance executed successfully. 
$ asadmin list-instances -1
NAME                       HOST         PORT    PID       CLUSTER                 STATE         
local-instance-1  localhost  24848  -1           my-first-cluster   not running  
local-instance-2  localhost  24849  16548  my-first-cluster   running      
Command list-instances executed successfully.
$ asadmin start-instance local-instance-1
Waiting for local-instance-1 to start ................
Successfully started the instance: local-instance-1
instance Location: /usr/local/Cellar/glassfish/3.1/libexec/glassfish/nodes/localhost-domain1/local-instance-1
Log File: /usr/local/Cellar/glassfish/3.1/libexec/glassfish/nodes/localhost-domain1/local-instance-1/logs/server.log
Admin Port: 24848
Command start-local-instance executed successfully.
The instance, local-instance-1, was started on host localhost
Command start-instance executed successfully.
$ asadmin list-instances -1
NAME                       HOST         PORT    PID       CLUSTER                 STATE         
local-instance-1  localhost  24848  16846  my-first-cluster   running  
local-instance-2  localhost  24849  16548  my-first-cluster   running  
Command list-instances executed successfully.


Listing 9
$ asadmin setup-ssh --sshuser 
julien 192.168.56.101
Enter SSH password for julien@192.168.56.101> 
Copied keyfile /Users/julien/.ssh/id_rsa.pub to julien@192.168.56.101
Successfully connected to julien@192.168.56.101 using keyfile 
/Users/julien/.ssh/id_rsa
Command setup-ssh executed successfully.


Listing 10
$ asadmin install-node --installdir /home/julien/glassfishv3 192.168.56.101
Created installation zip /Users/julien/glassfish7019987135256228796.zip
Successfully connected to julien@192.168.56.101 using keyfile 
/Users/julien/.ssh/id_rsa
Copying /Users/julien/glassfish7019987135256228796.zip (81441890 bytes) to 192.168.56.101:/home/julien/glassfishv3
Installing glassfish7019987135256228796.zip into 192.168.56.101:/home/julien/glassfishv3
Removing 192.168.56.101:/home/julien/glassfishv3/
glassfish7019987135256228796.zip
Fixing file permissions of all files under 192.168.56.101:/home/julien/glassfishv3/bin
Command install-node executed successfully.


Listing 11
$ asadmin create-instance --cluster my-first-cluster --node ubuntuvm vm1
Command _create-instance-filesystem executed successfully.
Port Assignments for server instance vm1: 
JMX_SYSTEM_CONNECTOR_PORT=28686
JMS_PROVIDER_PORT=27676
HTTP_LISTENER_PORT=28080
ASADMIN_LISTENER_PORT=24848
JAVA_DEBUGGER_PORT=29009
IIOP_SSL_LISTENER_PORT=23820
IIOP_LISTENER_PORT=23700
OSGI_SHELL_TELNET_PORT=26666
HTTP_SSL_LISTENER_PORT=28181
IIOP_SSL_MUTUALAUTH_PORT=23920
The instance, vm1, was created on host 192.168.56.101
Command create-instance executed successfully.


Listing 12
$ asadmin start-instance vm1
Waiting for vm1 to start ............................
Successfully started the instance: vm1
instance Location: /home/julien/glassfishv3/glassfish/nodes/ubuntuvm/vm1
Log File: /home/julien/glassfishv3/glassfish/nodes/ubuntuvm/vm1/logs/server.log
Admin Port: 24848
Command start-local-instance executed successfully.
The instance, vm1, was started on host 192.168.56.101
Command start-instance executed successfully.
$ asadmin list-instances -1
NAME                       HOST                    PORT    PID       CLUSTER                 STATE         
local-instance-1  localhost             24848  23449  my-first-cluster   running  
local-instance-2  localhost             24849  23450  my-first-cluster   running  
vm1                            192.168.56.101  24848  15376  my-first-cluster   running  
Command list-instances executed successfully.



Copyright 2012, Oracle Corporation