Montag, 17. Februar 2014

CDI EJB-Remote Context for wildfly

This article shows how ejb remote clients can be configured and connected in a more dynamic way.

Within wildfly (current version 8.0.0.Final) there are several possibilities to connect ejb remote clients. Descriptions can be found on:

The Problem

You have an application for two (or more) tenants which are identical but are deployed on different machines. Customers should be able to switch between those tenants without restarting the application. Additionally the code itself should not depend on tenant specific code.
As far as i know this would be necessary with the standard approach ( given above.

A Solution

After some consideration i have identified the following five components which are needed to implement this feature.
  • Component that creates WildFly ejb remote connections
  • Component that produces the JNDI-Context
  • Component that holds the selected tenant-key for the client
  • Component that maps the tenant-keys to connection properties
  • CDI-Modul Qualifier

Putting them together leads to the following UML:

To keep the example as simple as possible i have decided to use the ejb-remote-quickstart example and extend it with the jndicontext module. The current implementation uses a simple property file which holds the following data.

The Fazit

The resulting service locator just has to use the JndiSession (in this example the RemoteEJBClient) which technically holds the tenant key and can be set from the GUI.
public class RemoteEJBClient {
    @Inject @JndiContextAnno
    Instance<Context> contextInstance;

    JndiSession jndiSession;
    private RemoteCalculator lookupRemoteStatelessCalculator() throws NamingException {
        Context context = contextInstance.get();

public class CDIBootStrapper {
    public static void main(String[] args) throws Exception
        WeldContainer weld = new Weld().initialize();
        RemoteEJBClient client = weld.instance().select(RemoteEJBClient.class).get();


So the code looks very clean and in comparison to way more functional.
The sources can be found on
Have fun coding... ;)