0
votes

I have a Java application deployed on Websphere 8.5 as an EAR. I am trying to figure out how do a JNDI lookup from ejb-jar.xml. (Noob to EJB) I typically do JNDI entries lookup from the WAR by adding the entries in web.xml. Now working on a ejb module I figured out I can still do JNDI lookup at ejb bean[1]. See below my ejb-jar.xml:

<?xml version="1.0" encoding="UTF-8"?><ejb-jar id="ejb-jar_ID" version="2.1"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/ejb-jar_2_1.xsd">
<display-name>MyService-ejb</display-name>
<enterprise-beans>
    <message-driven id="MyServiceSyncOutboundCom">
        <ejb-name>MyServiceSyncOutboundcom</ejb-name>
        <ejb-class>
            com.company.MyService.sync.com.MyServiceSyncOutboundCom
        </ejb-class>
        <transaction-type>Container</transaction-type>
        <message-destination-type>
            javax.jms.Queue
        </message-destination-type>
        <env-entry>
             <env-entry-name>minBalance</env-entry-name>
             <env-entry-type>java.lang.Integer</env-entry-type>
             <env-entry-value>500</env-entry-value>
        </env-entry>
        <env-entry>
        <description></description>
             <env-entry-name>maxCreditBalance</env-entry-name>
             <env-entry-type>java.lang.Integer</env-entry-type>
             <env-entry-value>10000</env-entry-value>
        </env-entry>
    </message-driven>
</enterprise-beans>


My question: How do I get these entries into my class now that I have no web.xml ? Also if the ejb module comprises of a JAR dependency say myservice-config.jar with a Configs class, can I retrieve these entries in that class instead of my Message Driven Bean? How does that change the ejb-jar.xml file then?
[1] https://docs.oracle.com/cd/E16439_01/doc.1013/e13981/servjndi008.htm

1

1 Answers

0
votes

The ejb-jar.xml file serves the same purpose for EJBs as the web.xml file does for servlets. The XML syntax for injecting into an EJB is the same as the XML syntax for injecting into a servlet, except in ejb-jar.xml it is nested in an enterprise-bean, rather than just at the module level. From your example, it would be something like this:

<env-entry>
    <env-entry-name>minBalance</env-entry-name>
    <env-entry-type>java.lang.Integer</env-entry-type>
    <env-entry-value>500</env-entry-value>
    <injection-target>
        <injection-target-class>com.company.MyService.sync.com.MyServiceSyncOutboundCom</injection-target-class>
        <injection-target-name>name of field or method</injection-target-name>
    </injection-target>
</env-entry>

You can only inject into classes that will be created by a container; so for example, servlets are created by the Web Container; EJB instances and interceptors are created by the EJB Container.

Your Configs class does not appear to be a class that is managed by the container, so cannot be the target of injection. I can think of a couple of options to still inject into it:

1 - In your ejb-jar.xml, just declare it to be a singleton bean; then inject your env-entry values into it. You could then inject the Configs singleton bean into other beans. Although perhaps non-standard, ejb-jar.xml can declare any class on the application classpath to be an EJB.

2 - Declare Confg to be a CDI managed bean and use the @Inject annotation. CDI does not have anything like web.xml or ejb-jar.xml, so you would have to use annotations when using CDI.