Generating EJB Home and Remote Interfaces

Problem

You need XDoclet to generate the EJB home and remote interfaces each time your bean class changes.

Solution

Mark up your bean implementation class with the necessary XDoclet tags and use XDoclet to generate the home and remote interfaces.

Discussion

Writing EJB home and remote interfaces is a cumbersome task. The remote, home, and bean code must stay in sync or the deployment of the bean fails. Depending on the server, you may or may not receive suitable error messages. Let’s look at an example of what needs to be written if XDoclet is not used.

Example 9-3 shows an example of a hand-coded remote interface. When writing remote interfaces, ensure that each method throws java.rmi.RemoteException. This may not seem like a huge task but the first time you forget to add the exception to the throws clause you will wish you never wrote this interface.

Example 9-3. Hand-coded remote interface

package com.oreilly.javaxp.xdoclet.ejbdoclet.ejb;

import javax.ejb.EJBObject;
import java.rmi.RemoteException;

public interface PaymentProcessing extends EJBObject {

   public boolean makePayment(String accountNumber, double payment) 
           throws RemoteException;
}

Example 9-4 shows an example of a hand-coded home interface. The home interface provides a view into the container for creating, finding, and removing beans. You must ensure that all “create” methods throw javax.ejb.CreateException, that “finder” methods throw javax.ejb.FinderException, and all methods ...

Get Java Extreme Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.