如何使用java每15分钟将连续数据移入文件

时间:2022-12-17 01:08:11

There will be data coming continuously from x device i need to move them in to files for every 15minutes please share some ideas how we can do this one in java.

将有来自x设备的数据不断传输我需要每隔15分钟将它们移动到文件中请分享一些我们如何在java中执行此操作的想法。

2 个解决方案

#1


1  

There is an Executors framework in java SE since java 1.5.

自java 1.5以来,java SE中有一个Executors框架。

More specifically for your use case the class Executors contains method newSingleThreadScheduledExecutor(). Which will return new ScheduledExecutorService instance. Use that service to schedule some Runnable implementing work you need to do. Following code snippet illustrates the idea.

更具体地说,对于您的用例,类Executors包含方法newSingleThreadScheduledExecutor()。这将返回新的ScheduledExecutorService实例。使用该服务来安排您需要执行的一些Runnable实施工作。以下代码片段说明了这个想法。

private final ScheduledExecutorService scheduler =
   Executors.newSingleThreadScheduledExecutor();


 final Runnable myWorkingRunnable = new Runnable() {
   public void run() { System.out.println("Working"); }
 };

 final ScheduledFuture<?> workHandler =
   scheduler.scheduleAtFixedRate(myWorkingRunnable , 0, 15, TimeUnit.MINUTES);

You may use workHandler or scheduler to further manipulation with the service or scheduled Runnable.

您可以使用workHandler或scheduler来进一步操作服务或计划的Runnable。

#2


1  

You can set threads or use a scheduler like Quartz http://quartz-scheduler.org/

您可以设置线程或使用Quartz http://quartz-scheduler.org/等调度程序

You only need to schedule a task every 15 minutes with quartz, then move your files into the task

您只需要每15分钟使用quartz安排一个任务,然后将文件移动到任务中

First, import quartz dependency with maven

首先,用maven导入石英依赖

<dependency>
    <groupId>org.quartz-scheduler</groupId>
    <artifactId>quartz</artifactId>
    <version>2.2.1</version>
</dependency>

Then make a Job class

然后做一个Job类

public class MoveFilesJob implements Job {

    public void execute(JobExecutionContext context) throws JobExecutionException {
        // TODO move your files here

    }

}

Finally, make a trigger and run it

最后,触发并运行它

    JobDetail job = JobBuilder.newJob(MoveFilesJob.class)
            .withIdentity("moveFileJob", "group1").build();


    Trigger trigger = TriggerBuilder
            .newTrigger()
            .withIdentity("moveFileTrigger", "group1")
            .withSchedule(
                    SimpleScheduleBuilder.simpleSchedule()
                    .withIntervalInMinutes(15).repeatForever())
            .build();

    Scheduler scheduler = new StdSchedulerFactory().getScheduler();
    scheduler.start();
    scheduler.scheduleJob(job, trigger);

#1


1  

There is an Executors framework in java SE since java 1.5.

自java 1.5以来,java SE中有一个Executors框架。

More specifically for your use case the class Executors contains method newSingleThreadScheduledExecutor(). Which will return new ScheduledExecutorService instance. Use that service to schedule some Runnable implementing work you need to do. Following code snippet illustrates the idea.

更具体地说,对于您的用例,类Executors包含方法newSingleThreadScheduledExecutor()。这将返回新的ScheduledExecutorService实例。使用该服务来安排您需要执行的一些Runnable实施工作。以下代码片段说明了这个想法。

private final ScheduledExecutorService scheduler =
   Executors.newSingleThreadScheduledExecutor();


 final Runnable myWorkingRunnable = new Runnable() {
   public void run() { System.out.println("Working"); }
 };

 final ScheduledFuture<?> workHandler =
   scheduler.scheduleAtFixedRate(myWorkingRunnable , 0, 15, TimeUnit.MINUTES);

You may use workHandler or scheduler to further manipulation with the service or scheduled Runnable.

您可以使用workHandler或scheduler来进一步操作服务或计划的Runnable。

#2


1  

You can set threads or use a scheduler like Quartz http://quartz-scheduler.org/

您可以设置线程或使用Quartz http://quartz-scheduler.org/等调度程序

You only need to schedule a task every 15 minutes with quartz, then move your files into the task

您只需要每15分钟使用quartz安排一个任务,然后将文件移动到任务中

First, import quartz dependency with maven

首先,用maven导入石英依赖

<dependency>
    <groupId>org.quartz-scheduler</groupId>
    <artifactId>quartz</artifactId>
    <version>2.2.1</version>
</dependency>

Then make a Job class

然后做一个Job类

public class MoveFilesJob implements Job {

    public void execute(JobExecutionContext context) throws JobExecutionException {
        // TODO move your files here

    }

}

Finally, make a trigger and run it

最后,触发并运行它

    JobDetail job = JobBuilder.newJob(MoveFilesJob.class)
            .withIdentity("moveFileJob", "group1").build();


    Trigger trigger = TriggerBuilder
            .newTrigger()
            .withIdentity("moveFileTrigger", "group1")
            .withSchedule(
                    SimpleScheduleBuilder.simpleSchedule()
                    .withIntervalInMinutes(15).repeatForever())
            .build();

    Scheduler scheduler = new StdSchedulerFactory().getScheduler();
    scheduler.start();
    scheduler.scheduleJob(job, trigger);