开始&使用servlet在Java EE环境中停止ScheduledExecutorService [英] Start & Stop a ScheduledExecutorService in Java EE environment using servlet

查看:141
本文介绍了开始&使用servlet在Java EE环境中停止ScheduledExecutorService的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们需要使用简单的servlet应用程序通过JMX监视远程JVM详细信息。所以在独立应用程序中完成的工作是

We have a requirement where in we need to monitor remote JVM details via JMX using a simple servlet application. So things done till now in a standalone application is

1)创建一个JMX连接器&获取内存数据 - >完成
2)我们需要不断监控&得到记录
(2.1>可以被视为不间断延迟的计划任务&将记录插入DB或2.2> JMX是否提供历史记录,如果是,哪个MBean要访问该信息?)。

1) Creat a JMX connector & get the Memory data --> done 2) We need to constantly monitor & get the records (2.1 > which can be considered as scheduled task at constant delay & insert the records into DB Or 2.2> does the JMX gives the history if yes which MBean to access for the info?).

这里我打算使用一个界面来注册域,然后就可以了。有开始&来自JSP的停止按钮。我们点击启动系统时的功能将运行调度程序(ScheduledExecutorService)&捕获背景记录以提供历史记录。当使用点击停止时,调度程序必须停止后台进程。问题是我们如何控制和获取调度程序的对象?

Here I am planning to use an interface to register the domain , followed to it. Have start & stop button from JSP. The functionality been when we click start the system will run the scheduler (ScheduledExecutorService ) & capture the records at background to give the history. When the uses clicks stop the scheduler has to stop the background process. The question is how can we control & get the object of a scheduler ?

1)换句话说,我们怎样才能开始&通过servlets停止ScheduledExecutorService?从一个servlet开始一个线程&从另一个servlet停止某个特定任务的线程?

1) In other words how can we start & stop a ScheduledExecutorService via servlets ? start a thread from one servlet & stop a thread from another servlet for a particular task ?

2)如果我们有集群/负载均衡环境怎么办?

2) What if we have a clustered/load balanced environment ?

目前正在考虑添加每个ScheduledExecutorService进入HashMap,关键是任务对象&值是使用SingleTon设计模式的ScheduledExecutorService。有没有默认方法。使用SingleTon的整个循环处于集群/负载平衡环境中,我们可能无法获得适当的更新对象。

Currently am thinking of adding each ScheduledExecutorService into HashMap , key been the task object & value been the ScheduledExecutorService using SingleTon desgin pattern. Is there any default approach. The loop whole with SingleTon is in clustered/load balanced environment we may not be able to get the appropriate update objects.

期待您的宝贵建议。

推荐答案

如果在java ee 7上,请尝试使用 javax.enterprise.concurrent.ManagedScheduledExecutorService

If on java ee 7, try using the javax.enterprise.concurrent.ManagedScheduledExecutorService

然后你可以进行资源注入并使用类似下面的代码启动任务。

Then you can do a resource injection and start the task with code similiar to below.

@Resource
ManagedScheduledExecutorService mses;

public void startTask() {
    mses.scheduleAtFixedRate(runnable, 10, 10, SECONDS);
}

在Java EE 6中,您可以使用 timerservice API

In Java EE 6 you could have a servlet create/remove using the timerservice API

这篇关于开始&使用servlet在Java EE环境中停止ScheduledExecutorService的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆