Home

 

Adding a remote interface

For testing using JUnit and for some Web applications, we also want to have a remote interface for the EJBBankBean session bean:

In the RAD75EJB project, itso.bank.service package, create an interface named EJBBankRemote, that extends the business interface, EJBBankService.

Add one method to the interface, getCustomersAll, to retrieve all the customers.

Add a @Remote annotation.

Example | 4-19 shows the remote interface.

Example 14-19 Remote interface of the session bean

package itso.bank.service;
import itso.bank.entities.Customer;
import javax.ejb.Remote;
@Remote
public interface EJBBankRemote extends EJBBankService {
	public Customer[] getCustomersAll();
}

Open the EJBBankBean session bean:

Add the EJBBankRemote interface to the implements list.
Implement the getCustomersAll method similar to the getCustomers method, using the getCustomers named query (without a parameter):

public class EJBBankBean implements EJBBankService, EJBBankRemote {

......

......

public Customer[] getCustomersAll() {

System.out.println("getCustomers: all");

Query query = null;

try {

query = entityMgr.createNamedQuery("getCustomers");

List<Customer> beanlist = query.getResultList();

Customer[] array = new Customer[beanlist.size()];

return beanlist.toArray(array);

} catch (Exception e) {

System.out.println("Exception: " + e.getMessage());

return null;

}

}

}

ibm.com/redbooks