You are on page 1of 12

GlassFish Project - Simple Enterprise JavaBeansTM (EJB) 3.

0 Stateless Session Bean Example


New to GlassFish | Community Guidelines | Downloads | FAQ | How-Tos

Overview Business Interface Bean Class Deployment Descriptor Application Client Standalone Java Client SUN App Server Specific Deployment Configuration Running the example

Overview
This is a very simple EJB 3.0 Stateless Session Bean with a Remote business interface. Click here to get the ZIP file with the complete example. Contact us at ejb@glassfish.dev.java.net with any comments or questions.

Business Interface
The Stateless Session bean has a Remote business interface with a single business method.

package ejb30; import javax.ejb.Remote; @Remote public interface Sless { public String hello(); } Note that unlike prior versions of EJB, the Remote interface is not required to extend java.rmi.Remote and its business methods are not required to throw java.rmi.RemoteException. The business interface is designated as a remote business interface via the @javax.ejb.Remote annotation.

Stateless Session Bean Class


Here's the bean implementation : package ejb30; import javax.ejb.Stateless; @Stateless public class SlessBean implements Sless {

public String hello() { return "hello, world!\n"; } } @javax.ejb.Stateless is a component-defining annotation that designates this class as the bean class for a Stateless Session Bean.

Deployment Descriptor
The good news is the deployment descriptor is no longer required!!! The two java files above are sufficient to completely describe this stateless session bean.

Application Client
Here's a Java EE Application Client that accesses the Stateless Session Bean using the @EJB annotation and dependency injection : package ejb30; import javax.ejb.EJB; public class SlessAppClient { @EJB private static Sless sless; public static void main(String args[]) { System.out.println("Sless bean says : " + sless.hello()); } } The @EJB annotation tells the container that this component has a dependency on a business interface of an EJB. At initialization time, the Application Client container will inject the variable "sless" with a reference to a Stateless Session bean. There's no JNDI lookup or casting required. Note that the Java EE 5 specification requires that annotations within Application Client classes be static since the entry point for the application is the static main() method.

Standalone Java Client


Here's an example of a plain Java client that runs outside of a Java EE container. In this case, it does a global JNDI lookup since dependency injection is not available outside of a Java EE component. package ejb30; import javax.naming.InitialContext; public class SlessJavaClient { public static void main(String args[]) {

try { InitialContext ic = new InitialContext(); Sless sless = (Sless) ic.lookup("ejb30.Sless"); System.out.println("Sless bean says : " + sless.hello()); } catch(Exception e) { e.printStackTrace(); } } }

SUN App Server Specific Deployment Configuration


There is no need to define any SUN App Server-specific deployment descrpitors (e.g. sun-ejb-jar.xml, sun-applicationclient.xml) for this example. The JNDI name for the Remote Stateless Session bean will default to the fully-qualified class name of its Remote business interface : ejb30.Sless. The Application Client's Remote @EJB dependency will be resolved to the same value since its business interface type is also ejb30.Sless.

Running the example


Use the following steps to build and execute the test. 1. 2. 3. 4. 5. Set the glassfish.home property within build.xml. Set JAVA_HOME to point to a JDK 1.5 (or later) installation. ant build ant deploy ant run

This will execute the Application Client and produce the following output : Sless bean says : hello, world!

Other Targets

ant runjavaclient (run stand-alone java client) ant undeploy (undeploy stateless session bean) ant clean (remove build directory)

GlassFish Project - Simple Enterprise JavaBeansTM (EJB) 3.0 Stateful Session Bean Example

New to GlassFish | Community Guidelines | Downloads | FAQ | How-Tos

Overview Business Interface Bean Class Deployment Descriptor Application Client Standalone Java Client SUN App Server Specific Deployment Configuration Running the example

Overview
This is a very simple EJB 3.0 Stateful Session Bean with a Remote business interface. Click here to get the ZIP file with the complete example. Contact us at ejb@glassfish.dev.java.net with any comments or questions.

Business Interface
The Stateful Session bean has a Remote business interface with two business methods : one for setting an ID, and one for retrieving it. package ejb30; import javax.ejb.Remote; @Remote public interface Sful { public void setId(String id); public String getId(); } Note that unlike prior versions of EJB, the Remote interface is not required to extend java.rmi.Remote and its business methods are not required to throw java.rmi.RemoteException. The business interface is designated as a remote business interface via the @javax.ejb.Remote annotation. Note that there is no Home interface. The Home interface is not required in EJB 3.0 simplified API.

Stateful Session Bean Class


Here's the bean implementation :

package ejb30; import javax.ejb.Stateful; @Stateful

public class SfulBean implements Sful { private String myId = "unknown"; public void setId(String id) { myId = id; } public String getId() { return myId; } } @javax.ejb.Stateful is a component-defining annotation that designates this class as the bean class for a Stateful Session Bean. Note that all callback methods are optional in EJB 3.0, so it is not necessary to define an ejbCreate method.

Deployment Descriptor
The good news is the deployment descriptor is no longer required!!! The two java files above are sufficient to completely describe this stateful session bean.

Application Client
Here's a Java EE Application Client that accesses the Stateful Session Bean using the @EJB annotation and dependency injection :

package ejb30; import javax.ejb.EJB; public class SfulAppClient { @EJB private static Sful sful; public static void main(String args[]) { sful.setId("duke"); System.out.println("Sful id = " + sful.getId()); } } The @EJB annotation tells the container that this component has a dependency on a business interface of an EJB. At initialization time, the Application Client container will inject the variable "sful" with a reference to a new Stateful Session bean. There's no JNDI lookup or casting required. Note that the Java EE 5 specification requires that annotations within Application Client classes be static since the entry point for the application is the static main() method.

Standalone Java Client

Here's an example of a plain Java client that runs outside of a Java EE container. In this case, it does a global JNDI lookup since dependency injection is not available outside of a Java EE component.

package ejb30; import javax.naming.InitialContext; public class SfulJavaClient { public static void main(String args[]) { try { InitialContext ic = new InitialContext(); Sful sful = (Sful) ic.lookup("ejb30.Sful"); sful.setId("duke"); System.out.println("Sful id = " + sful.getId()); } catch(Exception e) { e.printStackTrace(); } } }

SUN App Server Specific Deployment Configuration


There is no need to define any SUN App Server-specific deployment descrpitors (e.g. sun-ejb-jar.xml, sun-applicationclient.xml) for this example. The JNDI name for the Remote Stateful Session bean will default to the fully-qualified class name of its Remote business interface : ejb30.Sful. The Application Client's Remote @EJB dependency will be resolved to the same value since its business interface type is also ejb30.Sful.

Running the example


Use the following steps to build and execute the test. 1. 2. 3. 4. 5. Set the glassfish.home property within build.xml. Set JAVA_HOME to point to a JDK 1.5 (or later) installation. ant build ant deploy ant run

This will execute the Application Client and produce the following output : Sful id = duke

Other Targets

ant runjavaclient (run stand-alone java client) ant undeploy (undeploy stateless session bean)

ant clean

(remove build directory)

GlassFish Project - Simple Enterprise JavaBeansTM (EJB) 3.0 MDB Example


New to GlassFish | Community Guidelines | Downloads | FAQ | How-Tos

Overview Bean Class Deployment Descriptor Application Client SUN App Server Specific Deployment Configuration Running the example

Overview
This is a very simple EJB 3.0 Message Driven Bean that consumes JMS Messages. Click here to get the ZIP file with the complete example. Contact us at ejb@glassfish.dev.java.net with any comments or questions.

Message Driven Bean Class


Here's the bean implementation : package ejb30; import javax.ejb.MessageDriven; @MessageDriven(mappedName="MDBQueue") public class MDB implements MessageListener { public void onMessage(Message msg) { System.out.println("Got message!"); } }

@javax.ejb.MessageDriven is a component-defining annotation that designates this class as a bean class for a Message Driven Bean. The message listener type(javax.jms.MessageListener) is derived from the implements clause.

Deployment Descriptor
The good news is the deployment descriptor is no longer required!!! The one java file above is sufficient to completely describe this message driven bean.

Application Client

Here's a Java EE Application Client that send a JMS message to the message driven bean. The Application Client uses dependency injection to get a Connection Factory and a Queue.

package ejb30; import javax.annotation.Resource; import javax.jms.*; public class MDBAppClient { @Resource(mappedName="MDBQueueConnectionFactory") private static QueueConnectionFactory queueCF; @Resource(mappedName="MDBQueue") private static Queue mdbQueue; public static void main(String args[]) { try { QueueConnection queueCon = queueCF.createQueueConnection(); QueueSession queueSession = queueCon.createQueueSession (false, Session.AUTO_ACKNOWLEDGE); QueueSender queueSender = queueSession.createSender(null); TextMessage msg = queueSession.createTextMessage("hello"); queueSender.send(mdbQueue, msg); System.out.println("Sent message to MDB"); queueCon.close(); } catch(Exception e) { e.printStackTrace(); } } }

Note that the Java EE 5 specification requires that annotations within Application Client classes be static since the entry point for the application is the static main() method.

SUN App Server Specific Deployment Configuration


There is no need to define any SUN App Server-specific deployment descrpitors (e.g. sun-ejb-jar.xml, sun-applicationclient.xml) for this example. The JNDI name of the Queue from which the message driven bean should consume is specified using the @MessageDriven mappedName() attribute. Likewise, the Application Client's @Resource dependencies are also resolved to JNDI names using the mappedName() attribute.

Running the example


Use the following steps to build and execute the test. 1. 2. 3. 4. 5. Set the glassfish.home property within build.xml. Set JAVA_HOME to point to a JDK 1.5 (or later) installation. ant build ant deploy ant run

This will execute the Application Client and produce the following output : Sent message to MDB

Other Targets

ant all ant undeploy ant clean (build, deploy, run, and undeploy the example) (undeploy message driven bean) (remove build directory)

GlassFish Project - Enterprise JavaBeansTM (EJB) 3.0 Adapted Stateful Session Bean Example
New to GlassFish | Community Guidelines | Downloads | FAQ | How-Tos

Overview EJB 2.x Home/Remote interfaces Bean Class Deployment Descriptor Application Client Standalone Java Client SUN App Server Specific Deployment Configuration Running the example

Overview
This is an example of a bean class implemented using the simplified EJB 3.0 API that exposes an Adapted EJB 2.x Home interface. Click here to get the ZIP file with the complete example. Contact us at ejb@glassfish.dev.java.net with any comments or questions.

EJB 2.x Home/Remote interfaces

In this case we want to expose the bean to clients written to the EJB 2.x remote programming model. For that, we need a Home and Remote interface. public interface AdaptedHome extends EJBHome { public AdaptedRemote create(String id) throws CreateException, RemoteException; } public interface AdaptedRemote extends EJBObject { public String getId() throws RemoteException; }

Stateful Session Bean Class


Here we have a bean class that uses the simplified EJB 3.0 API and annotations but exposes an EJB 2.x Remote view. @Stateful @RemoteHome(AdaptedHome.class) public class AdaptedBean { private String myId = "unknown"; @Init public void create(String id) { myId = id; } public String getId() { return myId; } } An EJB 2.x style Remote view is added via the @RemoteHome annotation. ( @LocalHome would be used for an EJB 2.x Local view. ) In addition, each create method in the 2.x Home interface must have a corresponding implementation in the bean class, although the method-name does not have to match. Each of these methods is tagged with the @Init annotation. Note that the EJB 2.x Remote component interface counterpart to AdaptedHome (AdaptedRemote) does not appear in the implements clause of the AdaptedBean class. In fact, it does not have to be declared explicitly in the bean class at all because it is derived from the signature of the create method declared in AdaptedHome.

Deployment Descriptor
No deployment descriptor is needed.

Application Client
Here's a Java EE Application Client that accesses the Adapted Stateful Session Bean using the @EJB annotation and dependency injection. @EJB can handle the 2.x Home/LocalHome view in addition to EJB 3.0 business interfaces.

public class AdaptedAppClient { @EJB private static AdaptedHome adaptedHome; public static void main(String args[]) { try { AdaptedRemote adapted = adaptedHome.create("duke"); System.out.println("Adapted id = " + adapted.getId()); } catch(Exception e) { e.printStackTrace(); } } }

Standalone Java Client


Here's an example of a plain Java client that runs outside of a Java EE container. public class AdaptedJavaClient { public static void main(String args[]) { try { InitialContext ic = new InitialContext(); Object o = ic.lookup(AdaptedHome.class.getName()); AdaptedHome adaptedHome = (AdaptedHome) PortableRemoteObject.narrow(o, AdaptedHome.class); AdaptedRemote adapted = adaptedHome.create("duke"); System.out.println("Adapted id = " + adapted.getId());

} catch(Exception e) { e.printStackTrace(); } } }

SUN App Server Specific Deployment Configuration


There is no need to define any SUN App Server-specific deployment descrpitors (e.g. sun-ejb-jar.xml, sun-applicationclient.xml) for this example. The JNDI name for the Adapted Stateful Session bean will default to the fully-qualified class name

of its Remote Home interface : ejb30.AdaptedHome. The Application Client's Remote Home @EJB dependency will be resolved to the same value since its type is also ejb30.AdaptedHome.

Running the example


Use the following steps to build and execute the test.

1.
2. 3. 4. 5.

Set the glassfish.home property within build.xml. Set JAVA_HOME to point to a JDK 1.5 (or later) installation. ant build ant deploy ant run

This will execute the Application Client and produce the following output : Adapted id = duke

Other Targets

ant runjavaclient (run stand-alone java client) ant undeploy (undeploy ejb application from the server) ant clean (remove build directory)

You might also like