PEAT Online Course -
How To Improve Your Personal Performance in Java work Technology

  • YOU + CONTROL
  • Focus
  • Autonomy
  • Proficiency
  • Purpose
  • Covid-19!!!
  • Deep Hidden Meaning
  • FREE updates
GET 75% OFF NOW
Your Promo code: TECHLAYOFF
Exclusively on
VIMEO ON-DEMAND

GlassFish 4 Promoted Build, Gradle and Embedded Application Server

31 January 2013 Comments off

7 minutes

1550

Very recently, perhaps towards end of last year, the GlassFish open source team released GlassFish 4.0 beta 72 as a promoted build. Arun Gupta posted an article on the Maven coordinates for the GlassFish 4 .0 beta 72 on his blog. This release was significant because the team published the artifacts into a maven repository.

This year, 2013, I am the author of a up and coming Java EE 7 User Guide and so it is important that I investigate the latest GlassFish, especially since it is the reference implementation of the specification. I want to actually research and investigate how far the latest Java Servlets 3.1, Web Sockets and JAX-RS specifications behave in the server.

Here is a Gradle build script that I wrote last night to execute an GlassFish Embedded application:

apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'eclipse'
apply plugin: 'idea'

group = 'com.javaeehandbook.book1'
archivesBaseName = 'ch06-servlets-basic'
version = '1.0'

repositories {
 mavenCentral()
 maven {
   url 'https://maven.java.net/content/groups/promoted'
 }
 maven {
   url 'https://repository.jboss.org/nexus/content/groups/public'
 }
}

dependencies {
 compile 'org.glassfish.main.extras:glassfish-embedded-all:4.0-b72'
 compile 'javax:javaee-api:7.0-b72'
 testCompile 'junit:junit:4.10'
}

// Override Gradle defaults - a force an exploded JAR view
sourceSets {
 main {
   output.resourcesDir = 'build/classes/main'
   output.classesDir = 'build/classes/main'
 }
 test {
   output.resourcesDir = 'build/classes/test'
   output.classesDir = 'build/classes/test'
 }
}

task(run, dependsOn: 'classes', type: JavaExec) {
 description = 'Runs the main application'
 main = 'je7hb.common.webcontainer.embedded.glassfish.EmbeddedRunner'
 classpath = sourceSets.main.runtimeClasspath
}

The key to the build script is the order of the dependencies. I found that the glassfish-embedded-all had to be first dependency on the list, otherwise there would be ValidationException from the Hibernate Validator (bean validator) jar not being found. The exception message was "javax.validation.ValidationException: Unable to load Bean Validation provider".

The Gradle build also references the GlassFish Java repositories, which is the second key point.

Here is the EmbeddedRunner, the Java application code:

package je7hb.common.webcontainer.embedded.glassfish;

import org.glassfish.embeddable.*;
import java.io.*;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;

public class EmbeddedRunner {

 private int port;
 private AtomicBoolean initialized = new AtomicBoolean();
 private GlassFish glassfish;

 public EmbeddedRunner(int port) {
   this.port = port;
 }

 public EmbeddedRunner init() throws Exception{
   if ( initialized.get() ) {
     throw new RuntimeException("runner was already initialized");
   }
   BootstrapProperties bootstrapProperties = new BootstrapProperties();
   GlassFishRuntime glassfishRuntime = GlassFishRuntime.bootstrap(bootstrapProperties);

   GlassFishProperties glassfishProperties = new GlassFishProperties();
   glassfishProperties.setPort("http-listener", port);
   String [] paths = System.getProperty("java.class.path").split(File.pathSeparator);
   for (int j=0; j<paths.length; ++j) {
     System.out.printf("classpath[%d] = %s\n", j, paths[j]);
   }
   glassfish = glassfishRuntime.newGlassFish(glassfishProperties);
   initialized.set(true);
   return this;
 }

  private void check() {
    if ( !initialized.get() ) {
      throw new RuntimeException("runner was not initialised");
    }
  }

  public EmbeddedRunner start() throws Exception{
    check();
    glassfish.start();
    return this;
  }

  public EmbeddedRunner stop() throws Exception{
    check();
    glassfish.stop();
    return this;
  }

  public static void main(String args[]) throws Exception {
    EmbeddedRunner runner = new EmbeddedRunner(8080).init().start();
    Thread.sleep(1000);
    runner.stop();
  }
}

The class executes the embedded GlassFish as the beginnings of a containerless build, which is a term that James Ward and others have coined. This class starts GlassFish, waits one second, and then shuts it down again. The code works with Gradle, by invoking at the command line gradle run or through an IDE. I used the command gradle idea to generate IDEA project files.

Here is sample output from IntelliJ IDEA 12:

/Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home/bin/java -Didea.launcher.port=7537 "-Didea.launcher.bin.path=/Applications/IntelliJ IDEA 11.app/bin" -Dfile.encoding=UTF-8 -classpath "/Users/Developer/Documents/IdeaProjects/javaee7-handbook/ch06/servlets-basic/out/production/servlets-basic:/Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home/lib/javafx-doclet.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home/lib/tools.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home/jre/lib/htmlconverter.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home/jre/lib/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home/jre/lib/JObjC.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Users/Developer/.gradle/caches/artifacts-15/filestore/org.glassfish.main.extras/glassfish-embedded-all/4.0-b72/jar/942b982d5c005806a08843d2a1f411f278c04077/glassfish-embedded-all-4.0-b72.jar:/Users/Developer/.gradle/caches/artifacts-15/filestore/javax/javaee-api/7.0-b72/jar/56d50eaa8d21c2f70394f607efc1aa27c360141d/javaee-api-7.0-b72.jar:/Users/Developer/.gradle/caches/artifacts-15/filestore/javax.activation/activation/1.1/jar/e6cb541461c2834bdea3eb920f1884d1eb508b50/activation-1.1.jar:/Users/Developer/.gradle/caches/artifacts-15/filestore/com.sun.mail/javax.mail/1.4.6-rc1/jar/5c5de8592e570afb595a8be727b484d438b49d69/javax.mail-1.4.6-rc1.jar:/Applications/IntelliJ IDEA 11.app/lib/idea_rt.jar" com.intellij.rt.execution.application.AppMain je7hb.common.webcontainer.embedded.glassfish.EmbeddedRunner
classpath[0] = /Users/Developer/Documents/IdeaProjects/javaee7-handbook/ch06/servlets-basic/out/production/servlets-basic
classpath[1] = /Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home/lib/ant-javafx.jar
classpath[26] = /Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home/jre/lib/ext/zipfs.jar
classpath[27] = /Users/Developer/.gradle/caches/artifacts-15/filestore/org.glassfish.main.extras/glassfish-embedded-all/4.0-b72/jar/942b982d5c005806a08843d2a1f411f278c04077/glassfish-embedded-all-4.0-b72.jar
classpath[28] = /Users/Developer/.gradle/caches/artifacts-15/filestore/javax/javaee-api/7.0-b72/jar/56d50eaa8d21c2f70394f607efc1aa27c360141d/javaee-api-7.0-b72.jar
classpath[29] = /Users/Developer/.gradle/caches/artifacts-15/filestore/javax.activation/activation/1.1/jar/e6cb541461c2834bdea3eb920f1884d1eb508b50/activation-1.1.jar
classpath[30] = /Users/Developer/.gradle/caches/artifacts-15/filestore/com.sun.mail/javax.mail/1.4.6-rc1/jar/5c5de8592e570afb595a8be727b484d438b49d69/javax.mail-1.4.6-rc1.jar
classpath[31] = /Applications/IntelliJ IDEA 11.app/lib/idea_rt.jar
Found populator: org.glassfish.kernel.embedded.EmbeddedDomainXml
Jan 31, 2013 10:05:12 AM org.glassfish.security.services.impl.authorization.AuthorizationServiceImpl initialize
INFO: Authorization Service has successfully initialized.
Jan 31, 2013 10:05:12 AM org.hibernate.validator.internal.util.Version <clinit>
INFO: HV000001: Hibernate Validator 5.0.0.Alpha1
Jan 31, 2013 10:05:13 AM com.sun.enterprise.config.modularity.StartupConfigBeanOverrider postConstruct
INFO: Starting the config overriding procedure
Jan 31, 2013 10:05:13 AM com.sun.enterprise.config.modularity.StartupConfigBeanOverrider postConstruct
INFO: Finished the config overriding procedure
Jan 31, 2013 10:05:13 AM com.sun.enterprise.v3.services.impl.GrizzlyProxy start
INFO: Grizzly Framework 2.3 started in: 18ms - bound to [/0.0.0.0:8,080]
Jan 31, 2013 10:05:13 AM com.sun.enterprise.v3.services.impl.GrizzlyProxy start
INFO: Grizzly Framework 2.3 started in: 3ms - bound to [/0.0.0.0:8,081]
Jan 31, 2013 10:05:13 AM com.sun.enterprise.v3.admin.adapter.AdminEndpointDecider setGuiContextRoot
INFO: Admin Console Adapter: context root: /admin
Jan 31, 2013 10:05:13 AM com.sun.enterprise.v3.admin.adapter.AdminEndpointDecider setGuiContextRoot
INFO: Admin Console Adapter: context root: /admin
Jan 31, 2013 10:05:13 AM com.sun.enterprise.v3.admin.adapter.AdminEndpointDecider setGuiContextRoot
INFO: Admin Console Adapter: context root: /admin
Jan 31, 2013 10:05:13 AM com.sun.enterprise.v3.server.AppServerStartup$StartupActivator awaitCompletion
INFO: Undefined Product Name - define product and version info in config/branding 0.0.0 (0) startup time : Embedded (1,204ms), startup services(856ms), total(2,060ms)
Jan 31, 2013 10:05:13 AM org.glassfish.admin.mbeanserver.JMXStartupService$JMXConnectorsStarterThread run
INFO: JMXStartupService has disabled JMXConnector system
Jan 31, 2013 10:05:13 AM com.sun.enterprise.connectors.jms.util.JmsRaUtil getInstalledMqVersion
WARNING: RAR7000 : Check for a new version of MQ installation failed : /var/folders/kr/vj5fd5s91g76_t348ndnbtxr0000gn/T/gfembed883899172293116872tmp/lib/install/applications/jmsra/../imqjmsra.rar (No such file or directory):/var/folders/kr/vj5fd5s91g76_t348ndnbtxr0000gn/T/gfembed883899172293116872tmp/lib/install/applications/jmsra/imqjmsra.rar
Jan 31, 2013 10:05:14 AM org.glassfish.admin.mbeanserver.JMXStartupService shutdown
INFO: JMXStartupService and JMXConnectors have been shut down.
JdbcRuntimeExtension, getAllSystemRAResourcesAndPools = [GlassFishConfigBean.org.glassfish.jdbc.config.JdbcResource, GlassFishConfigBean.org.glassfish.jdbc.config.JdbcResource, GlassFishConfigBean.org.glassfish.jdbc.config.JdbcConnectionPool, GlassFishConfigBean.org.glassfish.jdbc.config.JdbcConnectionPool]
Jan 31, 2013 10:05:15 AM com.sun.enterprise.v3.server.AppServerStartup stop
INFO: Shutdown procedure finished

Process finished with exit code 0

You should be seeing something the above output in the IDE if you are doing it right. It would appear we will have to be on our guard until JDK 9 in order to avoid class path loading issues. My book on the user guide to Java EE 7 is scheduled for the Summer of 2013.

Happy Testing.

+PP+

Hey all! Thanks for visiting. I provide fringe benefits to interested readers: checkout consultancy, training or mentorship Please make enquiries by email or
call +44 (0)7397 067 658.

Due to the Off-Payroll Working plan for the UK government, I am enforcing stricter measures on contracts. All potential public sector GOV.UK contracts engagements must be approved by QDOS and/or SJD Accounting. Please enquire for further information.