简体   繁体   English

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

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

I do create a timer which start when i deploy my application, what i note is this timer not stop when i Undeploy my application? 我确实创建了一个计时器,该计时器在部署应用程序时启动,我注意到在Undeploy应用程序时该计时器不会停止吗?

  1. How can this happen, and show me the result in Output netbeans? 这怎么发生,并在Output netbeans中显示结果?
  2. Should i restart my server every time that i Undeploy my application? 每次Undeploy应用程序时,我都应该重新启动服务器吗?

Singleton 辛格尔顿

@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;
        }
    }
}

It still show me the result like this in Output netbeans : 它仍然在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------------
...

Someone have an idea about this issue? 有人对这个问题有想法吗?

Thank you. 谢谢。

TimerTask spawns a new thread whose lifecycle is unaffected by undeploying your application. TimerTask产生一个新线程,该线程的生命周期不受取消部署应用程序的影响。

A better way to do this would be to use a proper EJB timer with @Schedule like this example : 更好的方法是像下面的示例那样在@Schedule使用适当的EJB计时器:

@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;
        }
    }
}

In GlassFish (in JavaEE in general), you should use the TimerService from the EJB specification for scheduling. 在GlassFish中(通常在JavaEE中),应使用EJB规范中的TimerService进行调度。 I assume you are using java.util.Timer , which just runs in a separate thread. 我假设您正在使用java.util.Timer ,它仅在单独的线程中运行。 GlassFish does not know anything about the thread, so it cannot stop it with undeploy. GlassFish对线程一无所知,因此无法取消部署就停止它。

You should rewrite your Singleton to something like this: 您应该将Singleton重写为如下形式:

@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();
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM