WebSocket and CDI integration.. again..

In one of my older blog posts, I had written about what does not work¬†as far as WebSocket and CDI integration within the Java EE Platform ūüėȬ†This one talks about what’s possible with CDI in terms of

  • Dependency Injection
  • Interceptors

Before you dig in, here is a summary

Feature Supported in EJB Annotated WebSocket endpoint ? Supported in Plain WebSocket endpoint ?
Inject CDI managed beans yes yes
Use CDI interceptors yes yes

DI support

It is possible to inject CDI managed beans in WebSocket endpoints. All injection targets are supported i.e. field, constructor, method


Interceptor support

You can use interceptors to implement cross-cutting concerns in for the business methods in your WebSocket endpoints

Good to know

Now that you have an idea about how WebSockets work with CDI in terms of Dependency injection and Interceptors, here are some other points

  • DI and interceptors are supported for both server and client endpoints running within a JavaEE container
  • DI and interceptors are supported for both annotated and programatic endpoints running within a JavaEE container
  • container managed injection features are not available to WebSocket endpoints which override the container implemented initialization (using the ServerEndpointConfig.Configurator)

Additional reading



Posted in Java, Java EE | Tagged , , , , , , , | 3 Comments

Integrating Oracle Application Container & Database Cloud using ‘Service Bindings’


Don’t forget to check out my new blog post on the Oracle Cloud Developer solutions community portal ! It demonstrates usage of Oracle Application Container Cloud and Database Cloud service. To be precise, it covers the following

  • An introduction to Service bindings (in Application Container Cloud) including setup + configuration and leveraging them to integrate with Oracle Database Cloud service
  • Developing a sample (Java SE based) application using JPA (Eclipselink implementation) for persistence and JAX-RS (Jersey framework) to expose a REST API
  • Build, package and deploy the solution to the Application Container cloud using its REST APIs


Posted in Java, Java EE, Oracle Application Container Cloud, Oracle Cloud, Oracle PaaS | Tagged , , , , , , , | Leave a comment

Configuring WebSocket server endpoints

WebSocket Server endpoint configuration

Before we dive into the details, here is a quick snapshot of the related interfaces

Class/Interface Description
ServerEndpointConfig A derivative of EndpointConfig interface which is specific for configuration related to server side WebSocket endpoints
ServerEndpointConfig.Configurator An interface whose custom implementation allows sharing of global (for all endpoints) available logic/state as well opportunity to intercept the WebSocket handshake process (via method override)
ServerEndpointConfig.Builder Used only for programmatic server endpoints to build a ServerEndpointConfig instance

From here on, we’ll explore the¬†configuration strategies for annotated and programmatic server endpoints

Configuring annotated server endpoints

Annotated server endpoints are configured implicitly via the elements of the @ServerEndpoint annotation. The WebSocket container picks up the value from the annotation elements and creates an instance of EndpointConfig behind the scenes. This instance is automatically injected (at run time by the WebSocket container) as a parameter of the @OnOpen method

Configuring programmatic server endpoints

Programmatic endpoints need (explicit) coding as far as configuration is concerned. This is because of the fact that programmatic endpoints are deployed differently and need an instance of ServerEndpointConfig

Please look at this blog post for details of the deployment aspects for programmatic WebSocket endpoints

Here is where the fluent builder ServerEndpointConfig.Builder comes into picture. Let’s look at an example which deomnstrates it’s usage

The big picture

Annotated and programmatic endpoint configuration are handled differently, but the end result is the same. Below is a table which shows the mapping b/w corresponding element of the @ServerEndpoint annotation, the corresponding method in ServerEndpointConfig as well as appropriate the method in the ServerEndpointConfig.Builder

@ServerEndpoint annotation element ServerEndpointConfig method ServerEndpointConfig.Builder method
value getPath() create(Class<?> endpointClass, String path)
configurator getConfigurator() configurator(ServerEndpointConfig.Configurator serverEndpointConfigurator)
decoders getDecoders() decoders(List<Class<? extends Decoder>> decoders)
encoders getEncoders() encoders(List<Class<? extends Encoder>> encoders)
subprotocols getSubprotocols() subprotocols(List subprotocols)

Also check out…

.. the new eBook – Java WebSocket API Handbook


Posted in Java, Java EE | Tagged , , , , | Leave a comment

Difference b/w Cache Writer and Cache entry listener

In one of my previous blogs, I gave an overview of Cache Loader+Writer as well as Cache entry listeners. The difference b/w a Cache Loader and Cache create/update entry listener is obvious and their roles are clearly differentiated

  1. a loader pulls the value from an external source
  2. populates the cache and
  3. the respective listener is then invoked

There is no question/doubt on whether one can be substituted for the other


Cache Writer and the Cache¬†create/update entry listener are similar (at least at first glance) –¬†both are invoked when an entry is created/updated in a specific cache

So the obvious Qs are

  • why should you use one over the other?
  • can we not use cache entry listener to integrate/sync up an external source when the a cache entry is updated/created ?

The important difference is

If the Cache Writer throws an exception, then the entry does not get stored in the cache (think of this as a rollback of sorts). But, in the case of an entry listener, an exception does not effect the value stored in the cache i.e. the created/updated cache entry remains untouched


Posted in Java, Java EE | Tagged , , , , , , | 2 Comments

Simple sample: Dockerized Java app using a maven plugin

It’s good to be able to build Docker images directly from artifacts (JAR, WAR etc.) produced by Maven builds.¬†One useful tool is the¬†Spotify Docker Maven plugin¬†which can help with this

This example (available on Github)

  • uses Jersey Grizzly connector to build a simple REST app
  • packages this as a (Uber) JAR using Maven
  • the maven build process pushes this as a Docker image (thanks to the plugin)

The pom.xml itself incorporates the Docker related configuration which is generally encapsulated in a Dockerfile (this mode is supported by the plugin as well). Here is a snippet..


Hope this gives you a simple yet useful starting point



Posted in Docker, Java | Tagged , , , , , | Leave a comment

Deploying WebSocket annotated & programmatic server endpoints together

One of my previous blog post discussed Programmatic WebSocket endpoints and their deployment methodology using the javax.server.ServerApplicationConfig class.

When annotated and programmatic endpoints co-exist…

you have to make use of the a custom implementation of ServerApplicationConfig and explicitly return set of all annotated endpoints from the getAnnotatedEndpointClasses method

Please note that: using this method, you can choose to restrict the endpoints (annotated and programmatic) being deployed i.e. not all the detected endpoints need to be (will be) deployed

This is how it works..

If the WebSocket container finds subclass of ServerApplicationConfig, it uses the same to deploy both programmatic and annotated endpoints. The result of the WAR scanning process (by the Servlet container) is passed on the methods of this class

  • For getEndpointConfigs, the set (java.util.Set) of detected programmatic endpoints are passed as the method parameter
  • For getAnnotatedEndpointClasses method, the set (java.util.Set) of detected annotated endpoints are passed as the method parameter

Summary of deployment scenarios

Annotated Endpoint Programmatic Endpoint Behavior
Yes No Automatic detection of annotated endpoint. No custom code needed
No Yes Custom implementation of ServerApplicationConfig is compulsory. Programmatic endpoints are not auto-detected and deployed
Yes Yes Custom implementation of ServerApplicationConfig is compulsory. Need to explicitly return set of all annotated endpoints from the `getAnnotatedEndpointClasses` method

Also check out …

… a new eBook – Java WebSocket API Handbook


Posted in Java EE | Tagged , , , | Leave a comment

Using JCache event listeners + loaders & writers

This blog will

  • zoom through JCache events, loaders and writers
  • look at scenarios when they are used together

You can download the sample maven project from this Github repo. It consists of

  • simple Listener, read through, write through examples
  • along with some test cases to demonstrate the point

Reacting to cache events and external store integration

JCache defines cache event listeners and Cache Loader/Writer features

Event listeners

  • react to cache events – create, update, delete, expiry
  • triggered as a result of many Cache actions (methods on javax.cache.Cache) e.g. get, put, remove etc.


  • Provide transparent way to integrate the cache with external store
  • Ability to implement this in form of Cache Loader (read from external source if key is not present in cache) and Cache Writer (update the external store in case of new entry, update or delete)
  • Caches which implement this are also known as Read Through and/or¬†Write Through

When they are used together …

Let’s look at the sequence of events when everything works in tandem


An example where a key (which did not previously exist) is looked up from the cache



An example where, a new key-value pair is inserted in the cache



Things work similarly in case of updated or removed cache entries

Its worth noting that

  • Event listeners are Synchronous:¬†i.e. they are invoked¬†in same¬†thread as the caller (e.g. the one invoking the get operation on the cache). Thus, that value can’t be returned to caller while event listener is still executing
  • Exceptions during cache entry listener invocation has no impact on the cache itself since it (the cache) has already undergone changes (create/update/delete)
  • One should not invoke cache operations from event listener callbacks,¬†loaders or writers implementations – check spec page 133 (#10)

Additional reading




Posted in Java EE | Leave a comment

CI/CD using Oracle Developer Cloud service

Here is another blog which¬†I published on the Oracle Cloud Developer Solutions portal¬†‚ÄstContinuous Deployment to App Container Cloud using Developer Cloud service¬†.¬†If you’re interested in exploring how Oracle Cloud solutions can help you seamlessly manage¬†your application (a lightweight fat JAR Java EE app in this case) lifecycle from within your IDE (NetBeans in this case), you should peek into this


Here is a quick overview

  • Leveraging NetBeans IDE while working with Oracle Developer Cloud service. This includes
    • Setting up the Team Server plugin
    • Leveraging it to interact with Developer Cloud service instance from within your IDE
  • Priming Developer Cloud service for Continuous Deployment


Posted in Cloud, Java, Java EE, Oracle Application Container Cloud, Oracle Cloud | Tagged , , | Leave a comment

Simple sample: Oracle NoSQL with Docker Compose

Here is an example of using the Dockerized Oracle NoSQL image. This, along with many other Dockerized Oracle products are available here


Key points

  • Demonstrates usage of Docker Compose¬†:¬†co-ordinates an Oracle NoSQL container and a dependent container running its (Java) client
  • Docker Containers
    • Oracle NoSQL ‘kvlite‘ instance (simple enough for demonstration)
    • A Jersey + Grizzly based Fat JAR application which acts as a client

Jersey App Image

  • Built from latest openjdk image
  • exposes a REST interface to put keys into Oracle NoSQL instance and retrieve the key (return the inserted time stamp)

Oracle NoSQL Image

  • The Docker Hub image is directly referenced in docker-compose configuration
  • Shows this as an example along with the build image pattern demonstrated by the Jersey app

Its very simple to setup – instructions available here. All you need to do is make sure that you have Docker installed ūüôā


Posted in Docker, Java, Java SE | Tagged , , , , , , | Leave a comment

Java EE microservices with Oracle Application Container Cloud and Wildfly Swarm



Just wanted to drop a note regarding one of the blogs I recently published on the Oracle Cloud Developer Solutions portal¬†–¬†Building ‚Äėright sized‚Äô Java EE applications with Oracle Application Container Cloud and Wildfly Swarm¬†.¬†You should check it out ! ūüôā

Here is an overview …

  • Uses Java EE 7 APIs ‚Äď JAX-RS, WebSocket, EJB, CDI, JSON-P
  • Covers aspects of developing lightweight Java EE microservices¬†for the Oracle Application Container Cloud
  • Uses Wildfly Swarm to select ‚Äėonly the required‚Äô APIs and package them as Uber/Fat JAR
  • It also has a attached ZIP file with the code which you can download and experiment with

Enjoy… Cheers!

Posted in Java, Java EE | Tagged , , , , , , | Leave a comment