16.10.2015 Views

Getting Started with WebSphere Application Server

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 5 – Enterprise Java Beans 95<br />

5.3.2 Using an EJB in an application client<br />

An application client is a stand-alone Java application invoking methods in an EJB. Like<br />

other Java EE compliant application servers, Community Edition provides an application<br />

client container where you can deploy the application client. You can create an application<br />

client following these steps:<br />

1. Import or copy the interface file of an EJB to the application client<br />

2. Find the EJB <strong>with</strong> JNDI<br />

3. Invoke the methods in the EJB interface<br />

5.3.2.1 Import or copy the interface file of an EJB to the application client<br />

A deployed server-side application client has to reference the local interface of an EJB. A<br />

stand-alone or deployed client-side application client has to reference remote interface. To<br />

reference the right interface, copy the interface file of the EJB to the application client.<br />

5.3.2.2 Find the EJB <strong>with</strong> JNDI<br />

In Community Edition the JNDI name for the local interface is by default [EJB name] +<br />

Local. For the remote interface the JNDI name is by default [EJB name] + Remote.<br />

For example, for an EJB named HelloEJB, HelloEJBLocal is the local interface JNDI<br />

name and HelloEJBRemote is the remote interface JNDI name.<br />

Before using the JNDI name to find the EJB, you need to initialize the context <strong>with</strong> some<br />

Community Edition specific properties. As mentioned earlier, Community Edition uses<br />

OpenEJB as the EJB container; therefore, the value of the properties context factory and<br />

naming provider URL has to be set. Normally, they are set to<br />

org.apache.openejb.client.RemoteInitialContextFactory and<br />

ejbd://localhost:4201. Listing 5.8 provides an example.<br />

public class HelloEJBAppClient {<br />

public static void main(String[] args) throws NamingException{<br />

Properties prop=new Properties();<br />

prop.put(Context.INITIAL_CONTEXT_FACTORY,<br />

"org.apache.openejb.client.RemoteInitialContextFactory");<br />

prop.put("java.naming.provider.url", "ejbd://localhost:4201");<br />

Context context = new InitialContext(prop);<br />

HelloInterfaceRemote hellEjb<br />

=(HelloInterfaceRemote)context.lookup("HelloEJBRemote");<br />

hellEjb.sayHello();<br />

}<br />

}<br />

Listing 5.8 - An application client invoking HelloEJB<br />

5.3.2.3 Invoke the methods in the EJB interface<br />

There are two ways to run the application client:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!