Professional Documents
Culture Documents
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.
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.
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(); } } }
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
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.
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.
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(); } } }
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
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.
@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.
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.
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; }
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(); } } }
} catch(Exception e) { e.printStackTrace(); } } }
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.
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)