Monday, January 16, 2017

EJB - JNDI Bindings

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 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.tutorialspoint.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.tutorialspoint.stateless.LibrarySessionBeanRemote - EJB3.x Remote Business Interface
...

Customized binding

Following annotations can be used to customized 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.tutorialspoint.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.tutorialspoint.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.tutorialspoint.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.tutorialspoint.stateless.LibrarySessionBeanLocal - EJB3.x Local Business Interface
...
Repeat the above steps for Remote and check the result.

No comments:

Post a Comment