Miles to go …

October 29, 2010

TOTD #148: JPA2 Metamodel Classes in NetBeans 7.0 – Writing type-safe Criteria API

Filed under: General — arungupta @ 5:32 am

NetBeans 7.0 M2 was released recently. There are several Java EE related improvements in this release:

  • Find usages of managed beans (JSF/CDI) and their properties
  • PrimeFaces is now available as an integrated JSF component library
  • Wizard for creating Bean Validation constraint
  • CDI Qualifier creation editor hint
  • Cleaned up Inspect Observer/Producer for CDI events
  • Generation of Bean Validation annotations for Entities

and some others. One of the features that is not much spoken about is the automatic generation of JPA 2 Metamodel classes from Entity classes. This Tip Of The Day (TOTD) will explain how to generate these classes and use them for writing type-safe JPA2 Criteria queries.

The JPA2 Metamodel classes capture the metamodel of the persistent state and relationships of the managed classes of a persistence unit. This abstract persistence schema is then used to author the type-safe queries using Critieria API. The canonical metamodel classes can be generated statically using an annotation processor following the rules defined by the specification. The good thing is that no extra configuration is required to generate these metamodel classes. NetBeans IDE automatically generates the canonical metamodel classes using the EclipseLink Canonical Model Generator. There are two ways these metamodel classes are generated in NetBeans:

  1. Pre-configured when Entity Classes are generated from a Database using the wizards. TOTD #122 provide more details on that. The actual metamodel classes are generated when the project is build using "Clean and Build", "Deploy" or some other related target.
  2. Explicitly configured by right-clicking on the project, "Properties", "Libraries", "Processor", "Add Library…", and select "EclipseLink(JPA 2.0)" and "EclipseLink-ModelGen(JPA 2.0)" libraries and click on "Add Library" as shown below.

This TOTD will use the "Manufacturer" table from the pre-configured "jdbc/sample" JDBC resource in NetBeans and GlassFish. It will create a simple 2-page application where the first page (index.xhtml) accepts a Manufacturer name and the second page (show.xhtml) displays some details about that manufacturer.

  1. Create a NetBeans Web project with the title "CriteriaMetamodel", make sure to enable CDI and Java Server Faces during the creation.
  2. Create "Manufacturer" JPA entity by using the pre-configured "jdbc:derby://localhost:1527/sample" database connection and using the MANUFACTURER table. Notice that the generated manufacturer entity contains the bean validation constraints derived from the database schema, yet another new feature in 7.0 M2. More on this topic in a later blog.
  3. Generate the metamodel classes by right-clicking on the project and selecting "Clean and Build". The generated metamodel class looks like:
    package org.glassfish.samples.entities;
    import javax.annotation.Generated;
    import javax.persistence.metamodel.SingularAttribute;
    import javax.persistence.metamodel.StaticMetamodel;
    @Generated("EclipseLink-2.1.0.v20100614-r7608 @ Mon Oct 25 16:35:03 PDT 2010")
    @StaticMetamodel(Manufacturer.class)
    public class Manufacturer_ {
    public static volatile SingularAttribute addressline2;
    public static volatile SingularAttribute zip;
    public static volatile SingularAttribute phone;
    public static volatile SingularAttribute addressline1;
    public static volatile SingularAttribute fax;
    public static volatile SingularAttribute manufacturerId;
    public static volatile SingularAttribute email;
    public static volatile SingularAttribute name;
    public static volatile SingularAttribute state;
    public static volatile SingularAttribute city;
    public static volatile SingularAttribute rep;
    }
    

    This is shown as "Generated Sources" in NetBeans IDE as shown:

  4. Generate a new Java class "DatabaseBean" and mark it with "@javax.enterprise.inject.Model" annotation. This class will be the "backing bean" for the JSF pages and will have
    1. A field to accept the manufacturer’s name from "index.xhtml"
    2. A field to show information about the searched manufacturer in "show.xhtml"
    3. A business method "searchManufacturer" that searches the database for the given manufacturer’s name. This method will use the generated metamodel class and type-safe Criteria API to query the database.

    The complete source code for the class looks like:

    @PersistenceUnit EntityManagerFactory emf;
    String name;
    Manufacturer manufacturer;
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public Manufacturer getManufacturer() {
    return manufacturer;
    }
    public void searchManufacturer() {
    EntityManager em = emf.createEntityManager();
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery criteria = builder.createQuery(Manufacturer.class);
    // FROM clause
    Root root = criteria.from(Manufacturer.class);
    // SELECT clause
    criteria.select(root);
    // WHERE clause
    Predicate condition = builder.like(root.get(Manufacturer_.name),
    "%" + name + "%");
    criteria.where(condition);
    // FIRE query
    TypedQuery query = em.createQuery(criteria);
    // PRINT result
    List manufacturers = query.getResultList();
    if (manufacturers != null && manufacturers.size() > 0) {
    manufacturer = (Manufacturer)manufacturers.get(0);
    }
    }
    

    The business method returns the first manufacturer whose name contains the text entered in the textbox. No validation is performed in order to keep the business logic simple.

    Notice how "searchManufacturer" method is not using any String-based identifiers for constructing the query graph. This gives the complete type-safety for query construction and allows the errors to be detected much earlier.

  5. Edit the generated "index.xhtml" such that the content within <h:body> looks like:
    <h:form>
    <h:panelGrid columns="3">
    <h:outputText value="Name:" />
    <h:inputText value="#{databaseBean.name}" id="name"/>
    </h:panelGrid>
    <h:commandButton
    actionListener="#{databaseBean.searchManufacturer}"
    action="show"
    value="submit"/>
    </h:form>
    

    This page shows a text box and a submit button. The "searchManufacturer" method of the DatabaseBean is invoked when the "submit" button is clicked and passes the entered text in the "name"property of the DatabaseBean.

  6. Create a new XHTML page and name it "show.xhtml". Replace the generated boilerplate code with the code given below:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <title>Show Manufacturer's Detail</title>
    </head>
    <body>
    Name: #{databaseBean.manufacturer.name}<br/>
    Phone: #{databaseBean.manufacturer.phone}<br/>
    Address Line1: #{databaseBean.manufacturer.addressline1}<br/>
    Address Line2: #{databaseBean.manufacturer.addressline2}<br/>
    </body>
    </html>

Now you can deploy the application to GlassFish by usual means and access "http://localhost:8080/CriteriaMetamodel/faces/index.xhtml" which gets displayed as:

Enter some value as "S" in the text box and click on "Submit" to display the result as:

The complete source code for this sample can be downloaded here.

Now Criteria query is little verbose but it does give you the type-safety and was explicitly asked within the JPA Expert Group. It allows you to manipulate different parts of a query such as SELECT, FROM, and WHERE clauses and that too using the Java type system. This reminds me of Mr Potato Head :-)

This behavior can be achieved in JPQL but is available exclusively using String manipulation and the errors are not detected until runtime.

How are you using Criteria API ?

What use cases would you like to see solved in JPA2.next ?

Technorati: totd jpa2 criteria metamodel netbeans javaee6 glassfish

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot

October 28, 2010

TOTD #147: Java Server Faces 2.0 Composite Components using NetBeans – DRY your code

Filed under: General — arungupta @ 5:03 am

The Java Server Faces 2.0 uses Facelets instead of JSP as the view declaration language. This allows "view" part of MVC to be completely written using XHTML and CSS only and all the business logic resides in the backing bean. This enables a cleaner separation of views with model and controller and thus follows the MVC design pattern in a more intuitive way. JSF 2 also defines how resources can be packaged, located, and rendered by JSF runtime within a web application.

Using these two features of Facelets and Resource Handling, JSF2 defines a composite component as a component that consists of one or more JSF components defined in a Facelet markup file that resides inside of a resource library. The composite component is defined in the defining page and used in the using page. The "defining page" defines the metadata (or parameters) using <cc:interface> and implementation using <cc:implementation> where "cc" is the prefix for "http://java.sun.com/jsf/composite" namespace. Future versions of the JSF 2 specification may relax the requirement to specify metadata as it can be derived from the implementation itself.

A composite component can be defined using JSF 1.2 as well but it requires a much deeper understanding of JSF lifecycle and also authoring multiple files. JSF2 really simplifies the authoring of composite components using just an XHTML file.

Code is king! This Tip Of The Day (TOTD) will explain how to convert an existing code fragment into a JSF2 composite component using NetBeans IDE.

Lets say a Facelet (index.xhtml) has the following code fragment:

<h:form>
    <h:panelGrid columns="3">
    <h:outputText value="Name:" />
    <h:inputText value="#{user.name}" id="name"/>
    <h:message for="name" style="color: red" />
    <h:outputText value="Password:" />
    <h:inputText value="#{user.password}" id="password"/>
    <h:message for="password" style="color: red" />
  </h:panelGrid>
  <h:commandButton actionListener="#{userService.register}"
                   id="loginButton" action="status" value="submit"/>
</h:form>

This fragment displays an HTML form with two text input boxes and a "submit" button. The two input boxes are bound to "user" bean and clicking on the button invokes "register" method of the "userService" bean.

Instead of repeating this code in multiple pages, its beneficial to convert this into a composite component and use the resulting tag instead of the complete fragment again. Why ?

  • Follows the DRY principle and allows to keep the code, that can be potentially be repeated at multiple places, in a single file.
  • It allows developers to author new components without any Java code or XML configuration.

How do you convert an existing code fragment to a composite component ? NetBeans makes it really easy.

In NetBeans IDE select the code fragment, right-click, "Refactor", "Convert to Composite Component…" as shown below:

In the next screen, change the filename to "loginPanel" and take every thing else as default as shown below:

and click on "Finish".

This will generate "web/resources/ezcomp/loginPanel.xhtml" and move the component definition to this file, aka "defining page" and looks like:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:cc="http://java.sun.com/jsf/composite"
xmlns:h="http://java.sun.com/jsf/html">
<!-- INTERFACE -->
<cc:interface>
</cc:interface>
<!-- IMPLEMENTATION -->
<cc:implementation>
<h:form>
<h:panelGrid columns="3">
<h:outputText value="Name:" />
<h:inputText value="#{user.name}" id="name"/>
<h:message for="name" style="color: red" />
<h:outputText value="Password:" />
<h:inputText value="#{user.password}" id="password"/>
<h:message for="password" style="color: red" />
</h:panelGrid>
<h:commandButton actionListener="#{userService.register}"
id="loginButton" action="status" value="submit"/>
</h:form>
</cc:implementation>
</html>

<cc:interface> defines metadata that describe the characteristics of component, such as supported attributes, facets, and attach points for event listeners. <cc:implementation> contains the markup substituted for the composite component.

<cc:interface> is generated in the page but is empty and may be made optional in a subsequent release of the JSF specification.The "using page" will declare a new namespace as:

xmlns:ez="http://java.sun.com/jsf/composite/ezcomp"

and then replace the code fragment with:

<ez:loginPanel/>

The tag name for the new composite component is the same as the "defining page" file name. This enables "<ez:loginPanel/>" to be used instead of repeating that entire code fragment.

Now lets say that the code fragment need to pass different value expressions (instead of #{user.name}) and invoke a different method (instead of #{userService.register}) when submit button is clicked in different "using page"s. The "defining page" can then look like:

<!-- INTERFACE -->
<cc:interface>
<cc:attribute name="name"/>
<cc:attribute name="password"/>
<cc:attribute name="actionListener"
method-signature="void action(javax.faces.event.Event)"
targets="ccForm:loginButton"/>
</cc:interface>
<!-- IMPLEMENTATION -->
<cc:implementation>
<h:form id="ccForm">
<h:panelGrid columns="3">
<h:outputText value="Name:" />
<h:inputText value="#{cc.attrs.name}" id="name"/>
<h:message for="name" style="color: red" />
<h:outputText value="Password:" />
<h:inputText value="#{cc.attrs.password}" id="password"/>
<h:message for="password" style="color: red" />
</h:panelGrid>
<h:commandButton id="loginButton"
action="status"
value="submit"/>
</h:form>
</cc:implementation>

The changes are highlighted in bold and explained below:

  • All the parameters are explicitly specified in <cc:interface> for clarity. The third parameter has a "targets" attribute referrring to "ccForm:loginButton".
  • In <cc:implementation>
    • The <h:form> in has "id" attribute. This is required such that the button within the form can be explicitly referenced.
    • <h:inputText> is now using #{cc.attrs.xxx} instead of #{user.xxx}. #{cc.attrs} is a default EL expression that is available for composite component authors and provide access to attributes of the current composite component. In this case #{cc.attrs} has "name" and "password" defined as attributes.
    • "actionListener" is an attach point for event listener, defined as a "method-signature" and describes the signature of a method pointed to by the expression.
    • <h:commandButton> has "id" attribute so that it can be clearly identified within the <h:form>.

The "user", "password", and "actionListener" are then passed as required attributes in the "using page" as:

<ez:loginPanel
    name="#{user.name}"
    password="#{user.password}"
    actionListener="#{userService.register}"/>

Now the "using page" can pass different "backing beans" and business method to be invoked when "submit" button is invoked.

The complete source code for this TOTD can be downloaded here.

How are you using JSF 2 composite components ?

The entire source code used in this blog can be downloaded here.

JSF 2 implementation is bundled with GlassFish Server Open Source Edition, try it today!

I realized TOTD #135 already explains how to author composite components. Hey, but this TOTD provides new information on how to attach event listeners :-)

Technorati: totd javaee6 glassfish jsf2 composite component facelets

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot

October 27, 2010

Java EE 6 & GlassFish at Cloud Computing Expo, OTN Developer Days, Oredev, Java EE 6 Workshops, JavaOne Brasil in next 2 months!

Filed under: General — arungupta @ 3:04 am

I’ll be speaking on Java EE 6 & GlassFish at several events around the world in the next couple of months and here is a quick summary …

Cloud Computing Expo Santa Clara, CA Nov 1-4 Taking your Java EE 6 Apps on the Cloud (2:25pm on 11/2) register now!
OTN Developer Days New York City, NY Nov 4 2 sessions + 3 Hands-on Lab (10am – 4:30pm), FREE, register now!
Oredev Malmo, Sweden Nov 8-12 Java EE 6 hands-on workshop (8:50am on Tuesday), JPA 2 session (5:40 on Wednesday), Run! (11:20am on Thursday), register now!
Java EE 6 & GlassFish Workshop Czech Republic Nov 22-23 Organized by Oracle University, register now!
Java EE 6 & GlassFish Workshop Hungary Nov 25-26 Organized by Oracle University, register now!
Devignition Reston, Virginia Dec 3 Java EE 6 & GlassFish 3 session (4:20pm), Panel (5:20pm), Organized by NovaJUG, register now!
CEJUG Fortaleza, Brazil Dec 4 Session, 8pm, registration page coming soon!
JavaOne Sao Paulo, Brazil Dec 7-9 Several sessions, hands-on labs, Technical General Session, JavaOne Brazil main page: English, Portuguese, Follow #javaonebrasil and @oracledobrasil.

Lots of travel over the next couple of months … New York City, Sweden, Czech Repbulic, Hungary, Reston, Fortaleza (Brazil), Sao Paulo.

Looking forward to meet several of you. Where will I see/run with you ?

Technorati: conf oracltechnet otn devdays newyorkcity oredev malmo sweden javaone brazil

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot

October 26, 2010

LOTD #23: Oracle Cloud API

Filed under: General — arungupta @ 11:14 am

Oracle Cloud Resource Model API is available here.

William blogged about it a few months back and you can find more details and references on his blog. The Elemental Resource Model of this API has already been submitted to DMTF and forms the foundation for the core of the IaaS layer. This API adopted a significant part of the original Sun Cloud API and is geared towards interoperability, private clouds, and compatibility with existing application infrastructure.

Jack Yu, Engineering Manager @ Oracle was joined by 250+ viewers on OTN Techcast Live. An archive of the video will be available soon, stay tuned!

Here are some other relevant pointers:

  • Cloud Computing on OTN
  • Cloud Computing on oracle.com
  • OTN Techcast Live

Technorati: lotd oracle cloud api dmtf

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot

October 20, 2010

Screencast #33: NetBeans 7.0 M2 and GlassFish 3.1 Features

Filed under: General — arungupta @ 6:30 am

NetBeans 7.0 M2 was released recently and comes pre-bundled with GlassFish 3.1. Vince Kraemer has been blogging about several features that have been introduced in the builds recently:

  • Application Scoped Resources
  • Web Services Nodes
  • Create domains with default ports
  • Support glassfish-resources.xml
  • View Server Log for Remote Instance
  • Restart a Remote Server in Debug Mode
  • Enable/Disable Action for Deployed Apps

Now, you can see all these features live in action in this screencast:

Enjoy!

Thanks Vince!

Technorati: screencast netbeans glassfish

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot

October 19, 2010

TOTD #146: Understanding the EJB 3.1 Timer service in Java EE 6 – Programmatic, Deployment Descriptor, @Schedule

Filed under: General — arungupta @ 4:00 pm

EJB 3.1 has greatly simplified the ability to schedule events according to a calendar-based schedule, at a specified time, after a specified elapsed duration, or at a specific recurring intervals.

There are multiple ways events can be scheduled:

  • Programmatically using Timer service
  • Automatic timers based upon the metadata specified using @Schedule
  • Deployment Descriptor

This Tip Of The Day (TOTD) will show code samples on how timer-based events can be created in EJBs.

Lets start with programmatic creation of timers first.

The Timer Service allows for programmatic creation and cancellation of timers. Programmatic timers can be created using createXXX methods on "TimerService". The method to be invoked at the scheduled time can be any of the flavors mentioned below:

  1. EJB implementing "javax.ejb.TimedObject" interface that has a single method "public void ejbTimeout(Timer timer)". For example:
    @Singleton
    @Startup
    public class DummyTimer2 implements TimedObject {
    @Resource TimerService timerService;
    @PostConstruct
    public void initTimer() {
    if (timerService.getTimers() != null) {
    for (Timer timer : timerService.getTimers()) {
    if (timer.getInfo().equals("dummyTimer2.1") ||
    timer.getInfo().equals("dummyTimer2.2"))
    timer.cancel();
    }
    }
    timerService.createCalendarTimer(
    new ScheduleExpression().
    hour("*").
    minute("*").
    second("*/10"), new TimerConfig("dummyTimer2.1", true));
    timerService.createCalendarTimer(
    new ScheduleExpression().
    hour("*").
    minute("*").
    second("*/45"), new TimerConfig("dummyTimer2.2", true));
    }
    @Override
    public void ejbTimeout(Timer timer) {
    System.out.println(getClass().getName() + ": " + new Date());
    }
    }
    
    

    The "initTimer" method is a lifecycle callback method and cleans up any previously created timers and then create new timers that triggers every 10th and 45th second. The "ejbTimeout" method, implemented from "TimedObject" interface is invoked everytime the timeout occurs. The "timer" parameter in the "ejbTimeout" method can be used to cancel the timer, get information on when the next timeout will occur, get information about the timer and other relevant data.

    Notice, there is a @Startup class-level annotation which ensures that bean is eagerly loaded, lifecycle callback methods invoked and thus timers are created before the bean is ready.

  2. At most one method tagged with "@Timeout". Methods annotated with @Timeout must have one of the following signature:
    1. void <METHOD>()
    2. void <METHOD>(Timer timer)

    The second method signature gives you the ability to cancel the timer and obtain metatdata about the timer. These methods may be public, private, protected, or package level access. For example:

    @Singleton
    @Startup
    public class DummyTimer3 {
    @Resource TimerService timerService;
    @PostConstruct
    public void initTimer() {
    if (timerService.getTimers() != null) {
    for (Timer timer : timerService.getTimers()) {
    if (timer.getInfo().equals("dummyTimer3.1") ||
    timer.getInfo().equals("dummyTimer3.2"))
    timer.cancel();
    }
    }
    timerService.createCalendarTimer(
    new ScheduleExpression().
    hour("*").
    minute("*").
    second("*/10"), new TimerConfig("dummyTimer3.1", true));
    timerService.createCalendarTimer(
    new ScheduleExpression().
    hour("*").
    minute("*").
    second("*/45"), new TimerConfig("dummyTimer3.2", true));
    }
    @Timeout
    public void timeout(Timer timer) {
    System.out.println(getClass().getName() + ": " + new Date());
    }
    }
    

    Here again, the "initTimer" method is used for cleaning / initializing timers. The "timeout" method is marked with "@Timeout" annotation is the target method that is invoked when the timer expires.

Read more details about Timer Service in the Java EE Tutorial.

Lets see how this similar functionality can be achieved using deployment descriptor (ejb-jar.xml). Basically a simple method in the POJO class given below:

public class DummyTimer4 {
public void timeout(Timer timer) {
System.out.println(getClass().getName() + ": " + new Date());
}
}

can be converted into a timer method by adding the following "ejb-jar.xml" in WEB-INF directory of WAR file:

<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar xmlns = "http://java.sun.com/xml/ns/javaee"
version = "3.1"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd">
<enterprise-beans>
<session>
<ejb-name>DummyTimer4</ejb-name>
<ejb-class>org.glassfish.samples.DummyTimer4</ejb-class>
<session-type>Stateless</session-type>
<timer>
<schedule>
<second>*/10</second>
<minute>*</minute>
<hour>*</hour>
<month>*</month>
<year>*</year>
</schedule>
<timeout-method>
<method-name>timeout</method-name>
<method-params>
<method-param>javax.ejb.Timer</method-param>
</method-params>
</timeout-method>
</timer>
</session>
</enterprise-beans>
</ejb-jar>

Multiple <schedule>s can be added to create multiple timers.

But all this is too much. EJB 3.1 adds @Schedule annotation that automatically creates timers based upon the metadata specified on a method, such as:

@Singleton
public class DummyTimer {
@Schedules({
@Schedule(hour="*", minute="*", second="*/10"),
@Schedule(hour="*", minute="*", second="*/45")
})
public void printTime() {
System.out.println(getClass().getName() + ": " + new Date());
}
}

EJB 3.1 container reads the @Schedule annotations and automatically create timers. Notice, there is no need for a @Startup annotation here as lifecycle callback methods are not required. Each re-deploy of application will automatically delete and re-create all the schedule-based timers … really clean and simple! No messing around with deployment descriptors too :-)

Interval timers created using TimerService can be easily created by using "ScheduleExpression.start()" and "end()" methods. The single-action timer can be easily created by specifying fixed values for each field:

@Schedule(year="A", month="B", dayOfMonth="C", hour="D", minute="E", second="F")

Optionally, you can also pass a "Timer" object to the methods annotated with @Schedule such as:

@Schedules({
@Schedule(hour="*", minute="*", second="*/10", info="every tenth"),
@Schedule(hour="*", minute="*", second="*/45", info="every 45th")
})
public void printTime(Timer timer) {
System.out.println(getClass().getName() + ": " +
new Date() + ":" +
timer.getInfo());
}

The "timer" object contains information about the timer that just expired. Note that "info" is passed to each @Schedule annotation and "timer.getInfo()" can be used in "printTime" method to find out which of the timers expired.

The complete source code used in this blog can be downloaded here.

Here are some other points to be noted:

  • Timers can be created in stateless session beans, singleton session beans, MDBs but not for stateful session beans. This functionality may be added to a future version of the specification.
  • Timers are persistent by default, need to made non-persistent programmatically (TimerConfig.setPersistent(false)) or automatically (by adding persistent=false on @Schedule)
  • Schedule-based timers may be optionally associated with a timezone.
  • The user code has no control over the timers created using @Schedule and thus cannot be canceled after creation.
  • Timers are not for real time as the container interleaves the calls to a timeout callback method with the calls to the business methods and the lifecycle callback methods of the bean. So the timed out method may not be invoked exactly at the time specified at timer creation.

This blog is derived from the whiteboard discussion with Linda as captured below and numerous emails with Marina for helping me understand the concepts.

Ah, the joys of sitting a few feet away from most of the Java EE 6 spec leads :-)

Here is one tweet that I saw recently on EJB timers …

Never wrote a Timer faster in #java …really like it #ejb3.1 #glassfish http://j.mp/d1owSM

I love the simplicity and power provided by @Schedule, how do you create timers in your enterprise applications ?

Technorati: totd javaee6 glassfish ejb timer deploymentdescriptor schedule annotations

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot

October 15, 2010

Java EE 6 & GlassFish @ Silicon Valley Code Camp 2010 Trip Report – Slides Now Available

Filed under: General — arungupta @ 5:18 pm

What’s common between 350 Mountain Mike’s Pizza, 920 Erik’s Deli Sandwiches, 29 huge jugs of Peet’s Coffee, and 194 geek sessions ?

It takes 1876 developers, hackers, architects, technology honchos etc to consume all of them over a weekend :-)

Yes, we are talking about Silicon Valley Code Camp 5.0!

This is a for the developer community, by the developer community, and to the developer community event focused on Agile, Cloud, HTML5, Google Developer Tools & Platforms, Java, Web services, and many other topics. The code camp website shows attendance records from the previous years as:

A slightly updated view of this data is:

As you can see, there is steady year/year growth in the number of sessions, registration, and attendance. This year specifically has seen about 60% growth on registrations and 80% on the actual attendance. The ratio between registered/attended was also steady for the first few years and has also gone higher this year. This event has truly grown organically over the past years and is the second biggest conference, with Java focus, in the USA after JavaOne! Oracle certainly was a platinum sponsor of this conference. Are there any other bigger conferences that I don’t know of ? ;-)

It would be interesting to revisit this pattern if the event starts charging a nominal (may be optional) fees from all the attendees. However the main website highlights three points:

  • by/for the developer community
  • always free
  • never occur during work hours

GlassFish team presented several sessions at the Code Camp:

  1. Java EE 6: Doing More with Less
  2. Java EE 6 Tooling
  3. Introduction to JAX-RS
  4. Servlets 3.0: Asynchronous, Extensible, Easy-to-use
  5. OSGi and Java EE in GlassFish
  6. Running your Java EE 6 applications in the Cloud

I could not deliver #5 due to a conflict but slides from all the sessions are now available:

Here are some key pointers for you:

  • Download/Participate in GlassFish community at glassfish.org
  • Download/Participate in NetBeans community at netbeans.org
  • GlassFish Videos: youtube.com/GlassFishVideos
  • NetBeans Videos

Check out the complete list of sessions by Oracle at the Code Camp. Please enter session evaluations at http://siliconvalley-codecamp.com/SessionsOverview.aspx (make sure you are logged in and then click the link at the end of the session you attended that says ‘Evaluate’).

The Android App was pretty useful, did not try the iPhone App. I wonder if there was a session on "How I created the Android/iPhone App" :-)

Personally, this was my fourth code camp (2009, 2008, 2007) and I enjoyed meeting the local geek community. I could barely spend Saturday morning at the code camp and delivered two of my sessions but its always fun to meet the usual suspects. Many thanks to Peter Kellner, Van Riper, Tammy Baker, Kevin Nilson, other organizers, and many other volunteers for running a great show!

Couple of suggestions for next year …

  • Expose the RESTful API for the code camp registration/session/speaker/etc data and organize a competition on the best created app. May be a panel with different attendees who attempted to build this application ?
  • Make sure the speakers are not running across the campus between their back-to-back talks. 

Check out some of the pictures:

And the complete photo album:

Looking forward to Silicon Valley Code Camp 6.0 next year!

Technorati: conf svcc javaee6 glassfish netbeans eclipse intellij cloud osgi servlets restful

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot

October 14, 2010

Java Pass – Training & Certification by Oracle University

Filed under: General — arungupta @ 11:00 pm
The Java Pass is a new offering from Oracle University and is a flexible education subscription program that enables you to attend its top ten Java courses in a live, virtual format for one year at an extremely discounted rate. It also includes one certification voucher so you can become Java certified.

There are several Java SE 6 and Java EE 6 courses available as part of this offering.

Hear all about this course in this short video introduction:

What are you waiting for ? Stretch your training $$ and sign up today!

Visit education.oracle.com for more details.

Technorati: oracle university education javapass javaee6 javase6 courses training certification

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot

October 9, 2010

Java2Days 2010 Trip Report

Filed under: General — arungupta @ 6:11 am

Java2Days 2010 in Sofia, Bulgaria was my first trip to Eastern Europe. I spent more time flying than actually in the city because of personal reasons but enjoyed the conference. There were approx 500 attendees but this being the only major conference in this part of the world, its only bound to grow.

The Day 1 conference started late and some initial speakers took extra time and thus by lunch time there was an hour long gap. My talk on "Running your Java EE applications in the Cloud" was bumped to end of the day in order to get back in track. Yes, the conference did get back on track but I lost a significant chunk of audience because of further delays by other speakers after lunch. Anyway the slides are available:

Running your Java EE 6 applications in the clouds

The talk explained

  • Oracle’s definition of Cloud and talked about Exalogic Elastic Compute Cloud, a.k.a "Cloud in a box"
  • Light-weight ness, extensibility, and simplicity/ease-of-use of Java EE 6
  • How to run Java EE 6 on Amazon, RightScale, Elastra, and Joyent
  • Comparison of multi-cloud vendors
  • What Java EE currently offers for the Cloud and what’s coming in Java EE 7

The other talk demonstrated how NetBeans provide extensive tooling around Java EE 6 & GlassFish. The slides-free talk showed how NetBeans makes you a lazy, aka productive, programmer and showed the wizards, code completion, integrated javadocs etc for Java Server Faces 2 (JSF), Context & Dependency Injection (CDI), Java Persistence API 2 (JPA), Servlets 3.0, Enterprise Java Beans 3.1 (EJB), and Java API for RESTful Web Services (JAX-RS). The complete sample application build during the talk can be downloaded here. Here is a review from one of the attendees:

No ivory tower or Chinese slides. Just NetBeans, Glassfish and the developer! I hope that more and more people think now that Java and Java EE in particular is much better than the various strange combinations like RoR, PHP or some other awkward framework that promises developer heaven.

Check out screencast #30 to view the complete set of Java EE 6 Netbeans tooling. Java EE 6 tooling is also available in Eclipse and can be seen in the screencast #31.

The speakers’ dinner to a local BBQ was one of the highlights. Enjoyed Rakia, local food, beautiful performances by local dancers, and then learning some basic steps with them. I wonder how many other conferences are going to provide a speakers’s dinner event like that ;-)

Enjoyed meeting Andrew Lombardi, Reza Rehman, Vassil Popovski, Damon Edwards, Eugene Ciurana, Geroge Reese, James Ward, Werner Keil, other speakers, and of course Alexis!

Many thanks to Emo, Yoana, Iva, Evo, Bobby, and rest of the Java2Days team. Their warm "airport to airport" hospitality started at the Sofia airport where Emo came to receive some of us and went all the way back to the airport when Evo ensured that I’m checked in at the airport. Looking forward to participate again next year!

Some travel tips to Bulgaria …

  • Bulgaria is not part of EU yet and does not accept Euros so make sure to withdraw some local currency from the ATM machines.
  • Any spirits/alcohol/wine from the duty free needs to be checked in if you are flying through EU.
  • Taxi drivers take local currency only, no credit cards.
  • 1 Bulgarian Lev is apporx $0.75.
  • Language will likely be an issue but with a local team member this was well taken care for us.
  • Make sure to try out some Rakia, it’s 40% alcohol made with either grape, apricot, and plums.
  • I was told to not run on the streets because there were drug addicts in at least the part of city where we were staying.
  • Sofia is not a small city and the traffic can be intense during peak hours so plan accordingly.

And finally some pictures …

And the complete album:

Next stop is Silicon Valley Code Camp which starts in about 3 more hours. This is the 5th anniversary of the conference and 4th mine (2009, 2008, 2007). With about 3000 registered attendees, this is going to be quite a conference. If half of the registered attendees show up, then this is going to be the biggest US conference after JavaOne!

Technorati: conf java2days sofia bulgaria glassfish javaee6 cloud netbeans

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot

October 6, 2010

TOTD #145: CDI Events – a light-weight producer/consumer in Java EE 6

Filed under: General — arungupta @ 5:00 am

Contexts & Dependency Injection (JSR 299) defines a standards-based type-safe Dependency Injection mechanism in the Java EE 6 platform. The type-safety comes from the fact that no String-based identifiers are used to identify the dependencies and instead all the information is specified using the Java object model. The loose coupling is possible because the bean requesting injection need not be aware of the lifecycle, concrete implementation, threading model and similar details of the injected bean. The CDI runtime automatically figures out the right bean in the right scope and context and then injects it correctly for the requestor.

CDI Events take loose coupling to the next level by following the Observer pattern. Event producers raise events that are consumed by observers. The event object carries state from producer to consumer.

Code is king, so lets understand the above text with a simple sample. Consider the following POJO that captures the state showing how many pages have been printed:

public class PrintEvent {
private int pages;
public PrintEvent(int pages) {
this.pages = pages;
}
public int getPages() {
return pages;
}
}

A producer of this event will look like:

@Inject Event printEvent;
public void print() {
printEvent.fire(new PrintEvent(5));
}

Inject a "PrintEvent" using @Inject and "fire" the event with the payload and state. And the consumer of this event will look like:

public void onPrint(@Observes PrintEvent event) {
System.out.println(event.getPages() + " pages printed");
}

Any method with "@Observes" and the observed event type as parameter will receive the event. In this case, this method will observe all the events. However the observer can specify qualifiers to narrow the set of event notifications received. So lets create a CDI qualifier as:

package com.example.events;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import javax.inject.Qualifier;
@Qualifier
@Retention(RUNTIME)
@Target({METHOD, FIELD, PARAMETER, TYPE})
public @interface ShortPrintJob {
}

and create two more similar qualifiers as "MediumPrintJob" and "LongPrintJob". The "producer" can then look like:

@Inject @ShortPrintJob Event shortPrint;@Inject @MediumPrintJob Event mediumPrint;@Inject @LongPrintJob Event longPrint;

public void print2() {    shortPrint.fire(new PrintEvent(5));    mediumPrint.fire(new PrintEvent(30));    longPrint.fire(new PrintEvent(80));}

The consumer can then look like:

public void onShortPrint(@Observes @ShortPrintJob PrintEvent event) {    System.out.println(event.getPages() + " pages printed (short)");}

public void onMediumPrint(@Observes @MediumPrintJob PrintEvent event) {    System.out.println(event.getPages() + " pages printed (medium)");}

public void onLongPrint(@Observes @LongPrintJob PrintEvent event) {    System.out.println(event.getPages() + " pages printed (long)");}

Notice how qualifiers are specified in each method to narrow the set of event notifications. This will print the output as:

INFO: 5 pages printed (short)INFO: 30 pages printed (medium)INFO: 80 pages printed (long)

In the above case, "producer" is "qualified" by annotating at the Event inject point. CDI also allows for the qualifiers to be dynamically specified. So

@Inject @ShortPrintJob Event shortPrint;shortPrint.fire(new PrintEvent(5));

can be replaced with

@Inject Event printEvent;
printEvent.select(new AnnotationLiteral<ShortPrintJob>(){}).fire(new PrintEvent(10));

Now, if we kept our original method "onPrint" in the consumer as well then each fired event will be delivered to this common method and the "qualified" method.

CDI also defines "Transactional observer methods" that receive event notifications during the different phases of a transaction in which the event was fired. There are pre-defined phases IN_PROGRESS, BEFORE_COMPLETION, AFTER_COMPLETION, AFTER_FAILURE, and AFTER_SUCCESS that allows you to receive and track events and take actions, if any, based upon the result. One use case is to keep the cache updated after receiving events when products are added / deleted from the database. Another use case is to it push data to the front end after a long-running transaction is successful.

You can certainly try all of this in GlassFish and NetBeans provides extensive tooling around CDI and broader Java EE 6. Check out screencast #30 for the complete Java EE 6 tooling using NetBeans.

How are you using CDI events ?

Technorati: totd javaee6 cdi events producer consumer glassfish netbeans

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • DZone
  • StumbleUpon
  • Technorati
  • Twitter
  • Slashdot
Older Posts »

The views expressed on this blog are my own and do not necessarily reflect the views of Oracle.
Powered by WordPress