Java EE 6 SDK |
The application is built and deployed as an EJB module.
The EJB module consists of:
A POJO that represents a LogRecord
.
The persistence.xml
file defines a persistence unit. The
persistence
unit uses jdbc/__default
as the data source, and the java2db
feature of
glassfish to create tables.
An EJB that uses @javax.ejb.Schedule
annotation to both cause
creation of an EJB Timer and designate the method test_automatic_timer as the
timeout method. This EJB logs records on
timeout callbacks, and cancels the timer after 10 callbacks. The sample
uses JPA to store the log records in the database.
The Stateless Session bean has a Remote business interface with a single business method.Note: Unlike EJB 3.0, this interface is used only to verify the timeout calls, not to create the EJB Timer.
@Remote
public interface StatelessSession {
public List<String> getRecords();
}
@javax.ejb.Remote
annotation.The bean implementation is as follows:Deployment Descriptor
@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.
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 assun-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
app_dir
is the sample application base
directory: samples_install_dir/javaee6/ejb/automatic-timer
.
Change directory to app_dir.
all
target.
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
app_dir> ant
clean
Perform the following steps to build, deploy, and run the application using NetBeans IDE:
samples_install_dir/javaee6/ejb/
directory, select automatic-timer
, and click Open Project.automatic-timer-ejb
and select Run to build, deploy, and run the project.If you have problems when running the application, refer the troubleshooting document.
Copyright © 1997-2010 Oracle and/or its affiliates. All rights reserved.