This tutorial explains ou what a Timer Service in EJB is.
In order to create a timer in your Java EJB in the programmatically you have to inject the TimerService into your EJB using @Resource.
Here it is an example of Timer Service in Java/ EJB :
packageejb;importjava.util.Date;importjavax.annotation.PostConstruct;importjavax.annotation.Resource;importjavax.ejb.Singleton;importjavax.ejb.Startup;importjavax.ejb.Timeout;importjavax.ejb.Timer;importjavax.ejb.TimerService;@Singleton@StartuppublicclassAddIntegersimplementsAddIntegersRemoteInterface{@ResourceprivateTimerService timerService;@PostConstructprivatevoidinit(){
timerService.createTimer(5000,5000,null);}@Timeoutpublicvoidexecute(Timer timer){System.out.println("Java procedure executed at "+newDate());System.out.println(returnSuma(1,3));System.out.println("____________________________________________");}@OverridepublicintreturnSuma(int a,int b){// What the EJB3 will returnreturn a+b;}}
This EJB will run the execute(Timer timer) method at every 5 seconds.
Using annotations to create a scheduled task is really easy as well. Take an EJB, add one @Schedule annotation or multiple @Schedule annotations as parameters of a @Schedules annotation to a method. Adding a Scheduler to an EJB is similar to adding a Timer.