取消部署应用程序时,计时器 java 不会停止在 Netbeans 中显示结果?

Timer java not stop showing result in Netbeans when Undeploy the application?

我确实创建了一个计时器,它在我部署我的应用程序时启动,我注意到这个计时器在我 Undeploy 我的应用程序时没有停止?

  1. 怎么会这样,并在输出 netbeans 中显示结果?
  2. 我是否应该在每次 Undeploy 我的服务器时重新启动我的服务器? 申请?

单例

@Singleton
@Startup
public class StartWhenDeploy {

    private static final int PERIOD = 3000;

    @PostConstruct
    public void init() {
        System.out.println("I will set information to start my task");
        Timer timer = new Timer();
        timer.schedule(new TimerAction(1), new Date(), PERIOD);
    }
}

TimerTask

public class TimerAction extends TimerTask {

    public int nbrUsers;

    public TimerAction(int nbrUsers) {
        this.nbrUsers = nbrUsers;
    }

    @Override
    public void run() {
        System.out.println("This task is planified to execute at " + new Date());
        System.out.println("Creation " + (createUser() ? "------------Success------------" : "------------Failed------------"));
    }

    public boolean createUser() {
        try {
            System.out.println("-------------->" + nbrUsers);
            for (int i = 0; i < nbrUsers; i++) {
                System.out.println("Create user >>>>" + i);
            }
            return true;
        } catch (Exception e) {
            System.out.println("Exception " + e);
            return false;
        }
    }
}

它仍然在 Output netbeans:

中显示这样的结果
...
Infos: This task is planified to execute at Wed Nov 16 14:40:29 GMT+01:00 2016
Infos: -------------->1
Infos: Create user >>>>0
Infos: Creation ------------Success------------
...

有人对这个问题有想法吗?

谢谢。

TimerTask 生成一个新线程,其生命周期不受取消部署应用程序的影响。

更好的方法是使用适当的 EJB 计时器和 @Schedule,例如 this example:

@Singleton
@Startup
public class SimpleTimerBean {

    static Logger logger = Logger.getLogger(SimpleTimerBean.class.getCanonicalName());

    @Schedule(hour = "*", minute = "*", second = "*/3", info = "Create user every 3 seconds", timezone = "UTC")
    public boolean createUser() {
        try {
            System.out.println("-------------->" + nbrUsers);
            for (int i = 0; i < nbrUsers; i++) {
                System.out.println("Create user >>>>" + i);
            }
            return true;
        } catch (Exception e) {
            System.out.println("Exception " + e);
            return false;
        }
    }
}

在 GlassFish 中(通常在 JavaEE 中),您应该使用 EJB 规范中的 TimerService 进行调度。我假设您正在使用 java.util.Timer,它只是在一个单独的线程中运行。 GlassFish 对线程一无所知,因此无法通过取消部署来停止它。

你应该将你的单例重写成这样:

@Singleton
@Startup
public class StartWhenDeploy {

    private static final int PERIOD = 3000;

    // Inject the TimerService into this EJB
    @Resource
    private TimerService timer;

    private TimerAction action;

    @PostConstruct
    public void init() {
        System.out.println("I will set information to start my task");
        // the action object is created before the timer
        action = new TimerAction(1);
        timer.createTimer(new Date(), PERIOD, "My timer");
    }

    // this method will be executed when the timer fires - it needs to wrap your `TimerAction` created once per this singleton instance (`TimerAction` does not have to extend `TimerTask` now)
    @Timeout
    public void runTimerAction() {
        action.run();
    }

}