EJB - JNDI Bindings


Advertisements

JNDI stands for Java Naming and Directory Interface. It is a set of API and service interfaces. Java based applications use JNDI for naming and directory services. In context of EJB, there are two terms.

  • Binding − This refers to assigning a name to an EJB object, which can be used later.

  • Lookup − This refers to looking up and getting an object of EJB.

In Jboss, session beans are bound in JNDI in the following format by default.

  • local − EJB-name/local

  • remote − EJB-name/remote

In case, EJB are bundled with <application-name>.ear file, then default format is as following −

  • local − application-name/ejb-name/local

  • remote − application-name/ejb-name/remote

Example of Default Binding

Refer to EJB - Create Application chapter's JBoss console output.

JBoss Application Server Log Output

...
16:30:02,723 INFO  [SessionSpecContainer] Starting jboss.j2ee:jar=EjbComponent.jar,name=LibrarySessionBean,service=EJB3
16:30:02,723 INFO  [EJBContainer] STARTED EJB: com.howcodex.stateless.LibrarySessionBean ejbName: LibrarySessionBean
16:30:02,731 INFO  [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:

   LibrarySessionBean/remote - EJB3.x Default Remote Business Interface
   LibrarySessionBean/remote-com.howcodex.stateless.LibrarySessionBeanRemote - EJB3.x Remote Business Interface
...

Customized Binding

Following annotations can be used to customize the default JNDI bindings −

  • local − org.jboss.ejb3.LocalBinding

  • remote − org.jboss.ejb3.RemoteBindings

Update LibrarySessionBean.java. Refer to EJB - Create Application chapter.

LibrarySessionBean

package com.howcodex.stateless;
 
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
 
@Stateless
@LocalBinding(jndiBinding="tutorialsPoint/librarySession")
public class LibrarySessionBean implements LibrarySessionBeanLocal {
    
    List<String> bookShelf;    
    
    public LibrarySessionBean() {
       bookShelf = new ArrayList<String>();
    }
    
    public void addBook(String bookName) {
       bookShelf.add(bookName);
    }    
 
    public List<String> getBooks() {
        return bookShelf;
    }
}

LibrarySessionBeanLocal

package com.howcodex.stateless;
 
import java.util.List;
import javax.ejb.Local;
 
@Local
public interface LibrarySessionBeanLocal {
 
    void addBook(String bookName);
 
    List getBooks();
    
}

Build the project, deploy the application on Jboss, and verify the following output in Jboss console −

...
16:30:02,723 INFO  [SessionSpecContainer] Starting jboss.j2ee:jar=EjbComponent.jar,name=LibrarySessionBean,service=EJB3
16:30:02,723 INFO  [EJBContainer] STARTED EJB: com.howcodex.stateless.LibrarySessionBean ejbName: LibrarySessionBean
16:30:02,731 INFO  [JndiSessionRegistrarBase] Binding the following Entries in Global JNDI:

   tutorialsPoint/librarySession - EJB3.x Default Local Business Interface
   tutorialsPoint/librarySession-com.howcodex.stateless.LibrarySessionBeanLocal - EJB3.x Local Business Interface
...
Advertisements