如何通过Web管理计时器/时间任务

时间:2014-05-21 12:58:47

标签: java timer get glassfish

我使用以下代码在java中实现了Timer

public static void main(String[] args) {

    Timer timer = new Timer("XMLFileReader");
    ReadXMLFile t = new ReadXMLFile();

    timer.schedule(t, 0, 10 * 1000);
}

每10秒钟应下载并处理一个XML文件。 ReadXMLFileTimerTask的子类,并实现方法run。 它从shell启动时工作正常。

关键是我必须通过网络启动/停止(即运行,取消)此计时器。也就是说,我应该能够使用这样的动作参数向我的glassfish(版本3.1.2.2 build 5)服务器发送请求:

http://localhost:8080/myDownloadXML?action=run
分别

http://localhost:8080/service?action=cancel

为此,我写了一个类如下:

@Path("/service")
public class PollService {

private final Logger logger = LoggerFactory.getLogger(this.getClass());

@GET
public Response poll(@QueryParam("action") String action) {

if (!action.equals("run") && !action.equals("cancel")) {
    String msg = "wrong action (run | cancel) " +   action + " " + action.length();
    logger.error(msg);

    return Response.status(HttpURLConnection.HTTP_BAD_REQUEST).entity(msg).build();
}

ReadXMLFile rXmlFile = new ReadXMLFile();
Timer timer = null;

if (action.equals("run")) {
    timer = new Timer("XMLFileReader");
    timer.schedule(rXmlFile, 0, delay * 1000);

    return Response.ok("Running").build();
}

if (action.equals("cancel")) {
     rXmlFile.cancel();

     return Response.ok("Cancelling").build();
}

return Response.ok().build();
}
}

我可以run我的计时器,但我不能取消它。据我所知,cancel方法是错误的,因为它被应用于一个不是正在运行的对象。

如何通过http?

使用GET请求停止我的计时器

2 个答案:

答案 0 :(得分:1)

您说得对,您尝试停止的计时器任务不是您已启动的计时器任务,因为您在每个HTTP GET上创建了一个新任务。您需要在某种单例中存储对任务的引用(在此上下文中,不是在每个请求上创建的对象)。一些选择:

  • 我不确定PollService是如何配置和实例化的,但如果它是你的web-app中的一个sigleton,你只需将计时器任务存储在类字段中。
  • 如果PollService不是单例,您可以将引用存储在PollService(或其他类)的静态字段中。在这种情况下,您需要处理并发访问。

答案 1 :(得分:0)

我找到了问题的解决方案。实际上@kkamenev是对的。诀窍是使用注释@singleton并同步计时器。

我发布了“最终”代码:

import java.net.HttpURLConnection;
import java.util.Timer;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sun.jersey.spi.resource.Singleton;

@Singleton
@Path("/inrix")
public class PollService {

private static Timer timer = new Timer("XMLFileReader");
private  boolean isRunning = false;

static {
    System.out.println("INSTANTIATED!!!!!!!!!!!!!!!!!!!!!!!!");
}


@GET
public Response poll(@QueryParam("action") String action) {

    if (!action.equals("run") && !action.equals("cancel")) {
        String msg = "wrong action (run | cancel) " + action + " " + action.length();
        logger.error(msg);

        return Response.status(HttpURLConnection.HTTP_BAD_REQUEST).entity(msg).build();
    }

    ReadKMLFile rKmlFile = new ReadKMLFile();

    if (action.equals("run")) {

        synchronized (timer) {
            if (isRunning) {
                return Response.ok("Process is already running").build();
            } else {
                // Delay in seconds
                timer.schedule(rKmlFile, 0, 100 * 1000);
                isRunning = true;
                return Response.ok("started...").build();
            }
        }

    }

    if (action.equals("cancel")) {

        synchronized (timer) {
              if (!isRunning) {             
                return Response.ok("Process is not running...").build();
              } else {
                timer.cancel();
                isRunning = false;
                return Response.ok("Cancelling").build();
              }
            }

    }

    return Response.ok().build();
  }

}
相关问题