Entity Bean Home methods (ejbHome)
Resin 3.0

Features
Installation
Configuration
Web Applications
IOC/AOP
Resources
JSP
Quercus
Servlets and Filters
Databases
Admin (JMX)
CMP
EJB
Amber
EJB 3.0
Security
XML and XSLT
XTP
JMS
Performance
Protocols
Third-party
Troubleshooting/FAQ

Tutorials
Burlap Clients
Hessian Clients
CORBA/IIOP Server
Scrapbook

Stateless Session Hello
Entity Bean Home methods
Local Session Counter
Message Bean
Stateless Session Hello
Tutorials
Local Session Counter

The EJB 2.0 spec adds home methods to entity beans. Applications can define methods callable from the home interface. In other words, there's no need to create or find an entity bean instance just to execute an entity method. The home methods resemble static Java methods or methods in a stateless session bean.

  1. EJB Implementation
  2. Servlet Implementation
  3. Burlap protocol for the example
    1. hello() call
    2. add(2, 3) call
    3. metadata call

EJB Implementation

This example adds a hello() method and an add method to the home interface. In this example, there are no entity bean instances. All the work is done by the home interface. The home methods are implemented in the bean by adding ejbHome to the name. So hello will become ejbHomeHello.

In essence, the home interface lets you create a simple service, like the Burlap hello service, and stick to the EJB API.

Even though there are no entity bean instances, the findByPrimaryKey method is still needed.

Home.java
package test;

import java.rmi.*;

public interface Home extends javax.ejb.EJBHome {
  public String hello() throws RemoteException;

  public int add(int a, int b) throws RemoteException;

  public HomeObj findByPrimaryKey(String a)
    throws RemoteException, FinderException;
}

The object interface is empty. Since the EJB spec requires it, we still need to define HelloObj.

HelloObj.java
package test;

public interface HelloObj extends javax.ejb.EJBObject {
}

As usual, the work is done the HelloBean. The home methods all start with ejbHome. ejbHomeHello implements the hello method. ejbHome/add implements the add method.

ejbFindByPrimaryKey is required, but it's easy to create a stub implementation that just returns an exception.

HelloBean.java
package test;

import javax.ejb.*;

public class HelloBean extends com.caucho.ejb.AbstractEntityBean {
  public String ejbHomeHello()
  {
    return "Hello, world";
  }

  public int ejbHomeAdd(int a, int b)
  {
    return a + b;
  }

  public String ejbFindByPrimaryKey(String key)
    throws FinderException
  {
    throw new FinderException("no children");
  }
}

Servlet Implementation

The client in this example is a servlet. As with other EJBs, the client gets the home interface using JNDI. Since it's only necessary to do the JNDI lookup once, the servlet caches the home object as a servlet variable.

package test.entity.home;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

import javax.ejb.*;
import javax.naming.*;

public class HomeServlet extends GenericServlet {
  Home home;

  public void init()
    throws ServletException
  {
    try {
      Context env = (Context) new InitialContext().lookup("java:comp/env");
      home = (Home) env.lookup("ejb/home");
    } catch (Exception e) {
      throw new ServletException(e);
    }
  }

  public void service(ServletRequest req, ServletResponse res)
    throws IOException, ServletException
  {
    PrintWriter pw = res.getWriter();
    
    try {
      pw.println("message: " + home.hello() + "
"); pw.println("1 + 3 = " + home.add(1, 3) + "
"); pw.println("7 + 1 = " + home.add(7, 1) + "
"); } catch (Exception e) { throw new ServletException(e); } } }

Burlap protocol for the example

The Burlap calls for home methods are particularly simple. Since there's no object, the client can call the home method directly. The protocol is identical to the Burlap hello example, so clients can work with either unchanged.

Since the example uses the burlap protocol, you can use 'telnet' as a simple client.

hello() call

unix> telnet localhost 8080
Trying 127.0.0.1...
Connected to localhost (127.0.0.1).
Escape character is '^]'.
POST /servlet/test.entity.home.HomeServlet HTTP/1.0
Content-Length: 54

<burlap:call>
<method>hello</method>
</burlap:call>

HTTP/1.0 200 OK
Server: Resin/1.3.s010312
Content-Length: 73
Date: Tue, 13 Mar 2001 03:15:27 GMT

<burlap:reply>
<value>
<string>Hello, world</string>
</value>
</burlap:reply>

The results have some added whitespace to make the example more readable. The server doesn't bother trying to pretty-print the results.

add(2, 3) call

The add call has two integer arguments. The integers directly follow the method.

The example below uses the short method name, add. To support method overloading, the Burlap server also accepts the method name add_int_int.

unix> telnet localhost 8080
Trying 127.0.0.1...
Connected to localhost (127.0.0.1).
Escape character is '^]'.
POST /ejb/home HTTP/1.0
Content-Length: 77

<burlap:call>
<method>add</method>
<int>2</int>
<int>3</int>
</burlap:call>

HTTP/1.0 200 OK
Server: Resin/1.3.s010404
Content-Length: 56
Date: Thu, 05 Apr 2001 05:36:42 GMT

<burlap:reply>
<value>
<int>5</int>
</value>
</burlap:reply>

metadata call

The Java client, in this case HomeServlet, needs to know the Home interface in order to generate stub for the Burlap protocol. Since the client doesn't know that information, it needs to ask the server for the classname. Burlap handles that with a reserved metadata call _burlap_getAttribute.

unix> telnet localhost 8080
Trying 127.0.0.1...
Connected to localhost (127.0.0.1).
Escape character is '^]'.
POST /ejb/home HTTP/1.0
Content-Length: 95

<burlap:call>
<method>_burlap_getAttribute</method>
<string>home-class</string>
</burlap:call>

HTTP/1.0 200 OK
Server: Resin/1.3.s010404
Content-Length: 82
Date: Thu, 05 Apr 2001 05:39:23 GMT

<burlap:reply>
<value>
<string>test.entity.home.Home</string>
</value>
</burlap:reply>


Stateless Session Hello
Tutorials
Local Session Counter
Copyright © 1998-2006 Caucho Technology, Inc. All rights reserved.
Resin® is a registered trademark, and HardCoretm and Quercustm are trademarks of Caucho Technology, Inc.