package test;
import java.io._;
import java.util._;
import java.util.concurrent.\*;
public class BatchJobExample {
public static void main(String[] args) {
// ScheduledExecutorService를 사용해 작업 스케줄링 (1분마다 실행)
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
Runnable batchTask = new Runnable() {
@Override
public void run() {
try {
System.out.println("Batch job started at: " + new Date());
List<Person> data = readDataFromFile("C:\\Users\\user\\eclipse-workspace_sl\\test\\src\\test\\simple-data.csv");
for (Person person : data) {
System.out.println(person);
}
System.out.println("Batch job finished at: " + new Date());
System.out.println("------------------------------------");
System.out.println();
} catch (IOException e) {
System.err.println("Error during batch execution: " + e.getMessage());
}
}
};
// 작업을 10초마다 실행하도록 스케줄링
scheduler.scheduleAtFixedRate(batchTask, 0, 3, TimeUnit.SECONDS);
}
// CSV 파일을 읽어 List<Person> 형태로 반환
private static List<Person> readDataFromFile(String fileName) throws IOException {
List<Person> persons = new ArrayList<>();
BufferedReader reader = new BufferedReader(new FileReader(fileName));
String line;
while ((line = reader.readLine()) != null) {
String[] tokens = line.split(",");
if (tokens.length == 2) {
persons.add(new Person(tokens[0], tokens[1]));
}
}
reader.close();
return persons;
}
}
class Person {
private String firstName;
private String lastName;
public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
@Override
public String toString() {
return "Person{firstName='" + firstName + "', lastName='" + lastName + "'}";
}
}
C:\Users\user\eclipse-workspace_sl\test\src\test\simple-data.csv" 위치에 저장 된 파일을 읽어 10초마다 콘솔에 출력한다.
'JAVA' 카테고리의 다른 글
| 자바 bigDecimal 사용방법 (0) | 2023.08.15 |
|---|---|
| 체크카드 매일 사용이벤트 가상구현 (0) | 2023.05.07 |
| [Java] Map보다 DTO 클래스를 사용해야 하는 이유 (FROM 망나니개발자) (0) | 2023.03.02 |
| JAVA BigDecimal > int 형변환 (0) | 2023.02.11 |