【问题标题】:Storing Servlet post requests for a minute in file for every 1 hour interval每 1 小时间隔在文件中存储 Servlet 发布请求一分钟
【发布时间】:2016-10-13 17:58:32
【问题描述】:

我正在尝试找到一些选项来将一分钟的发布请求存储到文件中,并希望每小时重复一次。我每秒有大约 3000 个请求,我只想存储请求消息一分钟,它会给我大约 180000 个请求进行分析。但我想每小时对请求消息进行相同的分析。

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    try {
        ServletInputStream inputStream = request.getInputStream();
        Reader reader = new InputStreamReader(inputStream);
        requestMessage = gson.fromJson(reader, Request.class);

        //I am trying to print requestMessage in one file for a minute at interval of 1 hour to do analysis on it
        //(I am having around 3000 post requests per seconds)

    } catch (Exception e) {
        e.printStackTrace();
    }

    response.setStatus(HttpServletResponse.SC_NO_CONTENT);

}

我尝试在 Post 方法中使用以下代码,但它不能正常工作,因为每次我有新请求时它都会启动新的计划,并且我将相同的代码放在 init() 方法中,但它没有输出。

service = Executors.newSingleThreadScheduledExecutor();
service.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                System.out.println("Starting of 1 Hour time: "+new Date());
                ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();

                exec.schedule(new Runnable(){
                    @Override
                    public void run(){
                        System.out.println("Starting of 1 minute: "+new Date());

                        while(requestMessage.getID().equals("123"))
                        {
                            try {
                            System.out.println("Printing to File: "+new Date());    Files.write(Paths.get("location/requestMessage.txt"),requestMessage, StandardOpenOption.APPEND);
                            } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                            }
                        }
                    }
                }, 1, TimeUnit.MINUTES);
            }
        }, 0, 1, TimeUnit.HOUR);

我有点迷失在这里并寻找一些选择。 这可以通过使用 Executors 或 Thread 来实现吗?如果没有,我可以尝试哪些其他选择?

谢谢!

【问题讨论】:

  • 这个“每隔一小时一分钟”不清楚,能否请您澄清一下?
  • 理想情况下你能提供一个例子来澄清吗?
  • 感谢 Nicolas 的回复。我试图解释更多。

标签: java multithreading servlets httprequest


【解决方案1】:

如果您想在每小时 1 分钟内执行给定任务,您可以做的最好的事情是安排一个每隔一小时启动一次的主任务,只要我们不这样做,它将继续执行递归任务达到一分钟。

ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
// Schedule the main task to be executed every one our
service.scheduleAtFixedRate(
    new Runnable() {
        @Override
        public void run() {
            // Get the initial time
            long initial = System.currentTimeMillis();
            // Iterate as long as the current time - initial time is less than 60 K ms (1m)
            do {
                // Here is my recursive task that I want to do during 1 minute
            } while ((System.currentTimeMillis() - initial) < 60_000L);
        }
    }, 0L, 1L, TimeUnit.HOURS
);

【讨论】:

  • 您好,再次感谢您的回复。
  • 我已经尝试根据我的用法使用你的代码结构到 init() 方法并且它有效。添加了 Thread.sleep(5);在 do...while 循环中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-03
  • 2017-10-17
  • 1970-01-01
  • 2021-12-08
  • 1970-01-01
相关资源
最近更新 更多