-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6b9e166
commit 8d64100
Showing
2 changed files
with
110 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package fr.abes.bestppn.kafka; | ||
|
||
import fr.abes.bestppn.exception.BestPpnException; | ||
import fr.abes.bestppn.exception.IllegalDoiException; | ||
import fr.abes.bestppn.service.EmailService; | ||
import fr.abes.bestppn.service.ExecutionReportService; | ||
import fr.abes.bestppn.service.KbartService; | ||
import lombok.SneakyThrows; | ||
import org.apache.kafka.clients.consumer.ConsumerRecord; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.net.URISyntaxException; | ||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
import java.util.concurrent.locks.ReentrantLock; | ||
|
||
public class Task implements Runnable { | ||
private final List<ConsumerRecord<String, String>> lignesKbartRecords; | ||
private final KbartService service; | ||
private final String providerName; | ||
private final boolean isForced; | ||
private volatile boolean stopped = false; | ||
private volatile boolean started = false; | ||
private volatile boolean finished = false; | ||
private final CompletableFuture<Long> completion = new CompletableFuture<>(); | ||
private final ReentrantLock startStopLock = new ReentrantLock(); | ||
private final AtomicLong currentOffset = new AtomicLong(); | ||
private Logger log = LoggerFactory.getLogger(Task.class); | ||
|
||
public Task(List<ConsumerRecord<String, String>> records, KbartService service, String providerName, boolean isForced) { | ||
this.lignesKbartRecords = records; | ||
this.service = service; | ||
this.providerName = providerName; | ||
this.isForced = isForced; | ||
} | ||
|
||
@SneakyThrows | ||
@Override | ||
public void run() { | ||
startStopLock.lock(); | ||
if (stopped){ | ||
return; | ||
} | ||
started = true; | ||
startStopLock.unlock(); | ||
|
||
for (ConsumerRecord<String, String> record : lignesKbartRecords) { | ||
if (stopped) | ||
break; | ||
// process record here and make sure you catch all exceptions; | ||
this.service.processConsumerRecord(record, this.providerName, this.isForced); | ||
currentOffset.set(record.offset() + 1); | ||
} | ||
finished = true; | ||
completion.complete(currentOffset.get()); | ||
} | ||
|
||
public long getCurrentOffset() { | ||
return currentOffset.get(); | ||
} | ||
|
||
public void stop() { | ||
startStopLock.lock(); | ||
this.stopped = true; | ||
if (!started) { | ||
finished = true; | ||
completion.complete(currentOffset.get()); | ||
} | ||
startStopLock.unlock(); | ||
} | ||
|
||
public long waitForCompletion() { | ||
try { | ||
return completion.get(); | ||
} catch (InterruptedException | ExecutionException e) { | ||
return -1; | ||
} | ||
} | ||
|
||
public boolean isFinished() { | ||
return finished; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters