16.10.2015 Views

Getting Started with WebSphere Application Server

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

1. Define local and remote interfaces for the EJB<br />

2. Implement the EJB class<br />

5.2.1.1 Defining local and remote interfaces<br />

Chapter 5 – Enterprise Java Beans 91<br />

To avoid code duplication, define a common interface which can then be inherited by both,<br />

local and remote interfaces. In an EJB, a local interface is used by an invoker using the<br />

same Java Virtual Machine (JVM) while a remote interface is used by an invoker in<br />

a different JVM. Following the EJB 3.0 specification, the local interface is annotated <strong>with</strong><br />

@Local while the remote interface is annotated <strong>with</strong> @Remote. Listing 5.1 below provides<br />

an example of how you can define the business interface of an EJB. In the example, you<br />

define a business interface named HelloInterface.<br />

package hello;<br />

public interface HelloInterface {<br />

public void sayHello();<br />

}<br />

Listing 5.1 - Defining the business interface of the EJB<br />

Listing 5.2 provides an example of how you can define the local interface of the EJB. It<br />

inherits the business interface.<br />

package hello;<br />

import javax.ejb.Local;<br />

@Local<br />

public interface HelloInterfaceLocal extends HelloInterface {}<br />

Listing 5.2 - Defining local interface of the EJB<br />

Listing 5.3 provides an example of how you can define the remote interface of the EJB.<br />

package hello;<br />

import javax.ejb.Remote;<br />

@Remote<br />

public interface HelloInterfaceRemote extends HelloInterface{}<br />

Listing 5.3 - Defining the remote interface of the EJB<br />

5.2.1.2 Implementing the EJB class<br />

Now that the interfaces have been defined, you can implement them. In this step, all the<br />

methods of the EJB class will be implemented. As defined in the EJB 3.0 specification,<br />

@Stateless is used for a stateless EJB, an EJB that is normally short lived and does not<br />

maintain the state on behalf of the client. @Stateful is used for a stateful EJB. Listing 5.4<br />

provides an example.<br />

package hello;<br />

import javax.ejb.Stateless;<br />

@Stateless<br />

public class HelloEJB implements HelloInterfaceLocal,<br />

HelloInterfaceRemote {

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

Saved successfully!

Ooh no, something went wrong!