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

查看:32
本文介绍了开始&使用 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) 换句话说,我们如何开始 &通过 servlet 停止 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 中,您可以使用 定时器服务 API

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

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

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