Oracle Blog

Cheng Fang's blog

Wednesday Apr 18, 2012

In this post, I will write about how to invoke EJB between 2 distinct
GlassFish instances, that is, the 2 GlassFish servers are not in the
same cluster. The key to accomplishing this is to correctly specify the
physical JNDI name of the target remote EJB, which I will explain in
more details with code samples. The following table illustrates the
configuration of both the client machine and server machine:[Read More]

Friday May 16, 2008

EJB 3.1 aims to further simplify EJB programming model, introducing a series of new features like no-interface local view, .war file packaging, singleton bean, cron-like schedule-based timer, asynchronous invocation, etc. Some of them have already been implemented by GlassFish V3. Ken's blog has more details on many proposed new features in EJB 3.1. Mahesh from GlassFish EJB container team gave a preview of EJB 3.1 container in GlassFish v3 TP2. In this post, I will share a riddles webapp to showcase the use of certain EJB 3.1 features and how they interact with web-tier components like Servlet, JSP, and JSTL.

The stateless bean class RiddleBean is annotated with @Stateless but implements no interfaces. All public methods (only getRiddle method in this case) in the bean class and its superclasses are exposed as business methods. @PostConstruct method is invoked after the bean instance is created and before any business method invocation. It initializes riddle data in classpath with class.getResourceAsStream().

RiddleServlet acts as the client of RiddleBean EJB, a reference of which is injected into RiddleServlet. With no-interface local view, the client references EJB with its bean class type. But the client must still obtain the reference either through injection or lookup, not new operator.

The servlet does not create any response; it merely sets the injected bean reference into application scope so that it is available for JSP pages. The purpose is to demonstrate the use of @EJB injection. Alternatively, this servlet can be replaced with a context listener or filter, or totally removed by declaring <ejb-ref> in web.xml.

riddle.jsp shows how easy it is to invoke EJB methods with JSP expression language (EL) , without using any java code. The expression ${riddleBean.riddle} has the same effect as calling riddleBean.getRiddle(). The return vaue, a value object of type Riddle, is stored as riddle in page scope.

In your project, EJB source files reside in the same place as servlet and other web component classes. With .war packaging, EJB classes can be packaged under WEB-INF/classes or inside a jar file under WEB-INF/lib, just like any other .war classes. It's no different than an ordinary .war, with no EJB interfaces (not even local business interface), no ejb-jar.xml, no ejb-jar, and no EAR. This is the content of the war file after building the project:

As for the riddle data file riddles.txt, you can package it inside the .war as WEB-INF/classes/riddles.txt. Or you can jar it up as riddles.jar and copy riddles.jar to $GLASSFISH_HOME/domains/domain1/lib. Any jars in this directory will be made available to all apps in domain1. In GlassFish V2, I used to directly copy classes and resources to domain1/lib/classes without the wrapper jar, but this doesn't seem to be supported yet. riddles.txt must follow certain format to be parsed properly (A: Q: %):

FORTUNE PROVIDES QUESTIONS FOR THE GREAT ANSWERS:A: To be or not to be.Q: What is the square root of 4b\^2?%

In my Ubuntu machine, I just use /usr/share/games/fortunes/riddles file. riddles.war with sample riddles can be downloaded here.

Tuesday Mar 25, 2008

J2EE and JavaEE API Chinese javadocs has been one of the most requested feature. Now it's available, according to Glassfish Aquarium_zh.

You can go directly to http://doc.java.sun.com/DocWeb/, select JavaEE 5 SDK from the list at upper-left, select Chinese (Simplified) at upper-right. JavaEE API classes are then listed on the left. You can browser alphabetically, or find by class name (e.g., EJBContext, ServletContext).

If you only see English method signatures, just click the little orange Show All button at upper right to display all Chinese docs that are collapsed by default.