Click here to Skip to main content
15,313,813 members
Articles / Programming Languages / Java / Java SE / J2EE
Posted 1 May 2008

Tagged as


27 bookmarked

EJB 3.0 Remote Session Bean Lookup

Rate me:
Please Sign up or sign in to vote.
3.50/5 (8 votes)
1 May 2008CPOL4 min read
This article describes a way to perform remote lookup of EJB 3.0 session bean into simple Java application.


EJB 3.0 is one of the most powerful technologies for implementing distributed computing. It comes with many cool features. Some of these features are annotation-based programming, persistence APIs, adoption of hybernate as OR mapping tool, POJO objects as entities, dependency injection and lookup, IDE support like Netbeans 6.0 and many more.

Imagine a scenario when you have EJBeans hosted on an application server like JBOSS, GlassFish or Apache. Your servlets are hosted on Tomcat, your clients are simple browsers, mobile phones are sending requests to servlets, servlets are calling EJBeans, which again invokes other EJBeans, and all these components are hosted on different machines. You are using caching at a web tier in your servlets, second level caching in your EJB tier, and for that you have to setup EHCACHE or memcached servers. The purpose of this kind of architecture is to provide high scalability, availability and performance which can handle millions of users. This all looks fascinating but it is also challenging to implement.

In this article, I would like to discuss one of the problems that EJB programmers face while implementing EJB session beans remote lookup between two enterprise applications. I am assuming that the reader has knowledge about EJB 3.0 technology and how to program using it. I am using NetBeans 5.5 with a configured JBOSS application server.


The problem is that you have a session bean residing in one Enterprise Application. It has both local and remote interface. Now you want to call that session bean and its methods into another client Java program like be an enterprise application, Java program or a web project. This kind of method invocation is also known as Remote method invocation.

There are two ways to implement this:

  1. You can write lookup code using JNDI APIS.
  2. You can use @EJB annotation to get remote session bean.

I tried both of these approaches but I found that the first approach is easy to implement and widely accepted by all the EJB-supported application servers. As per my knowledge, the second approach is not supported by JBOSS. Only GlassFish supports remote lookup using @EJB annotation.

So let's discuss the first approach. We will lookup a remote EJB session bean and will invoke its methods using a simple Java client program.

Using the Code

The attached source contains a NetBeans project named HelloWorld. Inside this project, there is one simple, EJB stateless session bean HelloWorldBean. It has one business method sayHello.

The following is the implementation of this method:

// public String sayHello(String str)
    str="Hello " + str +" Today is "+ (new Date());
    return str;

In order to call this method remotely through another Java program, you have to provide a remote interface for the HelloWorld bean.

Now let me show you the lookup code to invoke this session bean. This lookup code is in another project, HelloWorldClient, which is a simple Java program consuming our HelloWorldBean.

 private HelloWorldRemote lookupNewSessionBean() {
        try {
            Context c = new InitialContext();
            Properties properties = new Properties();
                "org.jboss.naming rg.jnp.interfaces");
            properties.put("java.naming.provider.url", "jnp://localhost:1099");
            Context ctx = new InitialContext(properties);
            System.out.println("Got context");
            Session.HelloWorldRemote ans=(Session.NewSessionRemote) ctx.lookup(
            return ans;
        catch(NamingException ne) {
            throw new RuntimeException(ne);

The most important thing in the above code is the lookup string "HelloWorld/HelloWorldBean/remote". It consists of three parts:

  • HelloWorld is the name of application/project which contains the implementation of HelloWorldBean.
  • HelloWorldBean is the name of your EJB bean class which has the sayHello method.
  • remote indicates that you are looking for a remote session bean.

Points of Interest

While implementing remote lookup, programmers are likely to see the following two exceptions. Here are the descriptions of these exceptions and explanations on how to solve them. These exceptions are specific to the JBOSS application server.

  1. Name not bound Exception:

    This exception generally comes when your bean is not properly deployed and not registered into your JBOSS application server. You can confirm the deployment of your beans by the following steps:

    1. Go to http://localhost:8080
    2. Go to JMX console
    3. Click on service=JNDIView and click on Invoke button within List of MBean Operations
    4. It will open a screen which shows all the deployed beans. If you have successfully deployed your bean, then it must be under your Global JNDI namespace.
    5. Class cast exception: $proxy cannot cast to remote bean type

    This exception occurs when you haven't put your remote interface properly into your client program. In order to invoke the remote session bean, it is necessary that you put your remote interface including its package into the client program, otherwise it will give this error.

The attached source code file contains two NetBeans projects. You can open them from the Netbeans 5.5 or 6.0 IDE with configured JBOSS application server.

Thanks for reading this article. I would really appreciate your feedback.


  • 1st May, 2008: Initial version


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer Microsoft
United States United States
No Biography provided

Comments and Discussions

GeneralMy vote of 1 Pin
havekar12-Mar-12 5:04
Memberhavekar12-Mar-12 5:04 
Generaljava shine pattern Pin
OmiD_Haghighatgoo1-Jul-10 10:26
MemberOmiD_Haghighatgoo1-Jul-10 10:26 
there is a new pattern for java that's very powerful and easy to work.
i've worked it and i love it

u can download it,it's open source with a document :[^]
Generalthanks Pin
santhosh.226-Nov-08 22:13
Membersanthosh.226-Nov-08 22:13 
Generalthanks ,wish u hava more deeply article Pin
xiaoGong168825-Jun-08 16:24
MemberxiaoGong168825-Jun-08 16:24 
QuestionThanks a lot! i have been looking for this answer for a long time Pin
Nazfra20-May-08 22:15
MemberNazfra20-May-08 22:15 
AnswerRe: Thanks a lot! i have been looking for this answer for a long time Pin
bijulsoni21-May-08 12:05
Memberbijulsoni21-May-08 12:05 
QuestionRe: Thanks a lot! i have been looking for this answer for a long time Pin
Nazfra22-May-08 23:56
MemberNazfra22-May-08 23:56 
QuestionRe: Thanks a lot! i have been looking for this answer for a long time Pin
Nazfra23-May-08 2:12
MemberNazfra23-May-08 2:12 
GeneralThanks for the useful article Pin
hardikkumar1-May-08 14:45
Memberhardikkumar1-May-08 14:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.