Inserting Java code to find an EJB instance

Use the Snippets view to insert Java code that finds an instance of an enterprise bean that you already defined and referenced.

The inserted Java code uses an EJB reference to look up the remote or local home interface of an enterprise bean and calls the "find" method to find an instance of the enterprise bean.

By using the Snippets view to quickly insert this code, you can avoid manually coding the appropriate business logic to find an instance of a bean. The Java code is generated and inserted in the current spot of the Java file that you are editing.

Example snippet insertion for calling an EJB find method

Before snippet insertion:

public class Test {

    public void findAccountsPayableByPrimaryKey() {              
        // insert snippet here
        
    }
}

After snippet insertion, with code added to do the lookup of the bean and call the findByPrimaryKey method with no argument specified:

import sample.Accountspayable;
import sample.AccountspayableKey;
import com.ibm.etools.service.locator.ServiceLocatorManager;
import java.rmi.RemoteException;
import sample.AccountspayableHome;
import javax.ejb.FinderException;
public class Test {

    private final static String STATIC_AccountspayableHome_REF_NAME = "ejb/Accountspayable";
    private final static Class STATIC_AccountspayableHome_CLASS = AccountspayableHome.class;
    public void findAccountsPayableByPrimaryKey() {
        // insert snippet here
        Accountspayable anAccountspayable = find_AccountspayableHome_findByPrimaryKey(primaryKey);
    }
    protected Accountspayable find_AccountspayableHome_findByPrimaryKey(
            AccountspayableKey primaryKey) {
        AccountspayableHome anAccountspayableHome = (AccountspayableHome) ServiceLocatorManager
                .getRemoteHome(STATIC_AccountspayableHome_REF_NAME,
                        STATIC_AccountspayableHome_CLASS);
        try {
            if (anAccountspayableHome != null)
                return anAccountspayableHome.findByPrimaryKey(primaryKey);
        } catch (FinderException fe) {
            // TODO Auto-generated catch block
            fe.printStackTrace();
        } catch (RemoteException re) {
            // TODO Auto-generated catch block
            re.printStackTrace();
        }
        return null;
    }
}

To insert Java code for finding an instance of an enterprise bean:

  1. In the J2EE perspective, open in the editor the Java file where you want to find an instance of an enterprise bean.

  2. Place your cursor in the point of the Java file where you want to insert the code.

  3. In the Snippets view, expand the EJB drawer and double-click Call an EJB "find" method. The Insert EJB Find wizard opens.

  4. Select the EJB reference for the bean that you want to find. If you have not added an EJB reference, add one first before you can complete this wizard. Click New EJB Reference to open the Add Reference wizard.

  5. Click Next.

  6. If the client and referenced enterprise bean are not in the same application server container, enter the Provider URL and Name Service Type to locate the referenced enterprise bean. Otherwise, you can select Use default context properties for doing a lookup on this reference.

  7. Click Next.

  8. Select the find method that you want to call.

  9. Click Next.

  10. If arguments are required, enter values for each parameter in the selected method. The values that you enter are inserted exactly as shown. If you want to enter a string value, make sure to include the quotation marks.

  11. Click Finish. The Java code to lookup the home interface and find an instance of the enterprise bean is added to the Java file.

Note: A serviceLocatorMgr.jar file is added as a utility JAR file to each enterprise application that the Java class you edited belongs to. This serviceLocator.jar file includes a ServiceLocatorManager class that is used within the inserted snippets of Java code. This class optimizes the lookups of the home interfaces and InitialContexts, and ensures that they are only looked up once for the entire application. Because the utility JAR file is added, a Java JAR dependency for the serviceLocator.jar file is added for the module or Java utility project that the Java file belongs to.

The ServiceLocatorManager class has a static method called setErrorHandler(ServiceLocatorErrorHandler handler) that you can use to specify a specific error handler for error conditions that occur when looking up the home interface. The default handler simply calls printStackTrace() on the exception that is handled.

 

Related tasks

Inserting Java code to create an EJB instance
Inserting Java code to send a message to a JMS queue listener
Inserting Java code to call a session bean method
Defining references in J2EE modules