Timer Service ის გამოსაყენებლად იმპლემენტაცია უნდა გავუკეთოთ javax.ejb.TimedObject ინტერფეისს, რომელშიც აღწერილია ქოლბექ მეთოდი ejbTimeout( ):
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface TimedObject { | |
public void ejbTimeout(Timer timer) ; | |
} |
EJB ში შეგვიძლია @javax.ejb.Timeout ანოტაციაც დავადოთ მეთოდს, რომელიც voidს აბრუნებს, და ექნება javax.ejb.Timer პარამეტრი.
აქვე ორივეს ვნახავთ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Stateless | |
public class Example1 implements javax.ejb.TimedObject { | |
public void ejbTimeout(javax.ejb.Timer timer) { | |
// ბიზნეს ლოგიკა | |
} | |
} |
ალტერნატივა არი @javax.ejb.Timeout ანოტაცია:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Stateless | |
public class Example2{ | |
@Timeout | |
public void maintenance(javax.ejb.Timer timer) { | |
// ბიზნეს ლოგიკა აქ | |
} | |
} |
}
იმისათვის რომ დავარეგისტრიროთ ბინი დროის პერიოდში TimerService –ს ვიყენებთ. TimerService შეგვიძლია inject გავუკეთოთ @javax.annotation.Resource ით ან EJBContext იდან წამოვიღოთ.
ანუ ეს ორი ვარიანტი გვაქვს:
@Resourceprivate SessionContext ctx; ctx.getTimerService();
@Resource javax.ejb.TimerService timerService;
მაგალითად დარეგისტრირებისთვის შევქმნათ CalendarTimer, ან შეგვილია პირდაპირ IntervalTimer, ანაც SingleActionTimer იც აქვს. დეტალურად API ში შეგიძლიათ ნახოთ, რა რას აკეთებს, აქ რო არ დავწყო მე პოემების წერა. აგერ ლინკიც: http://docs.oracle.com/javaee/6/api/javax/ejb/TimerService.html - Just Google :-)
ტაიმერის წაშლაც ადვილადაა შესაძლებელი, პირდაპირ cancell() მეთოდი აქვს.
აგერ ერთი მაგალითიც CalendarTimer–ის.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Resource | |
private TimerService timerService; | |
private ScheduleExpression expression; | |
private TimerConfig timerConfig; | |
@Override | |
public void setUpTimer() { | |
LOG.info("cleans all timer settings"); | |
if (timerService.getTimers() != null) { | |
for (Timer timer : timerService.getTimers()) { | |
timer.cancel(); | |
} | |
} | |
String nextTime=TimerConfUtils.getTimerInterval(); | |
expression = new ScheduleExpression().dayOfWeek("*").hour("*").minute("*").second(nextTime); | |
timerConfig = new TimerConfig(); | |
timerConfig.setPersistent(true); | |
timerConfig.setInfo(TIMER_NAME); | |
timerService.createCalendarTimer(expression, timerConfig); | |
} |
მოლკედ მთავარი მუღამი ისაა, რომ თუ სერვერი დარესტარტდება ან რამე მაგდაგვარი, ტაიმერრი თავის დროს მაინც გააგრძელებს მუშაობას. ანუ რომ შეხვიდეთ Jboss ის ფოლდერში, მაგალითად : /usr/jboss/standalone/data/timer-service-data ფოლდერში
მანდ ნახავთ თქვენს ობიექტებს ტაიმერისას.
რაც შეეხება EJB 3.1 აქ უკვე არის @Schedule ანოტაცია. ძალიან მარტიავად
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Singleton | |
public class TimerService { | |
@EJB | |
private CryptoDaoLocal cryptoDao; | |
@Schedule(second="*/1", minute="*",hour="*", persistent=false) | |
public void doWork(){ | |
LOG.log(CryptoDaoLocal.generateKeys()); | |
} | |
} |
აგერ XML ითაც შეგვიძლია გავაკეთოთ იგივე @Schedule რომ არ ვწეროთ:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<ejb-jar xmlns="http://java.sun.com/xml/ns/javaee" version="3.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd"> | |
<enterprise-beans> | |
<session> | |
<ejb-name>ClealTemporaryFile</ejb-name> | |
<ejb-class>ge.embedded.signer.schedule.ClealTemporaryFile</ejb-class> | |
<session-type>Stateless</session-type> | |
<timer> | |
<schedule> | |
<minute>*/30</minute> | |
<hour>*</hour> | |
<month>*</month> | |
<year>*</year> | |
</schedule> | |
<timeout-method> | |
<method-name>ServiceTask</method-name> | |
</timeout-method> | |
</timer> | |
</session> | |
</enterprise-beans> | |
</ejb-jar> |
ეს XML კი META-INF ში ჩავაგდოდ – Deployს მერე jar ის META_INF ში ჩახტება;