Remote EJB lookup in Websphere (and Weblogic / JBoss)

I have an EJB class that is used to search,

@Local(ILuceneEmployeeSearchManagerLocal.class)
@Remote(ILuceneEmployeeSearchManagerRemote.class)
public class LuceneEmployeeSearchManager implements ILuceneEmployeeSearchManagerLocal, ILuceneEmployeeSearchManagerRemote{
....
}

      

There is another class in the WAR project in the same EAR that refers to it,

public class EmployeeAccessor {

    private ILuceneEmployeeSearchManagerRemote searcher;

    public EmployeeMstAccessor() {

        Context ic = null;
        try {
            Properties props = new Properties();
            props.put(Context.INITIAL_CONTEXT_FACTORY, "<WHAT_TO_PUT_HERE>");
            props.put(Context.PROVIDER_URL, "iiop://127.0.0.1:9083");
            ic = new InitialContext(props);
            this.searcher = (ILuceneEmployeeSearchManagerRemote) ic
                .lookup("<WHAT_TO_PUT_HERE?>");
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    ....

}

      

This works great when I was doing local checks using JNDI. Due to some, it is now necessary to deploy EAR across multiple APs (in both clustered and non-clustered environments). In addition, the application server used will be the same for the APs, but not necessarily for Websphere, i.e. All hotspots can use JBoss / Websphere / Weblogic.

My question is, is there an implementation independent way to find and invoke remote EJBs? Something that will run on Websphere, Weblogic or JBoss (homogeneous environments).

My second question is if the AP servers are running Websphere (in a clustered or non-clustered environment), let's say I want EmployeeAccessor on all AP servers to use the LuceneEmployeeSearchManager EJB deployed in AP01 (ip: xxxx, port: yy) like configure it? Do I need to provide the fully qualified JNDI name (including the cell name and node name for websphere)? What are the correct values ​​for INITIAL_CONTEXT_FACTORY? and what is the correct JNDI lookup syntax?

Thanks for any help :)

+3


source to share


1 answer


You have to use the default constructor InitialContext()

- then it is platform independent and use ejb links - then it is location independent - you map your links at install time. Thus, the code is independent, but the rendering is platform specific but executed outside of the application. If you are using a Java EE 6 compatible server you can also use injection@EJB.

So your code could be like this:

public EmployeeMstAccessor() {

        Context ic = null;
        try {
            ic = new InitialContext();
            this.searcher = (ILuceneEmployeeSearchManagerRemote) ic
                .lookup("java:comp/env/ejb/ILuceneEmployeeSearchManagerRemoteRef");
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

      

then you will need to create an ejb/ILuceneEmployeeSearchManagerRemoteRef

EJB reference in web.xml

your web module and map that reference to your bean's JNDI name. This can be done during application installation / configuration or, in the case of WebSphere, via a ibm-web-bnd.xml

file.

Here's an example with @EJB

(it should be in a servlet or JSF managedBean for Java EE 5, for Java EE 6 you can put it in any class, but you need to enable CDI for the application):

public class MyServlet exetends HttpServlet {
    @EJB(name="ejb/ILuceneEmployeeSearchManagerRemote")
    private ILuceneEmployeeSearchManagerRemote searcher;  // replaces lookup
...}

      



again you need to match the link ejb/ILuceneEmployeeSearchManagerRemote

which is annotated @EJB

to the actual JNDI name.

Regarding your second question (in the case of WebSphere):

  • If the bean is in cell SAME, but on another server it will be in the form:

    cell/nodes/<node-name>/servers/<server-name>/<name binding location> 
    
          

    eg:.

    cell/nodes/S47NLA1/servers/Server47A1/ejb/Department549/AccountProcessors/CheckingAccountReconciler 
    
          

  • If the bean is in an outer cell, the best way if you want to avoid providing properties (host, port, factory) in the Initial context is to use customized bindings. Remember that for cross-cell in default configuration, you will have to exchange SSL certificates for them to work.

See also:

+1


source







All Articles