|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionEJB 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. ProblemThe 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.
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 CodeThe 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 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 is the name of application/project which contains the implementation of HelloWorldBean. HelloWorldBean is the name of your EJB bean class which has the Points of InterestWhile 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.
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.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||