问题
At the moment I'm working on an application that has to use a connection factory. When I lookup the connection factory directly on a global level by the name set in WAS everything is working fine, but for means of decoupling I want to define a resource reference in my application and lookup that name. So I created following entry in my application.xml:
<resource-ref>
<res-ref-name>jms/connectionFactory</res-ref-name>
<res-type>javax.jms.ConnectionFactory</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
What I do then inside my EJB is making following lookup:
ConnectionFactory connectionFactory =
(ConnectionFactory) ic.lookup("java:comp/env/jms/connectionFactory");
This leads to this exception:
javax.naming.NameNotFoundException: Name comp/env/jms not found in context "java:".
I also tried it with:
ConnectionFactory connectionFactory =
(ConnectionFactory) ic.lookup("java:app/jms/connectionFactory");
leading to:
javax.naming.NameNotFoundException: Context: BPMDev/applications/com.ibm.ws.AppNameSpaces/MAN_POT/root,
name: jms/connectionFactory: First component in name jms/connectionFactory not found.
Does anyone know what I'm doing wrong here? Thanks in advance!
回答1:
Further investigations brought me to: Lookup jndi resources WebSphere 8.5, Spring 3.1.3, Hibernate 4.1.10 EJB 3
There the answer is contained:
Both the entry in the application.xml and the lookup have to be preceeded with java:app/env. For my case this is:
<resource-ref>
<res-ref-name>java:app/env/jms/connectionFactory</res-ref-name>
<res-type>javax.jms.ConnectionFactory</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
and:
ConnectionFactory connectionFactory =
(ConnectionFactory) ic.lookup("java:app/env/jms/connectionFactory");
回答2:
java:comp resource references are scoped to the component, so the resource reference needs to be defined for the same component from which you are trying to look it up. Note that java:app is scoped to the whole application, so if you switched to java:app and that worked, it's likely that java:comp didn't work because the resource reference was defined for the wrong component.
来源:https://stackoverflow.com/questions/44325513/unable-to-lookup-resource-on-component-level-in-websphere