Search code examples
jakarta-eejbosswildflyjndi

Jboss wildfly how to bind entries in local JNDI namespaces?


I provide a library (lets call it event-source-lib) which abstracts the implementation of a datasource (in this case an eventstore) from my business services. Within the library I want to inject an eventstore datasource via a local application scoped JNDI namespace resource lookup:

@Resource(lookup="java:app/jdbc/eventstore")
Datasource eventstore;

This is because I have many separate microservices in a JEE container which are using using separate datasources. I want the container to determine which datasource to assign to java:app/eventstore for each microservice.

The naming subsystem in the JBoss wildfly standalone.xml does not seem to support local portable JNDI namespaces, it only supports :jboss, :global and :/.

How can I achieve this? Is there a way to declare global resources and map to local scoped portable JNDI namespaces? I notice that AppName is in the correct namespace, but I have no idea how to add stuff there.

I suspect that jboss-app.xml or jboss-web.xml may be the answer but I cannot find a good example. I am deploying my services as separate war files, they are NOT wrapped up in an ear.


Solution

  • Glad to say we have managed to resolve the problem. The answer was to use the web.xml to map between the local JNDI portable namespaces to the global portable namespaces used to define physical resources in the standalone.xml deployment descriptor:

    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
             version="3.1">
        <resource-ref>
            <description>DB Connection</description>
            <res-ref-name>java:app/jdbc/eventstore</res-ref-name>
            <res-type>javax.sql.DataSource</res-type>
            <lookup-name>java:global/something/DS.eventstore</lookup-name>
        </resource-ref>
    </web-app>
    

    Note the web.xml needs to be placed under src/main/webapp/WEB-INF/web.xml (if you are using maven to build your project).

    So in our case we define the resource in event-source-lib as java:app/jdbc/eventstore, then in our business service we specify a global portable namespace to lookup the resource from: java:global/something/DS.eventstore via the web.xml.

    In the server deployment descriptor naming subsystem we can then link this global JNDI namespace to the configuration of our physical datasource.