Skip to content
Open

9237 #18994

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 123 additions & 0 deletions netflix-modules/hollow/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<artifactId>hollow</artifactId>
<name>hollow</name>
<description>Module for Netflix Hollow</description>

<parent>
<groupId>com.baeldung</groupId>
<artifactId>netflix-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>

<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<maven.compiler.release>21</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<hollow.version>7.14.23</hollow.version>
<jupiter.version>5.9.3</jupiter.version>
</properties>

<dependencies>
<dependency>
<groupId>com.netflix.hollow</groupId>
<artifactId>hollow</artifactId>
<version>${hollow.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${jupiter.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<!-- Step 1: Compile producer code in generate-sources phase -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<executions>
<execution>
<id>compile-producer-for-api-generation</id>
<phase>generate-sources</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<includes>
<include>com/baeldung/hollow/model/**/*.java</include>
<include>com/baeldung/hollow/producer/**/*.java</include>
<include>com/baeldung/hollow/service/**/*.java</include>
</includes>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
</configuration>
</execution>
<execution>
<id>compile-all</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>

<!-- Step 2: Run API generator in process-sources phase (after generate-sources compilation) -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<?m2e execute onConfiguration,onIncremental?>
<id>generate-consumer-api</id>
<phase>process-sources</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<mainClass>com.baeldung.hollow.producer.ConsumerApiGenerator</mainClass>
<arguments>
<argument>${project.build.directory}/generated-sources</argument>
</arguments>
<includeProjectDependencies>true</includeProjectDependencies>
<classpathScope>compile</classpathScope>
</configuration>
</execution>
</executions>
</plugin>

<!-- Step 3: Add generated-sources folder as source (in process-sources phase) -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<id>add-generated-sources</id>
<phase>process-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated-sources</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.baeldung.hollow.consumer;

import com.baeldung.hollow.consumer.api.MonitoringEvent;
import com.baeldung.hollow.consumer.api.MonitoringEventAPI;
import com.netflix.hollow.api.consumer.HollowConsumer;
import com.netflix.hollow.api.consumer.fs.HollowFilesystemAnnouncementWatcher;
import com.netflix.hollow.api.consumer.fs.HollowFilesystemBlobRetriever;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;

public class MonitoringEventConsumer {
private final static Logger logger = LoggerFactory.getLogger(MonitoringEventConsumer.class);

static HollowConsumer consumer;
static HollowFilesystemAnnouncementWatcher announcementWatcher;
static HollowFilesystemBlobRetriever blobRetriever;
static MonitoringEventAPI monitoringEventAPI;

final static long POLL_INTERVAL_MILLISECONDS = 30000;

public static void main(String[] args) {
initialize(getSnapshotFilePath());
while (true) {
Collection<MonitoringEvent> events = monitoringEventAPI.getAllMonitoringEvent();
processEvents(events);
sleep(POLL_INTERVAL_MILLISECONDS);
}
}

private static void processEvents(Collection<MonitoringEvent> events) {
logger.info("Processing {} events", events.size());
events.forEach(evt -> {
logger.info("Event ID: {}, Name: {}, Type: {}, Status: {}, Device ID: {}, Creation Date: {}",
evt.getEventId(),
evt.getEventName().getValue(),
evt.getEventType().getValue(),
evt.getStatus().getValue(),
evt.getDeviceId().getValue(),
evt.getCreationDate().getValue());
});
}

private static void initialize(final Path snapshotPath) {
announcementWatcher = new HollowFilesystemAnnouncementWatcher(snapshotPath);
blobRetriever = new HollowFilesystemBlobRetriever(snapshotPath);
logger.info("snapshot data file location: {}", snapshotPath.toString());
consumer = new HollowConsumer.Builder<>()
.withAnnouncementWatcher(announcementWatcher)
.withBlobRetriever(blobRetriever)
.withGeneratedAPIClass(MonitoringEventAPI.class)
.build();
consumer.triggerRefresh();
monitoringEventAPI = consumer.getAPI(MonitoringEventAPI.class);
}

private static void sleep(long milliseconds) {
try {
Thread.sleep(milliseconds);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}

private static Path getSnapshotFilePath() {
String moduleDir = System.getProperty("user.dir");
String snapshotPath = moduleDir + "/.hollow/snapshots";
logger.info("snapshot data directory: {}", snapshotPath);

Path path = Paths.get(snapshotPath);
return path;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.baeldung.hollow.model;

import com.netflix.hollow.core.write.objectmapper.HollowPrimaryKey;

@HollowPrimaryKey(fields = "eventId")
public class MonitoringEvent {
private int eventId;
private String eventName;
private String creationDate;
private String eventType;
private String status;
private String deviceId;

public int getEventId() {
return eventId;
}

public void setEventId(int eventId) {
this.eventId = eventId;
}

public String getEventName() {
return eventName;
}

public void setEventName(String eventName) {
this.eventName = eventName;
}

public String getCreationDate() {
return creationDate;
}

public void setCreationDate(String creationDate) {
this.creationDate = creationDate;
}

public String getEventType() {
return eventType;
}

public void setEventType(String eventType) {
this.eventType = eventType;
}

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

public String getDeviceId() {
return deviceId;
}

public void setDeviceId(String deviceId) {
this.deviceId = deviceId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.baeldung.hollow.producer;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.baeldung.hollow.model.MonitoringEvent;
import com.netflix.hollow.api.codegen.HollowAPIGenerator;
import com.netflix.hollow.core.write.HollowWriteStateEngine;
import com.netflix.hollow.core.write.objectmapper.HollowObjectMapper;

public class ConsumerApiGenerator {
private static final Logger logger = LoggerFactory.getLogger(ConsumerApiGenerator.class);

public static void main(String[] args) {
String sourceDir = args[0];
Path outputPath = getGeneratedSourceDirectory(sourceDir);

HollowWriteStateEngine writeEngine = new HollowWriteStateEngine();
HollowObjectMapper mapper = new HollowObjectMapper(writeEngine);

mapper.initializeTypeState(MonitoringEvent.class);

logger.info("Starting HollowAPIGenerator with destination: {}", outputPath);

HollowAPIGenerator generator = new HollowAPIGenerator.Builder()
.withDestination(outputPath)
.withAPIClassname("MonitoringEventAPI")
.withPackageName("com.baeldung.hollow.consumer.api")
.withDataModel(writeEngine)
.build();
try {
generator.generateSourceFiles();
} catch (IOException e) {
logger.error("Error generating consumer API source files", e);
throw new RuntimeException(e);
}
}

private static Path getGeneratedSourceDirectory(String sourceDir) {
Path generatedSourceDir = Paths.get(sourceDir);
try {
Files.createDirectories(generatedSourceDir);
} catch (IOException e) {
logger.error("Unable to create output directory {}", generatedSourceDir, e);
throw new RuntimeException(e);
}

return generatedSourceDir;
}

}
Loading