Inserting Java code to create an EJB instance

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

The inserted Java code looks up the remote or local home interface of an EJB reference and calls the "create" method to create 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 create 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 create method

Before snippet insertion:

public class Test {

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

After snippet insertion, with code added to do the lookup and create of the bean:

import com.ibm.etools.service.locator.ServiceLocatorManager;
import java.rmi.RemoteException;
import sample.RegistrationFacadeHome;
import sample.RegistrationFacadeRemote;
import javax.ejb.CreateException;
public class Test {

    private final static String STATIC_RegistrationFacadeHome_REF_NAME = "ejb/RegistrationFacade";
    private final static Class STATIC_RegistrationFacadeHome_CLASS = RegistrationFacadeHome.class;
    public void createMyBean() {
        // insert snippet here
        RegistrationFacadeRemote aRegistrationFacadeRemote = createRegistrationFacadeRemote();
    }
    protected RegistrationFacadeRemote createRegistrationFacadeRemote() {
        RegistrationFacadeHome aRegistrationFacadeHome = (RegistrationFacadeHome) ServiceLocatorManager
                .getRemoteHome(STATIC_RegistrationFacadeHome_REF_NAME,
                        STATIC_RegistrationFacadeHome_CLASS);
        try {
            if (aRegistrationFacadeHome != null)
                return aRegistrationFacadeHome.create();
        } catch (CreateException ce) {
            // TODO Auto-generated catch block
            ce.printStackTrace();
        } catch (RemoteException re) {
            // TODO Auto-generated catch block
            re.printStackTrace();
        }
        return null;
    }
}

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

  1. In the J2EE perspective, open in the editor the Java file where you want to create 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 "create" method. The Insert EJB Create wizard opens.

  4. Select the EJB reference for the bean that you want to create, and click Next. 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. 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.

  6. Click Next.

  7. Select the create method that you want to call.

  8. Click Next.

  9. 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.

  10. Click Finish. The Java code to lookup the home interface and create 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 find 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