Author: Reza Rahman article source: www.theserverside.com
In the ' the ' I article, I urged you to provide feedback directly to the JCP at jsr-318-comments@jcp.org as as a At Rrahman@tripodtech.net. Before going farther, I would like to thank everyone who took the time to send in comments! I hope you would continue to send into your thoughts as I write more articles into this series. I am also very grateful for all of the encouraging comments on the series itself. It ' s time for Timer Service Features
Scheduling is a important part of many applications for tasks such as, database generation, maintenance OLAP summaries or data synchronization. If you are have used the EJB Timer Service in its current form, you are know that it is useful but pretty. The biggest limitations of the current EJB Timer Service are, it is isn't all so flexible and scheduled jobs can only Be created programmatically, not declaratively. Some of weaknesses were outlined by Richard Monson-haefel in the EJB 2.x time-frame. This theserverside article outlines Richard ' s views:http://www.theserverside.com/tt/articles/article.tss?l= Monsonhaefel-column4.
Let ' s take a super-quick look at the Timer Service as supported in EJB 3.0. This is the example from EJB 3 in Action:
@Stateless public class Placebidbean implements Placebid {@Resource timerservice timerservice. public void Addbid (Bid Bid ) { ... Code to add the bid goes ... timerservice.createtimer (15*60*1000, 15*60*1000, Bid); @Timeout public void Monitorbid (timer timer) {Bid Bid = (Bid) timer.getinfo (); Code to monitor the bid goes ...} }
The stateless session bean above creates a timer-is-triggered every fifteen minutes, starting with a fifteen minute D Elay when a bid was created in the Addbid method. Every time the trigger fires, the Monitorbid method annotated with the @Timeout annotation was invoked to if the bidder Was outbid.
While this functionality was fine for what Placebidbean does, imagine a slightly different scenario–a Beginning-of-the-mont H Newsletter mailing for all Actionbazaar customers. Implementing this in terms of millisecond intervals through programmatic the current Timerservice interface would to be a haz ARD at best. You'll also have to write some pretty awkward code so this timer is created when the application up. There are several existing mechanisms in place today to achieve this kind of flexible declarative schedules in Java EE. Can use a popular Open Source scheduler like Quartz, can be a commercial tool like Flux or your can use scheduling Services specific to your application server such as the ones a for available or Sybase WebLogic. The problem with this solutions is this they tend to are pretty cumbersome if all are really need is a declarative Ent of UNIX cron in Java EE. All of these solutions are also vendor-specific. Enter the Timer Service enhancemeNTS in EJB 3.1.
The most important one in this set of enhancements are the ability to declaratively create cron-like schedules to trigger E JB methods (There are more advanced features, feel free to check them out of the spec draft comes out). For example, all your would have to be annotate an EJB method with the @Schedule annotation to implement the Beginning-o F-the-month Actionbazaar newsletter like so:
@Stateless public class Newslettergeneratorbean implements Newslettergenerator {@Schedule (second= "0", minute= "0", hour = "0", dayofmonth= "1", month= "*", year= "*") public void Generatemonthlynewsletter () {... Code to generate the "Monthly News Letter Goes ...} The following table describes the attributes of the @Schedule annotation as "as" as default values:
Attribute |
allowable Values |
Default |
Second |
[0,59] |
0 |
Minute |
[0,59] |
0 |
Hour |
[0,23] |
0 |
DayOfMonth |
[1,31] |
* |
Month |
[1,12] or {"Before", "Feb", "Mar", "APR", "may", "June", "may", "Aug", "Sep", "Oct", "Nov", Dec "} |
* |
DayOfWeek |
[0,7] or {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"} |
* |
Year |
A Four-digit calendar year |
* |
Note any of the attributes support the Cron-style "*" wildcard to represent all values, a comma separated list (such as "J An, Feb, Mar ' for the month attribute ' or a dash-separated range (such as "Mon-fri" for the ' Day of week attribute). Should the expression syntax support the "/" operator as OK? How about supporting fully expanded abbreviations such as "January" instead of "the"? Also, should a compact, full cron-expression format is supported as. Our little example could is expressed as:
@Schedule (expression= "0 0 0 1 * * *")
Some folks argue that this ' pure cron style expression ' is way too ' cryptic, while others to point out that a lot of develo Pers are so used to it, it should is supported in EJB. The New methods were added to the Timerservice interface to support the programmatic version of Cron-like. The programmatic version supports defining the activation and deactivation for a dates given. For example, we newsletter could become active at a predetermined time in the future instead of being active as soon as T He timer is created. Should similar support is added to the @Schedule annotation? How about supporting defining a finite number of occurrences a cron-based trigger would fire? Can You do you have any other features? stripped down EJB packaging
Making XML deployment descriptors optional in EJB 3.0 has significantly simplified packaging and deployment of Java EE app Lications. However, Java EE packaging is still clearly oriented towards strict. Namely, you must the create separate jar files for Web and EJB modules. In a typical Java EE deployment scenario, an EAR file would contain a war archive and a separate EJB jar. Figure 1 depicts the current Java EE packaging scheme. Roughly, the idea are that the EJB jar represents "modularized" business services that are consumed by the "client" Web MoD Ule. While modularization are very important, the problem is this it is overkill to simple Web applications where business serv Ices are unlikely to is shared across clients in multiple and other Java EE modules.
Figure 1:current Java EE packaging.
Simplified EJB packaging for Web applications was aimed at addressing this issue. In the new scheme, there are no need to create a separate EJB jar module. Rather, EJBs (especially in the form of annotated POJOs) can is directly dropped into the web-inf/classes directory and de Ployed as part of the WAR. In a similar vein, the Ejb-jar.xml deployment descriptor, if you happen to is using one, can is placed into the Web-inf di Rectory along with the Web.xml file. It may also being possible to place a EJB jar into the Web-inf/lib directory (does you are important?). The new packaging scheme is depicted in Figure 2.
Figure 2:simplified EJB packaging for Web applications.
For me, a very interesting implication of it that's simplified packaging scheme makes EJBs much more agnostic of The rigidly defined structure of Java EE EAR files. There is another really nice side-effect for those of us so still live in the land of occasional XML configuration and J NDI look-ups instead of 100% annotations and DI. All EJBS references, resource references or environment entries defined anywhere in the WAR can now is shared. This is because the entire WAR file has only one local component environment (bound to the JNDI java:comp/env namespace). Let ' s say your define a data source reference in the Web.xml file like so:
<resource-ref> <description>my Data source</description> <res-ref-name>jdbc/mydb</ Res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>container</res-auth > </resource-ref> You can now does a lookup like the following is container components like Servlets b UT also inside EJBs packaged inside the WAR:
//Looking up my data source. DataSource ds = (DataSource) envctx.lookup ("Java:comp/env/jdbc/mydb"); What are your thoughts on this? There is one more reason I really like the simplified packaging enhancement-it and goes with EJB Hand-in-hand. EJB Lite is a very minimal sub-set the EJB features designed for use in stripped-down. I ' ll talk more about EJB Lite in a later article in the series. One interesting possibility is that many vendors would likely start implementing EJB Lite on top of Servlet containers Tomcat or Jetty, with EJBs directly deployable to WAR files, completely by-passing Java EE ears. I have the JBoss as Express, GlassFish Express or TOMCAT+OPENEJB as possibilities that are difficult to ignore, especially give N Java EE 6 Profiles. What Do you have these possibilities? more to Come
Believe it or not, the features discussed in the "the" and second parts of this series are still just the tip of the IC Eberg. Here are some of the more interesting ones I ' ll cover in this SERIES:EJB support into minimal in the form of EJB Lite. This would was similar to what are already available in the ' Open Source World ' of the form of Embedded JBoss, OPENEJB and Eas Ybeans plugging in the top of Tomcat. Support for asynchronous session Bean invocation. Support for stateful Web services via stateful session Bean Web service endpoints. The standardization of JNDI mapping instead of keeping it for vendors to decide is being preliminarily.
Something else I am intending to discuss in the series is using EJB through Web Beans. As you might already know, Web Beans is a very powerful the framework that integration it makes to use possible some very Resting DI features with EJBs, an area Java EE has been criticized by a number of folks. The Web Beans specification is being led by Gavin King and incorporates ideas from JBoss Seam and Google Guice ("Crazy" Bo b Lee of Guice fame is working on Webbeans too). Until then, wish the Expert Group luck and keep the feedback rolling! References JSR 318:enterprise JavaBeans 3.1, http://jcp.org/en/jsr/detail?id=318. JSR 299:web Beans, http://jcp.org/en/jsr/detail?id=299.
As per Linux conventions, either 0 or 7 can is used to represent Sunday.