Java EE 7 SDK 

Samples Main Page

The Automatic Timer EJB Sample Application (EJB Module)

This is a simple EJB module that demonstrates how to use the EJB automatic timer.

Description

The EJB module contains an stateless session bean with a remote business interface and a Java entity. The stateless session bean demonstrates how to use the EJB automatic timer.

This sample application consists of two parts: an EJB module (this project) and a test client. The rest of this document describes the EJB module.

EJB Module

The EJB module consists of the following elements:

Business Interface

The stateless session bean has a remote business interface with a single business method:

@Remote
public interface StatelessSession {

    public List<String> getRecords();
}

Unlike in EJB 3.0, this interface is used only to verify the timeout calls. It is not used 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 the following:

@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();
    }
}

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

Deployment Descriptor

The deployment descriptor is no longer required by using annotations.

GlassFish Enterprise Server Specific Deployment Configuration

There is no need to define any GlassFish Enterprise Server-specific deployment descrpitors, such as sun-ejb-jar.xml or 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.

Key Features

This sample demonstrates the following key features:

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/javaee7/ejb/automatic-timer/automatic-timer-server.
  3. Change directory to app_dir.
  4. Ensure that the JavaDB database service is running.
  5. Build and deploy the sample application using the run outcome.

    mvn clean verify cargo:run

  6. Go to: samples_install_dir/javaee7/ejb/automatic-timer/automatic-timer-client.
  7. Build and Run the application client.

    mvn clean verify exec:java

Troubleshooting

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



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