3
votes

In Tomcat any jndi resources defined in context.xml have prefix java:/comp/env/. In Websphere they don't. There're quite a few resources that we are injecting with

@Resource(mappedName="...")
private Queue queue1;

and it's a pain to configure them twice for local and managed environments. I would like to configure it to have only only @Resource injection that will work for all environments. That's the point of using jndi after all.

There are ways to configure Tomcat to define a global jndi resource but they all involve changing server.xml, which we can't do since it's a gradle tomcat plugin.

Is there any way to achieve the same effect without modifying server.xml in Tomcat?

1
Note, the prefix required by the EE specs is java:comp/env/, not java:/comp/env/ as used by some application servers.Brett Kail

1 Answers

1
votes

WebSphere ignores mappedName see here, so you can have there whatever you like for Tomcat, but for WebSphere use lookup=jndiName. The java:comp/env/ prefix is used for resource references, or resource definitions embedded in the application. It shouldn't be used as JNDI name for global objects defined in server configuration.

For WebSphere you can also use binding file (ibm-web-bnd.xml in the WEB-INF), and map there your reference to the JNDI name defined in the server configuration e.g.

// In code use just resource with name
@Resource(name="myQueue")    // this defines reference name which will be bound to jndi name using binding file
private Queue queue;


// binding file
<?xml version="1.0" encoding="UTF-8"?>
<web-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-web-bnd_1_2.xsd"
    version="1.2">

    <virtual-host name="default_host" />

    <resource-env-ref name="myQueue" binding-name="jms/myQ" />
</web-bnd>