Notice: This resource is no longer being actively maintained and as a result some information may be out of date or inaccurate. Please see www.cloudbees.com/documentation for technical instructions or support.cloudbees.com for support related concerns.
Glassfish 4

Glassfish 4

Last modified by Cyrille Le Clerc on 2014/02/26 10:05

Deploying on Glassfish 4 Web Profile

bees app:deploy -t glassfish4 -a my-app path/to/my/app.war

Where my-app is the name of the application you want to create or to update with app.war.

Pre requisite: setting context root to "/"

Don't forget to specify <context-root>/</context-root> in WEB-INF/glassfish-web.xml. Otherwise, your application will be deployed under the context "/app/".

Extract from glassfish-web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
<glassfish-web-app>
 <context-root>/</context-root>
  ...
</glassfish-web-app>

Database Bindings

CloudBees MySQL Databases are a commonly used resource type that that can be easily bound to your application using the following CloudBees SDK command:

Binding Command

bees app:bind -a my-app -db my-db -as mydb
  • -a my-app the ID of your application (can also be formatted: ACCOUNT/APPNAME)
  • -db my-db the name of your CloudBees MySQL database (can also be formatted: ACCOUNT/DBNAME)
  • -as mydb the name of the binding which is used to identify the binding and to compose the name of the environment variables used to describe this binding.

Add mysql-connector jar in the META-INF/lib folder of your webapp. The content of the META-INF/lib/ folder is automatically added to Glassfish server classpath.

A JNDI DataSource will be created in your Glassfish 4 container. Valid JNDI names are:

  • java:comp/env/jdbc/mydb: the official naming, qualified private name valid / OK
  • jdbc/mydb: unqualified relative JNDI name valid / OK

Invalid JNDI names

  • java:jdbc/mydb and java:/jdbc/mydb: qualified relative names are not supported by Glassfish
  • java:global/env/jdbc/mydb: qualified global name is not supported by Glassfish

Optional declaration in web.xml

<!--
<resource-ref> declaration is NOT mandatory, Glassfish4 discovers the datasource without <resource-ref> declaration
-->

<resource-ref>
 <res-ref-name>jdbc/mydb</res-ref-name>
 <res-type>javax.sql.DataSource</res-type>
 <res-auth>Container</res-auth>
 <res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>

Java Code sample

Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/mydb"); // "jdbc/mydb" is also a valid JNDI name
Connection conn = ds.getConnection();
ResultSet rst = stmt.executeQuery("select 1");
while (rst.next()) {
  out.print("resultset result: " + rst.getString(1));
}
rst.close();
stmt.close();
conn.close();

Securing web application with JaaS Authentication

CloudBees Glassfish4 runtime offer JaaS Authentication based on a JDBCRealm which means that the users and groups are defined in a database.

See details on Glassfish4 JaaS Authentication with JDBC Realm.

Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy

Change the JVM to use java7-jce_ustrength:

bees config:set -a myapp -R java_version=java7-jce_ustrength

Customising the JVM (Java 8 ...)

You can provide your own JVM or use a JVM provided by CloudBees on the repo http://cb-s3-dist.s3.amazonaws.com/ specifying a URL for the java_version configuration parameter.

Sample:

bees config:set -a myapp -R java_version=http://cb-s3-dist.s3.amazonaws.com/java/openjdk/jdk-8-ea-bin-b111-linux-x64-10_oct_2013.gz

Pre-requisites on the provided JVM:

  • The url must use http://  or https://  and preferably be an Amazon S3 bucket located in the same region as your deployed server
  • The archive must be a ".zip", a ".tgz" or a ".tar.gz"
  • The JAVA_HOME must be the root of the expanded archive or there must be just one first level dir in the archive and that first level dir is the JAVA_HOME

Clickstack Release Notes

See https://github.com/CloudBees-community/glassfish4-clickstack/releases.

Glassfish4 FAQ

I get Glassfish Homepage instead of my application

There could be several reasons why you get Glassfish Default Page "Your Server is now running" instead of your application page

  • Your application does not contain glassfish-web.xml with <context-root>/</context-root>. Then your application is deployed under http://<<my-app>.<<my-account>>.cloudbees.net/app/.
  • Your application is not yet deployed. CloudBees Glassfish4 clickstack relies on the Application Auto Deployment feature (/autodeploy folder).
    • Please look in your server logs for the message
      [#|2013-06-04T13:12:39.816+0000|INFO|glassfish4.1.2|javax.enterprise.system.tools.deployment.org.glassfish.deployment.common|_ThreadID=31;_ThreadName=AutoDeployer;|[AutoDeploy] Successfully autodeployed : /mnt/genapp/apps/0e177a87/glassfish4/glassfish/domains/domain1/autodeploy/app.war.|#]
    • To handle this temporary Glassfish default page, you can use the Blue Green Deployment technics.
  • The deployment of your application failed. An exception message should be displayed in the logs.

Glassfish Default Page

Can I use Glassfish 4 Full Platform

Please see our Glassfish 4 Full Platform page.

How to add libraries to Glassfish server classpath

It is sometimes useful to add libraries directly to Glassfish server classpath (JDBC datasource, Mail Session, ...).

To do so, just add them to the META-INF/lib/ folder of your webapp.

Sample maven configuration to add Oracle JDBC driver jar to Glassfish classpath (you don't need to add mysql jar or postgresql jar when you bind a mysql/postgresql database to your app):
<project ....>
    ...
   <build>
       <plugins>
            ...
           <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-dependency-plugin</artifactId>
               <version>2.7</version>
               <executions>
                   <execution>
                       <id>copy</id>
                       <phase>prepare-package</phase>
                       <goals>
                           <goal>copy</goal>
                       </goals>
                       <configuration>
                           <artifactItems>
                               <artifactItem>
                                   <groupId>com.oracle</groupId>
                                   <artifactId>ojdbc6</artifactId>
                                   <version>11.2.0.3</version>
                                   <type>jar</type>
                               </artifactItem>
                           </artifactItems>
                           <outputDirectory>${project.build.directory}/appserver-dependency</outputDirectory>
                       </configuration>
                   </execution>
               </executions>
           </plugin>
           <plugin>
               <groupId>org.apache.maven.plugins</groupId>
               <artifactId>maven-war-plugin</artifactId>
               <version>2.3</version>
               <configuration>
                   <webResources>
                       <resource>
                           <directory>${project.build.directory}/appserver-dependency</directory>
                           <targetPath>META-INF/lib/</targetPath>
                       </resource>
                   </webResources>
               </configuration>
           </plugin>
       </plugins>
   </build>
</project>

'False positive' log warning messages

JBoss Weld warning messages

The following warning messages should be ignored, they are 'false-positive' alert messages.
[#|2013-06-09T15:00:27.233+0000|WARNING|glassfish 4.0|org.jboss.weld.interceptor.util.InterceptionTypeRegistry|_ThreadID=1;_ThreadName=main;_TimeMillis=1370790027233;_LevelValue=900;|
  Class 'javax.ejb.PostActivate' not found, interception based on it is not enabled|#]

[#|2013-06-09T15:00:27.234+0000|WARNING|glassfish 4.0|org.jboss.weld.interceptor.util.InterceptionTypeRegistry|_ThreadID=1;_ThreadName=main;_TimeMillis=1370790027234;_LevelValue=900;|
  Class 'javax.ejb.PrePassivate' not found, interception based on it is not enabled|#]

EJB Timer warning message

This message shows up when and EJB Timer with @Scheduled(persistent = false, ...) is declared and should be ignored.
[#|2013-06-09T15:00:26.006+0000|WARNING|glassfish 4.0|javax.enterprise.system.container.ejb.com.sun.ejb.containers|_ThreadID=1;_ThreadName=main;_TimeMillis=1370790026006;_LevelValue=900;_MessageID=ejb.warn_feature_requires_full_profile;|
  EJB5187:The feature use of persistent EJB Timer Service requires Full Java EE Profile to be supported|#]

Tags:
Created by Cyrille Le Clerc on 2013/06/08 17:58