I'm migrating application from Websphere to Liberty but I am having issue with EJB lookup.
So I have one server application with some EJBs. Those EJBs have a custom mapping defined in ibm-ejb-jar-bnd.xml file
<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar-bnd
xmlns="http://websphere.ibm.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee http://websphere.ibm.com/xml/ns/javaee/ibm-ejb-jar-bnd_1_1.xsd"
version="1.1">
<session name="SampleBatchModule" simple-binding-name="ejb/sample/SampleBatchModule"/>
</ejb-jar-bnd>
When launching the server, we can see that Liberty is correctly binding it:
[INFO ] CNTR0167I: The server is binding the mySample.BatchModule interface of the SampleBatchModule enterprise bean in the mySample-0.0.1-SNAPSHOT.jar module of the sample-batch application. The binding location is: ejb/sample/SampleBatchModule
[INFO ] CNTR0167I: The server is binding the mySample.BatchModule interface of the SampleBatchModule enterprise bean in the mySample-0.0.1-SNAPSHOT.jar module of the sample-batch application. The binding location is: java:global/sample-batch/mySample-sample-batch-0.0.1-SNAPSHOT/SampleBatchModule!mySample.BatchModule
I need know to access it via a Liberty Client through IIOP. This client has main class, doing basically this:
context = new InitialContext();
Object object = context.lookup(jndiName);
The issue is that only the java:global
JNDI binding is working:
java:global/sample-batch/mySample-sample-batch-0.0.1-SNAPSHOT/SampleBatchModule!mySample.BatchModule
I still haven't found a way to use my custom binding:
ejb/sample/SampleBatchModule
I have tried numerous combination, but no luck so far.
That same code was working on Websphere (using com.ibm.websphere.naming.WsnInitialContextFactory
as InitialContextFactory)
Unfortunately, the Liberty Client only supports JNDI lookups of the specification defined java:
namespace.
WebSphere traditional supports a "distributed" namespace, where all servers in a cell have access to JNDI bindings from all other servers. Access to this "distribubed" namespace is achieved through the use of com.ibm.websphere.naming.WsnInitialContextFactory
.
Liberty does not provide a "distributed" namespace, so in general, all bindings in JNDI are only visible within the server process where they were bound. Liberty does not support the use of com.ibm.websphere.naming.WsnInitialContextFactory
. There are generally only 2 exceptions to this limitation:
1. The Java/Jakarta EE Application Client may access the java:
namespace
2. Remote EJBs may be accessed using a corbaname URL through the CORBA name service, but the corbanames are limited to a name that is very similar to the java:global
name binding.
Net is that while Liberty recognizes the legacy WebSphere custom bindings, they are only visible within the process that binds them.
While Liberty does not support accessing the legacy customized bindings remotely, it is still possible to customize the EJB bindings using the specification defined java:
namespace. For example, you could add the following at the class level on a servlet or EJB:
@EJB(name = "java:global/ejb/sample/SampleBatchModule", beanInterface = BatchModule.class, beanName = "SampleBatchModule")
This is defining an ejb-ref
, which would by default be bound in java:comp/env
, but the EJB specification allows you to change that using the name
attribute, and instead make the EJB reference available in any java:
name context.
The above example would require a change in the application code, but you could instead just add an ejb-ref
in either the web.xml
or ejb-jar.xml
which similarly sets the ejb-ref-name
element to a customized name in java:global
.
There are other variation to this, such as
@EJB(name = "java:global/ejb/sample/SampleBatchModule", beanInterface = BatchModule.class,lookup = "java:global/<app>/<module>/SampleBatchModule!mySample.BatchModule")
And, as with any other ejb-ref
, you would also have the flexibility to change which EJB it is bound to in either ibm-web-bnd.xml
or ibm-ejb-jar-bnd.xml
.
The end result is that the client application should be able to perform the following:
context = new InitialContext();
Object object = context.lookup("java:global/ejb/sample/SampleBatchModule);
Very similar to the original goal, as it does provide a customized name that does not need to be aware of the specific application name or module name, or even bean name.