⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timerservicebean.java

📁 ejb3实例教程全部配书源码
💻 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 + -