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.

About this task

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;
	}
}

Procedure

  1. In the Java EE perspective, open in the Java editor the Java file where you want to add the code snippet and place your cursor in the point of the Java file where you want to insert the code.
  2. In the Snippets view, expand the EJB drawer and double-click Call a Session bean service method. The Insert Session Bean Service wizard opens.
  3. Select the EJB reference for the bean that you want to create, and click Next. Click New EJB Reference to add an EJB reference if you have not done added one already. You must add a reference before you can complete the wizard.
  4. Click Next
  5. If the client and enterprise bean are not in the same application server container, you must 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 method in the session bean that you want to call.
  8. Click Finish.

Results

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


Feedback