forked from usdot-jpo-ode/jpo-cvmanager
-
Notifications
You must be signed in to change notification settings - Fork 3
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
Showing
12 changed files
with
131 additions
and
248 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
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
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
68 changes: 68 additions & 0 deletions
68
jpo-conflictvisualizer-api/src/main/java/us/dot/its/jpo/ode/api/topologies/BaseTopology.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,68 @@ | ||
package us.dot.its.jpo.ode.api.topologies; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import lombok.Getter; | ||
import org.apache.kafka.streams.KafkaStreams; | ||
import org.apache.kafka.streams.Topology; | ||
import org.apache.kafka.streams.errors.StreamsUncaughtExceptionHandler; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import us.dot.its.jpo.ode.api.controllers.StompController; | ||
|
||
import java.time.Duration; | ||
import java.util.Properties; | ||
|
||
/** | ||
* Default implementation of common functionality for topologies | ||
*/ | ||
public abstract class BaseTopology implements RestartableTopology{ | ||
|
||
protected abstract Logger getLogger(); | ||
protected abstract Topology buildTopology(); | ||
|
||
protected final Topology topology; | ||
protected KafkaStreams streams; | ||
|
||
@Getter | ||
protected final String topicName; | ||
|
||
protected final Properties streamsProperties; | ||
|
||
public BaseTopology(String topicName, Properties streamsProperties) { | ||
this.topicName = topicName; | ||
this.streamsProperties = streamsProperties; | ||
topology = buildTopology(); | ||
} | ||
|
||
@Override | ||
public void start() { | ||
if (streams != null && streams.state().isRunningOrRebalancing()) { | ||
throw new IllegalStateException("Start called while streams is already running."); | ||
} | ||
|
||
streams = new KafkaStreams(topology, streamsProperties); | ||
if (exceptionHandler != null) streams.setUncaughtExceptionHandler(exceptionHandler); | ||
if (stateListener != null) streams.setStateListener(stateListener); | ||
streams.start(); | ||
} | ||
|
||
@Override | ||
public void stop() { | ||
getLogger().info("Stopping topology for {}", topicName); | ||
if (streams != null) { | ||
// Trigger streams to shut down without blocking | ||
streams.close(Duration.ZERO); | ||
} | ||
} | ||
|
||
KafkaStreams.StateListener stateListener; | ||
public void registerStateListener(KafkaStreams.StateListener stateListener) { | ||
this.stateListener = stateListener; | ||
} | ||
|
||
StreamsUncaughtExceptionHandler exceptionHandler; | ||
public void registerUncaughtExceptionHandler(StreamsUncaughtExceptionHandler exceptionHandler) { | ||
this.exceptionHandler = exceptionHandler; | ||
} | ||
|
||
} |
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
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
Oops, something went wrong.