Inserting Java code to call a session bean method
Use the Snippets view to insert Java code that calls a particular method on a session bean.
The inserted Java code calls the create method on the session bean, then invokes the method that you specify in the wizard.
Example snippet insertion for calling an session bean service method
Before snippet insertion:
public class Test { public void callSessionMethod() { // insert snippet here } }After snippet insertion, with the getPrimaryKey method selected on the RegistrationFacade session bean:
import java.rmi.RemoteException; import com.ibm.etools.service.locator.ServiceLocatorManager; 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 callSessionMethod() { // insert snippet here RegistrationFacadeRemote aRegistrationFacadeRemote = createRegistrationFacadeRemote(); try { Object anObject = aRegistrationFacadeRemote.getPrimaryKey(); } catch (RemoteException ex) { // TODO Auto-generated catch block ex.printStackTrace(); } } 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 calling a session bean service method:
- In the J2EE perspective, open in the Java editor the Java file where you want to add the code snippet.
- Place your cursor in the point of the Java file where you want to insert the code.
- In the Snippets view, expand the EJB drawer and double-click Call a Session bean service method. The Insert Session Bean Service wizard opens.
- Select the EJB reference for the session bean that you want to call. If you have not defined an EJB reference for the session bean, add one before you can complete this wizard. Click New EJB Reference to open the Add EJB Reference wizard.
- Click Next
- If the client and 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.
- Click Next
- Select the method in the session bean that you want to call.
- Click Finish.
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 find an EJB instance
Inserting Java code to send a message to a JMS queue listener
Defining references in J2EE modules