Click here to Skip to main content
Click here to Skip to main content
Go to top

EJB 3.0 Remote Session Bean Lookup

, 1 May 2008
Rate this:
Please Sign up or sign in to vote.
This article describes a way to perform remote lookup of EJB 3.0 session bean into simple Java application.

Introduction

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.

Problem

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();
            properties.put("java.naming.factory.initial",
                "org.jnp.interfaces.NamingContextFactory");
            properties.put("java.naming.factory.url.pkgs",
                "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(
                "HelloWorld/HelloWorldBean/remote");  
            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 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 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 can not cast to remote bean type

    This exception occurs when your 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.

License

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

Share

About the Author

bijulsoni
Software Developer Microsoft
United States United States
No Biography provided

Comments and Discussions

 
Generalthanks Pinmembersanthosh.226-Nov-08 22:13 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140926.1 | Last Updated 1 May 2008
Article Copyright 2008 by bijulsoni
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid