Skip to content

Commit

Permalink
Provide a WorkerOptions to plug in a worker executor service in Deplo…
Browse files Browse the repository at this point in the history
…ymentOptions. The current worker pool settings are retrofitted in a WorkerPoolOptions implementing WorkerOptions.
  • Loading branch information
vietj committed Sep 5, 2023
1 parent 839c6e7 commit 377598f
Show file tree
Hide file tree
Showing 12 changed files with 409 additions and 77 deletions.
61 changes: 61 additions & 0 deletions src/main/generated/io/vertx/core/WorkerPoolOptionsConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package io.vertx.core;

import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.impl.JsonUtil;
import java.time.Instant;
import java.time.format.DateTimeFormatter;
import java.util.Base64;

/**
* Converter and mapper for {@link io.vertx.core.WorkerPoolOptions}.
* NOTE: This class has been automatically generated from the {@link io.vertx.core.WorkerPoolOptions} original class using Vert.x codegen.
*/
public class WorkerPoolOptionsConverter {


private static final Base64.Decoder BASE64_DECODER = JsonUtil.BASE64_DECODER;
private static final Base64.Encoder BASE64_ENCODER = JsonUtil.BASE64_ENCODER;

static void fromJson(Iterable<java.util.Map.Entry<String, Object>> json, WorkerPoolOptions obj) {
for (java.util.Map.Entry<String, Object> member : json) {
switch (member.getKey()) {
case "maxExecuteTime":
if (member.getValue() instanceof Number) {
obj.setMaxExecuteTime(((Number)member.getValue()).longValue());
}
break;
case "maxExecuteTimeUnit":
if (member.getValue() instanceof String) {
obj.setMaxExecuteTimeUnit(java.util.concurrent.TimeUnit.valueOf((String)member.getValue()));
}
break;
case "name":
if (member.getValue() instanceof String) {
obj.setName((String)member.getValue());
}
break;
case "size":
if (member.getValue() instanceof Number) {
obj.setSize(((Number)member.getValue()).intValue());
}
break;
}
}
}

static void toJson(WorkerPoolOptions obj, JsonObject json) {
toJson(obj, json.getMap());
}

static void toJson(WorkerPoolOptions obj, java.util.Map<String, Object> json) {
json.put("maxExecuteTime", obj.getMaxExecuteTime());
if (obj.getMaxExecuteTimeUnit() != null) {
json.put("maxExecuteTimeUnit", obj.getMaxExecuteTimeUnit().name());
}
if (obj.getName() != null) {
json.put("name", obj.getName());
}
json.put("size", obj.getSize());
}
}
59 changes: 33 additions & 26 deletions src/main/java/io/vertx/core/DeploymentOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,12 @@ public class DeploymentOptions {

private JsonObject config;
private boolean worker;
private WorkerOptions workerOptions;
private String isolationGroup;
private String workerPoolName;
private int workerPoolSize;
private long maxWorkerExecuteTime;
private boolean ha;
private List<String> extraClasspath;
private int instances;
private List<String> isolatedClasses;
private TimeUnit maxWorkerExecuteTimeUnit;
private ClassLoader classLoader;

/**
Expand All @@ -56,10 +53,7 @@ public DeploymentOptions() {
this.isolationGroup = null;
this.ha = DEFAULT_HA;
this.instances = DEFAULT_INSTANCES;
this.workerPoolName = null;
this.workerPoolSize = VertxOptions.DEFAULT_WORKER_POOL_SIZE;
this.maxWorkerExecuteTime = VertxOptions.DEFAULT_MAX_WORKER_EXECUTE_TIME;
this.maxWorkerExecuteTimeUnit = VertxOptions.DEFAULT_MAX_WORKER_EXECUTE_TIME_UNIT;
this.workerOptions = new WorkerPoolOptions();
}

/**
Expand All @@ -75,10 +69,7 @@ public DeploymentOptions(DeploymentOptions other) {
this.extraClasspath = other.getExtraClasspath() == null ? null : new ArrayList<>(other.getExtraClasspath());
this.instances = other.instances;
this.isolatedClasses = other.getIsolatedClasses() == null ? null : new ArrayList<>(other.getIsolatedClasses());
this.workerPoolName = other.workerPoolName;
setWorkerPoolSize(other.workerPoolSize);
setMaxWorkerExecuteTime(other.maxWorkerExecuteTime);
this.maxWorkerExecuteTimeUnit = other.maxWorkerExecuteTimeUnit;
this.workerOptions = other.workerOptions.copy();
}

/**
Expand Down Expand Up @@ -268,11 +259,33 @@ public DeploymentOptions setIsolatedClasses(List<String> isolatedClasses) {
return this;
}

/**
* @return the worker options
*/
public WorkerOptions getWorkerOptions() {
return workerOptions;
}

/**
* Set the verticle worker options.
*
* @param workerOptions the worker options to use
* @return a reference to this, so the API can be used fluently
*/
public DeploymentOptions setWorkerOptions(WorkerOptions workerOptions) {
this.workerOptions = workerOptions;
return this;
}

private WorkerPoolOptions assumeWorkerPool() {
return (WorkerPoolOptions) workerOptions;
}

/**
* @return the worker pool name
*/
public String getWorkerPoolName() {
return workerPoolName;
return assumeWorkerPool().getName();
}

/**
Expand All @@ -283,7 +296,7 @@ public String getWorkerPoolName() {
* @return a reference to this, so the API can be used fluently
*/
public DeploymentOptions setWorkerPoolName(String workerPoolName) {
this.workerPoolName = workerPoolName;
assumeWorkerPool().setName(workerPoolName);
return this;
}

Expand All @@ -298,7 +311,7 @@ public DeploymentOptions setWorkerPoolName(String workerPoolName) {
* @return the maximum number of worker threads
*/
public int getWorkerPoolSize() {
return workerPoolSize;
return assumeWorkerPool().getSize();
}

/**
Expand All @@ -310,10 +323,7 @@ public int getWorkerPoolSize() {
* @return a reference to this, so the API can be used fluently
*/
public DeploymentOptions setWorkerPoolSize(int workerPoolSize) {
if (workerPoolSize < 1) {
throw new IllegalArgumentException("workerPoolSize must be > 0");
}
this.workerPoolSize = workerPoolSize;
assumeWorkerPool().setSize(workerPoolSize);
return this;
}

Expand All @@ -330,7 +340,7 @@ public DeploymentOptions setWorkerPoolSize(int workerPoolSize) {
* @return The value of max worker execute time, the default value of {@link DeploymentOptions#setMaxWorkerExecuteTimeUnit} {@code maxWorkerExecuteTimeUnit} is {@link TimeUnit#NANOSECONDS}
*/
public long getMaxWorkerExecuteTime() {
return maxWorkerExecuteTime;
return assumeWorkerPool().getMaxExecuteTime();
}

/**
Expand All @@ -344,10 +354,7 @@ public long getMaxWorkerExecuteTime() {
* @return a reference to this, so the API can be used fluently
*/
public DeploymentOptions setMaxWorkerExecuteTime(long maxWorkerExecuteTime) {
if (maxWorkerExecuteTime < 1) {
throw new IllegalArgumentException("maxWorkerExecuteTime must be > 0");
}
this.maxWorkerExecuteTime = maxWorkerExecuteTime;
assumeWorkerPool().setMaxExecuteTime(maxWorkerExecuteTime);
return this;
}

Expand All @@ -357,7 +364,7 @@ public DeploymentOptions setMaxWorkerExecuteTime(long maxWorkerExecuteTime) {
* @return the time unit of {@code maxWorkerExecuteTime}
*/
public TimeUnit getMaxWorkerExecuteTimeUnit() {
return maxWorkerExecuteTimeUnit;
return assumeWorkerPool().getMaxExecuteTimeUnit();
}

/**
Expand All @@ -369,7 +376,7 @@ public TimeUnit getMaxWorkerExecuteTimeUnit() {
* @return a reference to this, so the API can be used fluently
*/
public DeploymentOptions setMaxWorkerExecuteTimeUnit(TimeUnit maxWorkerExecuteTimeUnit) {
this.maxWorkerExecuteTimeUnit = maxWorkerExecuteTimeUnit;
assumeWorkerPool().setMaxExecuteTimeUnit(maxWorkerExecuteTimeUnit);
return this;
}

Expand Down
38 changes: 38 additions & 0 deletions src/main/java/io/vertx/core/WorkerOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2011-2023 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*/
package io.vertx.core;

import io.vertx.codegen.annotations.DataObject;
import io.vertx.codegen.annotations.Unstable;

import java.util.concurrent.ExecutorService;

/**
* Generic worker options defining the characteristics of a worker service.
*/
@Unstable
@DataObject
public interface WorkerOptions {

/**
* Create the executor service that implements this worker options.
*
* @param vertx the vertx instance
* @return the executor service implementing these worker options
*/
ExecutorService createExecutor(Vertx vertx);

/**
* @return a copy of this object
*/
WorkerOptions copy();

}
Loading

0 comments on commit 377598f

Please sign in to comment.