Java EE 7 SDK |
This is a simple EJB module that demonstrates how to use the EJB automatic timer.
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:
LogRecord
.
The persistence.xml
file defines a persistence unit
which uses jdbc/__default
as the data source and the java2db
feature of Glassfish to create tables.
@javax.ejb.Schedule
annotation to create a timer and to register the test_automatic_timer
method as the timeout method. This EJB creates log records on timeout callbacks and
cancels the timer after 10 callbacks. The sample uses the Java Persistence API
to store the log records in a database.
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
.
This sample demonstrates the following key features:
app_dir
is the sample application base
directory: samples_install_dir/javaee7/ejb/automatic-timer/automatic-timer-server
.
Change directory to app_dir.
run
outcome.mvn clean verify cargo:run
samples_install_dir/javaee7/ejb/automatic-timer/automatic-timer-client
. mvn clean verify exec:java
If you have problems when running the application, refer to the troubleshooting document.
Copyright © 1997-2013 Oracle and/or its affiliates. All rights reserved.