October 21, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Developing Session EJBs with Borland JBuilder Enterprise

  • March 20, 2006
  • By Vlad Kofman
  • Send Email »
  • More Articles »

After the wizard completed, I used JBuilder's EJB Designer to create the Stateless Session Bean WeatherBean. The EJB Designer is a visual tool that opens automatically after the EJB wizard completes or by clicking on the EJB module in the project pane. The designer visually adds any type of EJB to the project based on J2EE EJB specification. It even can import database schema and create an entity EJB from it. In this case, I chose to create session bean, entered its name, and added the business logic method getWeatherData(String zip) to the interface. The Bean's interface's and concrete class's source were created automatically.

The only remaining task was to add the actual implementation of the business method to the concrete bean object.

Note: Because it is a stateless session bean, its home interface only has a single create() method.

The added code is in bold.

public class WeatherBean
      implements SessionBean {
   SessionContext sessionContext;
   public void ejbCreate() throws CreateException {
   }

   public void ejbRemove() {
   }

   public void ejbActivate() {
   }

   public void ejbPassivate() {
   }

   public void setSessionContext(SessionContext sessionContext) {
      this.sessionContext = sessionContext;
   }

   public String getWeatherData(String zip) throws RemoteException {
      return "Tonight: Partly cloudy skies early will give way to
      cloudy skies late. Low 38F. Winds SSE at 5 to 10 mph.
      Tomorrow: Showers early becoming less numerous later in the
      day. High 51F. Winds SSE at 10 to 15 mph. Chance of rain 40%.
      Tomorrow night: Cloudy skies. Low 46F. Winds S at 5 to 10 mph.";
  }
}

Another feature that is worth mentioning is the EJB module's DD (data definition) Editor and the Structure Pane. They work in tandem and give a high-level overview of all the beans grouped in the EJB module. EJB's properties, such as container transactions, security, method permissions, and the like, and general options relating to the target application container can be viewed and changed with the DD Editor. Under the hood, they modify the XML deployment descriptor files ejb-jar.xml and other app-server specific files.

During the application deployment, one of configuration files in the EJB module tells the application server the name of the EJB that needs to be registered with the JNDI. This name will be used to look up the Bean's interface and create the object. Because the project's target is WebLogic, the file is weblogic-ejb-jar.xml. The lookup name can be edited manually.

<weblogic-ejb-jar>
   <weblogic-enterprise-bean>
      <ejb-name>WeatherBean</ejb-name>
      <enable-call-by-reference>True</enable-call-by-reference>
      <jndi-name>ejb/WeatherBean</jndi-name>
   </weblogic-enterprise-bean>
</weblogic-ejb-jar>

Here is the view of WebLogic's JNDI directory with the Weather bean listed after deployment.

To locate and tie EJB with the Servlet, I added its definition to the servlet class.

private static WeatherHome weatherHome = null;
private Weather weatherObj             = null;
private Context mContext               = null;

The lookup code to locate the bean via its Home interface went in to the init() method of the servlet.

Note: The name must match the one from weblogic-ejb-jar.xml.
public void init() throws ServletException {
   try {
   mContext    = new InitialContext(System.getProperties());
   weatherHome = (WeatherHome) mContext.lookup("ejb/WeatherBean");
   }
   catch (NamingException ex) {
   }
}

The create method call went to the doGet() method of the Servlet. This way, the lookup of the bean and initiation is done once, in the init(), but on each new request an object is acquired from the pool maintained by the EJB container, and reused.

In doGet() {
   String weatherData = null;
   String zip         = request.getParameter("zip");
   ....
   weatherObj  = weatherHome.create();
   weatherData = weatherObj.getWeatherData(zip);
   ...
}




Page 3 of 4



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel