Categories
Docker Java

Creating a minimal Docker image with Glassfish

docker-logoI have been using Docker to run Glassfish in a container and I noticed some news about using Alpine Linux.Apparently, you create much smaller containers with Alpine Linux. This article from Atlassian (https://developer.atlassian.com/blog/2015/08/minimal-java-docker-containers/) mentioned a minimal base image with OpenJDK.

Since I prefer to use the Oracle JDK with Glassfish 4.1, I ran a few tests myself to compare a phusion baseimage with Alpine Linux.

My Phusion based container produced an image 1130 MB large, while the Alpine Linux based image was only 392 MB. To produce a working application, I used the Primefaces showcase WAR.

I am surprised about the size differences, see the resulting images:

Phusion baseimage + Java 8 + Glassfish 4.1

phusion/baseimage         305 MB
ubuntu-oracle-jdk8        892 MB
koert/glassfish-4.1       1130 MB
koert/glassfish-showcase  1156 MB

Alpine Linux + Java 8 + Glassfish 4.1

frolvlad/alpine-oraclejdk8       170.8 MB
koert/alpine-glassfish-4.1       391.8 MB
koert/alpine-glassfish-showcase  417.8 MB

My examples at Github

 

Categories
Java

Functional style programming with Optional in Java 8

With the Java 8, we have a more functional style programming available and the Optional type. This type is like Option in Scala and should replace the commonly used pattern of returning null if something is not found or available.

See the below example of a repository – authenticateUser2 returns a null if it cannot authenticate the user.

Using Optional, authenticateUser returns Optional.empty is it cannot authenticate the user, otherwise it will return an Optional that wraps the AuthenticationToken with Optional.of.

Using the classical null-checking pattern, LoginService2 uses an if-then-else structure:

Using a functional style of checking for an empty result, LoginService uses the map and orElse methods:

This new style replaces 8 lines of code with 3 lines. You may wonder – what is the big deal – 5 lines of code?

At first glance, this map orElse pattern looks a little puzzling. After seeing this a few times, this new functional style describes the intention of the code more clearly than the if-then-else structure. The intention is that it returns a token, or else a not-acceptable status code.

The purpose of code is, next to implementing a solution, to communicate to the maintainer what you meant to do. We software developers sometimes spend too much time trying to understand what the code does, it would be great if we can see in one glance the intention – this will prevent many bugs and misunderstandings, and improve the quality of our work.

 

Categories
Java

Restful service with JAX RS and CDI on Glassfish

I was working on a RESTful service with JAX RS on Glassfish and pieced together a working implementation with CDI dependency injection.

 

This REST service is pretty simple, when you access /test/hello?name=John with a GET, you will get “Hello world John” back. Note the @ManagedBean annotation – this will make CDI dependency injection working in a JAX RS instantiated service.

The HelloRepository is injected with CDI:

You can package this as a WAR file and deploy it on a Glassfish server. To make this work, you need a minimal web.xml in WEB-INF:

To enable CDI, you need an empty beans.xml in WEB-INF:

And to make JAX RS working correctly and set the root path, you need this class:

To set the context root, you could also add a glassfish-web.xml in WEB-INF – this is of course Glassfish specific.

After figuring this out, it is easy to configure and doesn’t need any complicated XML. I got the above configuration working on Glassfish 3 and 4.

Categories
Docker Java

Web application development with Docker and Glassfish

Although Docker is often used to deploy/run applications in a defined environment, you can also use Docker in your development environment. To make this productive, you need an easy way to deploy and debug your web application. In the Glassfish image (see https://github.com/koert/docker-glassfish) I can add deploy a web application and hook into the file system.

Glassfish provides an autodeploy directory – just copy a war or ear file to this directory and Glassfish will deploy this automatically. You can mount this directory to your host with the -v option:

docker run ... -v ~/tmp/glassfish/deploy:/opt/glassfish4/glassfish/domains/domain1/autodeploy ...

To view the logs directory you can add this:

docker run ... -v ~/tmp/glassfish/logs:/opt/glassfish4/glassfish/domains/domain1/logs ...

I cloned a copy of the Spring petclinic application (https://github.com/koert/spring-petclinic) to setup my development environment. Curiously, I needed to add “implements Serializable” to BaseEntity, to make it work on Glassfish 4.1. Normally, the petclinic application creates its own datasource, not a JEE datasource. I changed the configuration to use the Glassfish datasource.

The Glassfish server now needs a datasource configuration, including the database driver implementation for hsqldb. By adding the jar to /opt/app/extlib, the driver is copied to the Glassfish lib directory – this makes it available for datasources.

The configure-glassfish.sh (in /opt/app/bin) script creates the necessary datasource:

asadmin create-jdbc-connection-pool --restype=javax.sql.DataSource --datasourceclassname=org.hsqldb.jdbc.JDBCDataSource \
 --property "user=sa:password=sa:url=jdbc\:hsqldb\:hsql\:mem\:petclinic" \
 PetClinicPool
asadmin create-jdbc-resource --connectionpoolid PetClinicPool jdbc/petclinic

In the Dockerfile I add the hsqldb jar and the configure script:

ADD hsqldb-2.3.2.jar /opt/app/extlib/hsqldb-2.3.2.jar
ADD configure-glassfish.sh /opt/app/bin/configure-glassfish.sh

After building the Docker image, you can run Glassfish with the necessary configuration:

docker run --rm -it -v ~/tmp/glassfish/deploy:/opt/glassfish4/glassfish/domains/domain1/autodeploy \
 -v ~/tmp/glassfish/logs:/opt/glassfish4/glassfish/domains/domain1/logs \
 -p 4848:4848 -p 8080:8080 -p 9009:9009 \
 -e DEBUG="true" koert/glassfish-showcase

The “–rm” option instructs Docker not to keep the state of the started container, so you can start the image fresh every time. The “-it” option keeps the container running in the foreground – you can stop it with Ctrl-C.

While this is running, you can deploy your web application by copying its war or ear file to the ~/tmp/glassfish/deploy directory. You can debug the application by letting your IDE connect to port 9009.

This way you have a consist development environment with a well defined state.

Categories
Docker Java

Packaging a webapp with Docker and Glassfish

As I wrote in a previous article, you can run Glassfish in a Docker container.

The purpose of Docker is to package everything into a container so that you can run your application anywhere in a consistent state. To achieve this with a web application and Glassfish, you package the Java virtual machine (JDK 8), Glassfish 4.1 and your web application together.

I created an example of using my base Glassfish image with the Primefaces showcase war file:

FROM koert/glassfish-4.1
MAINTAINER Koert Zeilstra <koert.zeilstra@zencode.nl>

RUN wget http://repository.primefaces.org/org/primefaces/showcase/5.2/showcase-5.2.war -O /opt/app/deploy/showcase.war

This will download the war file and put it into the /opt/app/deploy directory – the startup script will copy this file into the Glassfish autodeploy directory. When Glassfish starts up, it will automatically deploy the war and you can access the applciation after starting with:

docker run -d -p 8080:8080 koert/glassfish-showcase

On my laptop it starts up in about 12 seconds, you can look at the application in your browser: http://localhost:8080/showcase

Look at my example on Github: https://github.com/koert/docker-glassfish-showcase

Categories
Docker

Running Glassfish Application Server in a Docker container

glassfish-logoDocker (http://docker.io) is getting a lot of attention lately – it provides a container (sort of lightweight virtual machine) that makes a clean separation from the rest of the operating system. In this container you can run any application and because you can tightly control the contents of the container, you can create a predictable and repeatable environment for your application.

Although the trend seems to go to standalone Java applications, you can also run a Java (JEE) application server in a docker container. Since I work a lot with the Glassfish application server, I gave it a try in a Docker container by “dockerizing” Glassfish 4.1 into a Docker image.

I created a base image with Ubuntu 14.04 with Oracle JDK 8 and used this to create an image with Glassfish 4.1. I took an existing Docker image definition from Github (https://github.com/bonelli/tutum-docker-glassfish) and made some modifications to make it easier to configure the application server. My resulting Docker source is on Github: https://github.com/koert/docker-glassfish

After building the image, you can run it:

docker run -d -p 4848:4848 -p 8080:8080 -e GLASSFISH_PASS="mypass" koert/glassfish-4.1

This will set the admin password to “mypass” and make the 4848 and 8080 port accessable on the host. You just open https://localhost:4848 in your browser and you can deploy an application. I deployed the Primefaces showcase (http://repository.primefaces.org/org/primefaces/showcase/5.2/showcase-5.2.war) and it runs great.

The nice thing thing about the Docker container is that you can stop the container and start a fresh one within seconds. No matter how you mess up the Glassfish configuration, you can throw it way (or restart it) quickly.

Categories
Software development

Beautiful code

Kent Beck and Ward Cunningham have been the pioneers of the agile software movement, which is becoming mainstream. In their talk for the hacksummit they reflect on their life-long career as programmers and how they still enjoy software development. It seems to me that the key is the joy of constant learning and digging into new ideas. This probably applies to many other areas of life.

Look at at their great session: http://youtu.be/ShOMGASbcJ0

Important points:

  • Design Patterns
  • Refactoring
  • Know your tools
  • Write beautiful code (“it does what you expect”)

 

 

Categories
Java

Devoxx 2014

Chet Haase
Chet Haase

Devoxx had a lot of interesting sessions this year. It is great to see what the movers and shakers of the IT industry are doing and where the innovation is going.

Categories
Uncategorized

JFall 2014

Komende week, woensdag 5 november 2014, organiseert de NLJUG JFall – een conferentie voor laatste ontwikkelingen in de Java wereld.

Hot topics:

  • Docker
  • Internet of things
  • Microservices
  • Single Page Applications – AngularJS

http://www.nljug.org/jfall/schedule/2014