-
Notifications
You must be signed in to change notification settings - Fork 1
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
09800e2
commit 630f5a4
Showing
6 changed files
with
623 additions
and
0 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
cookbook/src/main/java/com/fluxtion/example/cookbook/racing/RaceCalculator.java
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,60 @@ | ||
package com.fluxtion.example.cookbook.racing; | ||
|
||
import com.fluxtion.runtime.annotations.ExportService; | ||
import com.fluxtion.runtime.annotations.OnEventHandler; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
import java.time.Instant; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class RaceCalculator { | ||
|
||
public record RunnerStarted(long runnerId, Instant startTime) { | ||
} | ||
|
||
public record RunnerFinished(long runnerId, Instant finishTime) { | ||
} | ||
|
||
public interface ResultsPublisher { | ||
void publishAllResults(); | ||
} | ||
|
||
@Getter | ||
public static class RaceTimeTracker { | ||
|
||
private final transient Map<Long, Long> runnerRaceTimeMap = new HashMap<>(); | ||
|
||
@OnEventHandler(propagate = false) | ||
public boolean runnerStarted(RunnerStarted runnerStarted) { | ||
//add runner start time to map | ||
return false; | ||
} | ||
|
||
@OnEventHandler | ||
public boolean runnerFinished(RunnerFinished runnerFinished) { | ||
//calc runner total race time and add to map | ||
return true; | ||
} | ||
} | ||
|
||
@RequiredArgsConstructor | ||
public static class ResultsPublisherImpl implements @ExportService ResultsPublisher{ | ||
|
||
private final RaceTimeTracker raceTimeTracker; | ||
|
||
@OnEventHandler(propagate = false) | ||
public boolean runnerFinished(RunnerFinished runnerFinished) { | ||
//get the runner race time and send individual their results | ||
long raceTime = raceTimeTracker.getRunnerRaceTimeMap().get(runnerFinished.runnerId()); | ||
return false; | ||
} | ||
|
||
@Override | ||
public void publishAllResults() { | ||
//get all results and publish | ||
var runnerRaceTimeMap = raceTimeTracker.getRunnerRaceTimeMap(); | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
cookbook/src/main/java/com/fluxtion/example/cookbook/racing/RaceCalculatorAotBuilder.java
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,22 @@ | ||
package com.fluxtion.example.cookbook.racing; | ||
|
||
import com.fluxtion.compiler.EventProcessorConfig; | ||
import com.fluxtion.compiler.FluxtionCompilerConfig; | ||
import com.fluxtion.compiler.FluxtionGraphBuilder; | ||
import com.fluxtion.example.cookbook.racing.RaceCalculator.RaceTimeTracker; | ||
import com.fluxtion.example.cookbook.racing.RaceCalculator.ResultsPublisherImpl; | ||
|
||
public class RaceCalculatorAotBuilder implements FluxtionGraphBuilder { | ||
@Override | ||
public void buildGraph(EventProcessorConfig eventProcessorConfig) { | ||
RaceTimeTracker raceCalculator = eventProcessorConfig.addNode(new RaceTimeTracker(), "raceCalculator"); | ||
eventProcessorConfig.addNode(new ResultsPublisherImpl(raceCalculator), "resultsPublisher"); | ||
} | ||
|
||
@Override | ||
public void configureGeneration(FluxtionCompilerConfig fluxtionCompilerConfig) { | ||
fluxtionCompilerConfig.setClassName("RaceCalculatorProcessor"); | ||
fluxtionCompilerConfig.setPackageName("com.fluxtion.example.cookbook.racing.generated"); | ||
} | ||
} | ||
|
28 changes: 28 additions & 0 deletions
28
cookbook/src/main/java/com/fluxtion/example/cookbook/racing/RaceCalculatorApp.java
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,28 @@ | ||
package com.fluxtion.example.cookbook.racing; | ||
|
||
import com.fluxtion.example.cookbook.racing.generated.RaceCalculatorProcessor; | ||
|
||
import java.time.Instant; | ||
|
||
import static com.fluxtion.example.cookbook.racing.RaceCalculator.*; | ||
|
||
public class RaceCalculatorApp { | ||
public static void main(String[] args) { | ||
RaceCalculatorProcessor raceCalculatorProcessor = new RaceCalculatorProcessor(); | ||
raceCalculatorProcessor.init(); | ||
|
||
ResultsPublisher resultsPublisher = raceCalculatorProcessor.getExportedService(); | ||
|
||
//connect to event stream and process runner timing events | ||
raceCalculatorProcessor.onEvent(new RunnerStarted(1, Instant.now())); | ||
raceCalculatorProcessor.onEvent(new RunnerStarted(2, Instant.now())); | ||
raceCalculatorProcessor.onEvent(new RunnerStarted(3, Instant.now())); | ||
|
||
raceCalculatorProcessor.onEvent(new RunnerFinished(2, Instant.now())); | ||
raceCalculatorProcessor.onEvent(new RunnerFinished(3, Instant.now())); | ||
raceCalculatorProcessor.onEvent(new RunnerFinished(1, Instant.now())); | ||
|
||
//publish full results | ||
resultsPublisher.publishAllResults(); | ||
} | ||
} |
Oops, something went wrong.