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.
Asked
Active
Viewed 198 times
-1
-
You need scheduled task and file copy. Which of these two is giving you problems ? – John Aug 20 '15 at 05:16
2 Answers
1
You can set threads or use a scheduler like Quartz http://quartz-scheduler.org/
You only need to schedule a task every 15 minutes with quartz, then move your files into the task
First, import quartz dependency with maven
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
Then make a Job class
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);

Jos
- 111
- 1
- 9
-
Quartz strikes me as overkill but would certainly work. But using a thread and sleep instead of executors in 2015 is inexcusable. – Voo Aug 20 '15 at 07:17
-
-
Actually that's exactly how this works: downvote bad answers, upvote good ones. If you removed the thread part and just showed hire the quartz solution actually worked (not just a link!) This would certainly qualify as a good alternative to the other answer and deserve an upvote. – Voo Aug 21 '15 at 09:55
-
1
There is an Executors framework in java SE since java 1.5.
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.
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
.

plastique
- 1,164
- 2
- 9
- 19
-
Thanks but how to process the data in to files which was coming from device. Since it is continuous process. Scheduler method will called only after 15 minutes but what about the data which was coming continuously coming from x device. – Nani Aug 20 '15 at 16:48
-
Please specify how the data are coming? Through stream? Or are they placed to some URL/Directory? And what exactly you mean by x device? – plastique Aug 20 '15 at 18:56