Java EE 6 SDK 

Samples Main Page

The EJB 3.1 Automatic Timer Sample Application

This is a very simple EJB Stateless Session Bean with a Remote business interface and a POJO Entity that demonstrates EJB 3.1 automatic timer feature.

Application Structure

The application is built and deployed as an EJB module.

EJB Module

The EJB module consists of:

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

@Remote
public interface StatelessSession {

    public List<String> getRecords();

}


        Note: Unlike EJB 3.0, this interface is used only to verify the timeout calls, not to create the EJB Timer.

The business interface is designated as a remote business interface by using the @javax.ejb.Remote annotation.

Stateless Session Bean Class
The bean implementation is as follows:

@Stateless
public class StatelessSessionBean implements StatelessSession {

    @PersistenceContext EntityManager em;

    @Schedule(second="*/3", minute="*", hour="*", info="Automatic Timer Test")
    public void test_automatic_timer(Timer t) {
        long count = (Long)em.createNamedQuery("LogRecord.count").getSingleResult();
        System.out.println("Call # " + (count + 1));
        if (count > 10) {
            throw new IllegalStateException("Too many timeouts received: " + cache.size());
        } else if (count == 10) {
            LogRecord lr = new LogRecord("Canceling timer " + t.getInfo() + " at " + new Date());
            em.persist(lr);
            t.cancel();
            System.out.println("Done");
        } else {
            LogRecord lr = new LogRecord("" + t.getInfo() + " timeout received at " + new Date());
            em.persist(lr);
        }
    }

    public List<String> getRecords() {
        return (List<String>)em.createNamedQuery("LogRecord.findAllRecords").getResultList();
    }


}

Note: The use of the @javax.ejb.Schedule annotation that causes timeouts to happen every 3 seconds starting with 0.

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.  
Sun GlassFish Enterprise Server Specific Deployment Configuration
There is no need to define any Sun GlassFish Enterprise Server-specific deployment descrpitors, such as sun-ejb-jar.xml, sun-application-client.xml for this example.   The global JNDI name used for lookup of the Remote Stateless Session is : java:global/automatic-timer-ejb/StatelessSessionBean

Building, Deploying, and Running the Application

Perform the following steps to build, deploy, and run the application:
  1. Setup your build environment and configure the application server with which the build system has to work by following the common build instructions.
  2. app_dir is the sample application base directory: samples_install_dir/javaee6/ejb/automatic-timer.
  3. Change directory to app_dir.
  4. Make sure the JavaDB database is started.
  5. Set up JDBC resources. Build, deploy, and run the sample application using the all target.
  6. app_dir> ant all

    You can replace the ant all command with the following set of commands:

    app_dir> ant default compiles and packages the application

    app_dir> ant deploy deploys it to application server

    app_dir> ant run runs the test java client

  7. Use the target clean to undeploy the sample application and to remove the temporary directories like build and dist.

    app_dir> ant clean

Building, Deploying, and Running the Application in NetBeans IDE

Perform the following steps to build, deploy, and run the application using NetBeans IDE:

  1. Refer to the common build instructions for setting up NetBeans IDE and Java EE 6 SDK.
  2. In the NetBeans IDE, choose File → Open Project (Ctrl-Shift-O), navigate to the samples_install_dir/javaee6/ejb/ directory, select automatic-timer, and click Open Project.
  3. In the Projects tab, right click automatic-timer-ejb and select Run to build, deploy, and run the project.

Troubleshooting

If you have problems when running the application, refer the troubleshooting document.

 

Copyright © 1997-2010 Oracle and/or its affiliates. All rights reserved.