Skip to content

Commit

Permalink
feat: add configuration to set default threading model for verticles
Browse files Browse the repository at this point in the history
  • Loading branch information
kristian committed Jan 17, 2024
1 parent a413e33 commit ccc4fbd
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public class NeonBeeConfigConverter {
static void fromJson(Iterable<java.util.Map.Entry<String, Object>> json, NeonBeeConfig obj) {
for (java.util.Map.Entry<String, Object> member : json) {
switch (member.getKey()) {
case "defaultThreadingModel":
if (member.getValue() instanceof String) {
obj.setDefaultThreadingModel(io.vertx.core.ThreadingModel.valueOf((String) member.getValue()));
}
break;
case "deploymentTimeout":
if (member.getValue() instanceof Number) {
obj.setDeploymentTimeout(((Number) member.getValue()).intValue());
Expand Down Expand Up @@ -111,6 +116,9 @@ static void toJson(NeonBeeConfig obj, JsonObject json) {
}

static void toJson(NeonBeeConfig obj, java.util.Map<String, Object> json) {
if (obj.getDefaultThreadingModel() != null) {
json.put("defaultThreadingModel", obj.getDefaultThreadingModel().name());
}
json.put("deploymentTimeout", obj.getDeploymentTimeout());
if (obj.getEventBusCodecs() != null) {
JsonObject map = new JsonObject();
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/io/neonbee/config/NeonBeeConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
import io.vertx.codegen.annotations.DataObject;
import io.vertx.codegen.annotations.Fluent;
import io.vertx.codegen.json.annotations.JsonGen;
import io.vertx.core.DeploymentOptions;
import io.vertx.core.Future;
import io.vertx.core.ThreadingModel;
import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
import io.vertx.core.metrics.MetricsOptions;
Expand All @@ -52,6 +54,11 @@ public class NeonBeeConfig {
*/
public static final int DEFAULT_DEPLOYMENT_TIMEOUT = 30;

/**
* The default threading model for deploying verticles (event loop).
*/
public static final ThreadingModel DEFAULT_THREADING_MODEL = DeploymentOptions.DEFAULT_MODE;

/**
* The default tracking data handling strategy.
*/
Expand All @@ -75,6 +82,8 @@ public class NeonBeeConfig {

private Integer verticleDeploymentTimeout;

private ThreadingModel defaultThreadingModel = DEFAULT_THREADING_MODEL;

private Map<String, String> eventBusCodecs = Map.of();

private String trackingDataHandlingStrategy = DEFAULT_TRACKING_DATA_HANDLING_STRATEGY;
Expand Down Expand Up @@ -362,6 +371,28 @@ public NeonBeeConfig setVerticleDeploymentTimeout(Integer verticleDeploymentTime
return this;
}

/**
* Get the default threading model to be used when deploying verticles.
*
* @return the default threading model for verticles
*/
public ThreadingModel getDefaultThreadingModel() {
return defaultThreadingModel;
}

/**
* Set the default threading model to be used when deploying verticles. Can be overridden either on runtime or using
* the verticle configuration.
*
* @param threadingModel the threading model to use when deploying verticles
* @return the {@linkplain NeonBeeConfig} for fluent use
*/
@Fluent
public NeonBeeConfig setDefaultThreadingModel(ThreadingModel threadingModel) {
this.defaultThreadingModel = threadingModel;
return this;
}

/**
* Gets a list of default codecs to register on the event bus.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,11 @@ static Future<DeploymentOptions> readVerticleConfig(Vertx vertx, String classNam
: config; // if there is no default config, or what we read was the default config return it
}).onFailure(throwable -> {
LOGGER.warn("Could not read deployment options for deployable {}", className, throwable);
}).onSuccess(config -> {
if (!config.containsKey("threadingModel")) {
config.put("threadingModel", NeonBee.get(vertx).getConfig()
.getDefaultThreadingModel().toString());
}
}).map(DeploymentOptions::new);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import io.neonbee.internal.scanner.ClassPathScanner;
import io.vertx.core.DeploymentOptions;
import io.vertx.core.Future;
import io.vertx.core.ThreadingModel;
import io.vertx.core.Verticle;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
Expand Down Expand Up @@ -206,6 +207,24 @@ void testReadVerticleConfigFailure() throws IOException {
.isEqualTo("test");
}

@Test
@DisplayName("test read verticle config threading model")
void testReadVerticleConfigThreadingModel() throws IOException {
NeonBee neonBeeMock = newNeonBeeMockForDeployment(
new NeonBeeOptions.Mutable().setWorkingDirectory(Path.of("")));
Vertx vertxMock = neonBeeMock.getVertx();
FileSystem fileSystemMock = vertxMock.fileSystem();

when(fileSystemMock.readFile(any()))
.thenReturn(failedFuture(new FileSystemException(new NoSuchFileException("file"))));
when(fileSystemMock.readFile(endsWith(".yml")))
.thenReturn(succeededFuture(Buffer.buffer("---\nthreadingModel: WORKER")));

JsonObject defaultObject = new JsonObject().put("threadingModel", "EVENT_LOOP");
assertThat(DeployableVerticle.readVerticleConfig(vertxMock, "test", defaultObject).result().toJson())
.isEqualTo(new DeploymentOptions().setThreadingModel(ThreadingModel.WORKER).toJson());
}

@Test
@DisplayName("test scan class path")
@SuppressWarnings("rawtypes")
Expand Down

0 comments on commit ccc4fbd

Please sign in to comment.