?? timerservicebean.java
字號:
package com.foshanshop.ejb3.impl;
import java.util.Date;
import com.foshanshop.ejb3.TimerService;
import javax.annotation.Resource;
import javax.ejb.Remote;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
import javax.ejb.Timeout;
import javax.ejb.Timer;
@Stateless
@Remote ({TimerService.class})
public class TimerServiceBean implements TimerService {
private int count = 1;
private @Resource SessionContext ctx;
public void scheduleTimer(long milliseconds){
count = 1;
ctx.getTimerService().createTimer(new Date(new Date().getTime() + milliseconds),milliseconds, "大家好,這是我的第一個定時器");
}
@Timeout
public void timeoutHandler(Timer timer)
{
System.out.println("---------------------");
System.out.println("定時器事件發生,傳進的參數為: " + timer.getInfo());
System.out.println("---------------------");
if (count>=5){
timer.cancel();//如果定時器觸發5次,便終止定時器
}
count++;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -