Skip to content

Commit

Permalink
optimize WorkerFramelessMain code
Browse files Browse the repository at this point in the history
  • Loading branch information
ponfee committed Apr 5, 2024
1 parent b07cdf2 commit 7dd7e36
Show file tree
Hide file tree
Showing 29 changed files with 123 additions and 156 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
</div>
</div>
<div class="form-group">
<label class="col-xs-2 control-label" style="padding-right: 0;">Ctx-path:</label>
<label class="col-xs-2 control-label" style="padding-right: 0;">Context path:</label>
<div class="col-xs-10">
<input name="workerContextPath" th:field="*{workerContextPath}" class="form-control" type="text" autocomplete="off" />
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
}

function edit(group) {
$.modal.open("修改" + table.options.modalName, prefix + "/edit/" + group, '660', '430');
$.modal.open("修改" + table.options.modalName, prefix + "/edit/" + group, '680', '430');
}

function workers(group) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@ public static int getActualWebServerPort(WebServerApplicationContext webServerAp
return port;
}

// Ahead start web server for get actual port
WebServer webServer = webServerApplicationContext.getWebServer();
webServer.start();
// port=null -> default 8080
// port=0 -> random available port
return webServer.getPort();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package cn.ponfee.disjob.common.spring;

import cn.ponfee.disjob.common.base.Symbol.Char;
import cn.ponfee.disjob.common.base.Symbol.Str;
import cn.ponfee.disjob.common.collect.TypedMap;
import cn.ponfee.disjob.common.util.ClassUtils;
import cn.ponfee.disjob.common.util.Fields;
Expand Down Expand Up @@ -58,6 +59,11 @@ public <T> T extract(Class<T> beanType, String prefix) {
if (CollectionUtils.isEmpty(fields)) {
return null;
}

if (!prefix.isEmpty() && !prefix.endsWith(Str.DOT)) {
prefix += Str.DOT;
}

T bean = ClassUtils.newInstance(beanType);
char[] separators = {Char.HYPHEN, Char.DOT};
for (Field field : fields) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@
*/
@Getter
@Setter
@ConfigurationProperties(prefix = JobConstants.HTTP_KEY_PREFIX)
@ConfigurationProperties(prefix = HttpProperties.KEY_PREFIX)
public class HttpProperties extends ToJsonString implements Serializable {
private static final long serialVersionUID = 5956808059609905150L;
public static final String KEY_PREFIX = JobConstants.DISJOB_KEY_PREFIX + ".http";

/**
* Http rest connect timeout milliseconds, default 2000.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
*
* @author Ponfee
*/
public class JobConstants {
public final class JobConstants {

/**
* Version
Expand All @@ -47,31 +47,6 @@ public class JobConstants {
*/
public static final String DISJOB_BOUND_SERVER_HOST = DISJOB_KEY_PREFIX + ".bound.server.host";

/**
* Disjob server registry key prefix
*/
public static final String DISJOB_REGISTRY_KEY_PREFIX = DISJOB_KEY_PREFIX + ".registry";

/**
* Disjob worker configuration key prefix.
*/
public static final String WORKER_KEY_PREFIX = DISJOB_KEY_PREFIX + ".worker";

/**
* Disjob supervisor configuration key prefix.
*/
public static final String SUPERVISOR_KEY_PREFIX = DISJOB_KEY_PREFIX + ".supervisor";

/**
* Http rest configuration key prefix.
*/
public static final String HTTP_KEY_PREFIX = DISJOB_KEY_PREFIX + ".http";

/**
* Retry configuration key prefix.
*/
public static final String RETRY_KEY_PREFIX = DISJOB_KEY_PREFIX + ".retry";

/**
* Spring container bean name prefix.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@
*/
@Getter
@Setter
@ConfigurationProperties(prefix = JobConstants.RETRY_KEY_PREFIX)
@ConfigurationProperties(prefix = RetryProperties.KEY_PREFIX)
public class RetryProperties extends ToJsonString implements Serializable {
private static final long serialVersionUID = -2300492906607942870L;
public static final String KEY_PREFIX = JobConstants.DISJOB_KEY_PREFIX + ".retry";

/**
* Retry max count, default 3.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ public abstract class Server implements Serializable {
protected final int port;

protected Server(String host, int port) {
Assert.hasText(host, "Host cannot be empty.");
Assert.isTrue(!host.contains(Str.COLON), "Host cannot contains symbol ':'");
// -1 for test case
Assert.isTrue(0 < port && port <= 65535, "Port must be range (0, 65535].");
this.host = host;
this.host = host.trim();
this.port = port;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class Supervisor extends Server {
public Supervisor(String host, int port) {
super(host, port);

this.serializedValue = host + COLON + port;
this.serializedValue = super.host + COLON + super.port;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import cn.ponfee.disjob.common.base.SingletonClassConstraint;
import cn.ponfee.disjob.common.util.Numbers;
import cn.ponfee.disjob.common.util.Strings;
import cn.ponfee.disjob.core.exception.AuthenticationException;
import cn.ponfee.disjob.core.model.SchedJob;
import cn.ponfee.disjob.core.model.TokenType;
Expand Down Expand Up @@ -79,12 +78,14 @@ public class Worker extends Server {
public Worker(String group, String workerId, String host, int port) {
super(host, port);

Assert.hasText(group, "Group cannot be empty.");
Assert.hasText(workerId, "Worker id cannot be empty.");
Assert.isTrue(!group.contains(COLON), "Group cannot contains symbol ':'");
Assert.isTrue(!workerId.contains(COLON), "Worker id cannot contains symbol ':'");
this.group = Strings.requireNonBlank(group.trim());
this.workerId = workerId;
this.group = group.trim();
this.workerId = workerId.trim();

this.serializedValue = group + COLON + workerId + COLON + host + COLON + port;
this.serializedValue = this.group + COLON + this.workerId + COLON + super.host + COLON + super.port;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public HttpTaskDispatcher(ApplicationEventPublisher eventPublisher,

@Override
protected boolean doDispatch(ExecuteTaskParam param) {
return httpTaskReceiverClient.invoke(param.getWorker(), client -> client.receive(param));
return httpTaskReceiverClient.call(param.getWorker(), client -> client.receive(param));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package cn.ponfee.disjob.registry;

import cn.ponfee.disjob.common.base.ToJsonString;
import cn.ponfee.disjob.core.base.JobConstants;
import lombok.Getter;
import lombok.Setter;

Expand All @@ -32,6 +33,11 @@
public abstract class AbstractRegistryProperties extends ToJsonString implements Serializable {
private static final long serialVersionUID = -4967408535982883608L;

/**
* Disjob server registry key prefix
*/
public static final String DISJOB_REGISTRY_KEY_PREFIX = JobConstants.DISJOB_KEY_PREFIX + ".registry";

/**
* Registry namespace
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private DestinationServerInvoker(T remoteServiceClient, T localServiceProvider,
this.currentServer = currentServer;
}

public <R, E extends Throwable> R invoke(S destinationServer, ThrowingFunction<T, R, E> function) throws E {
public <R, E extends Throwable> R call(S destinationServer, ThrowingFunction<T, R, E> function) throws E {
Objects.requireNonNull(destinationServer);
if (localServiceProvider != null && destinationServer.equals(currentServer)) {
return function.apply(localServiceProvider);
Expand All @@ -80,8 +80,8 @@ public <R, E extends Throwable> R invoke(S destinationServer, ThrowingFunction<T
}
}

public <E extends Throwable> void invokeWithoutResult(S destinationServer, ThrowingConsumer<T, E> consumer) throws E {
invoke(destinationServer, consumer.toFunction(null));
public <E extends Throwable> void invoke(S destinationServer, ThrowingConsumer<T, E> consumer) throws E {
call(destinationServer, consumer.toFunction(null));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ private GroupedServerInvoker(T localServiceProvider, T remoteServiceClient, Pred
this.serverGroupMatcher = serverGroupMatcher;
}

public <R, E extends Throwable> R invoke(String group, ThrowingFunction<T, R, E> function) throws E {
public <R, E extends Throwable> R call(String group, ThrowingFunction<T, R, E> function) throws E {
if (localServiceProvider != null && serverGroupMatcher.test(group)) {
return function.apply(localServiceProvider);
} else {
Expand All @@ -153,8 +153,8 @@ public <R, E extends Throwable> R invoke(String group, ThrowingFunction<T, R, E>
}
}

public <E extends Throwable> void invokeWithoutResult(String group, ThrowingConsumer<T, E> consumer) throws E {
invoke(group, consumer.toFunction(null));
public <E extends Throwable> void invoke(String group, ThrowingConsumer<T, E> consumer) throws E {
call(group, consumer.toFunction(null));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package cn.ponfee.disjob.registry.consul.configuration;

import cn.ponfee.disjob.core.base.JobConstants;
import cn.ponfee.disjob.registry.AbstractRegistryProperties;
import lombok.Getter;
import lombok.Setter;
Expand All @@ -29,9 +28,10 @@
*/
@Getter
@Setter
@ConfigurationProperties(prefix = JobConstants.DISJOB_REGISTRY_KEY_PREFIX + ".consul")
@ConfigurationProperties(prefix = ConsulRegistryProperties.KEY_PREFIX)
public class ConsulRegistryProperties extends AbstractRegistryProperties {
private static final long serialVersionUID = -851364562631134942L;
public static final String KEY_PREFIX = DISJOB_REGISTRY_KEY_PREFIX + ".consul";

/**
* Consul client host
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package cn.ponfee.disjob.registry.database.configuration;

import cn.ponfee.disjob.core.base.JobConstants;
import cn.ponfee.disjob.registry.AbstractRegistryProperties;
import lombok.Getter;
import lombok.Setter;
Expand All @@ -31,9 +30,10 @@
*/
@Getter
@Setter
@ConfigurationProperties(prefix = JobConstants.DISJOB_REGISTRY_KEY_PREFIX + ".database")
@ConfigurationProperties(prefix = DatabaseRegistryProperties.KEY_PREFIX)
public class DatabaseRegistryProperties extends AbstractRegistryProperties {
private static final long serialVersionUID = -7144285250901660738L;
public static final String KEY_PREFIX = DISJOB_REGISTRY_KEY_PREFIX + ".database";

/**
* Session timeout milliseconds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package cn.ponfee.disjob.registry.etcd.configuration;

import cn.ponfee.disjob.core.base.JobConstants;
import cn.ponfee.disjob.registry.AbstractRegistryProperties;
import lombok.Getter;
import lombok.Setter;
Expand All @@ -32,9 +31,10 @@
*/
@Getter
@Setter
@ConfigurationProperties(prefix = JobConstants.DISJOB_REGISTRY_KEY_PREFIX + ".etcd")
@ConfigurationProperties(prefix = EtcdRegistryProperties.KEY_PREFIX)
public class EtcdRegistryProperties extends AbstractRegistryProperties {
private static final long serialVersionUID = -7448688693230439783L;
public static final String KEY_PREFIX = DISJOB_REGISTRY_KEY_PREFIX + ".etcd";

/**
* Server endpoints, multiple addresses separated by ","
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package cn.ponfee.disjob.registry.nacos.configuration;

import cn.ponfee.disjob.core.base.JobConstants;
import cn.ponfee.disjob.registry.AbstractRegistryProperties;
import lombok.Getter;
import lombok.Setter;
Expand All @@ -33,9 +32,10 @@
*/
@Getter
@Setter
@ConfigurationProperties(prefix = JobConstants.DISJOB_REGISTRY_KEY_PREFIX + ".nacos")
@ConfigurationProperties(prefix = NacosRegistryProperties.KEY_PREFIX)
public class NacosRegistryProperties extends AbstractRegistryProperties {
private static final long serialVersionUID = 2961908276104522907L;
public static final String KEY_PREFIX = DISJOB_REGISTRY_KEY_PREFIX + ".nacos";

/**
* Nacos server address
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package cn.ponfee.disjob.registry.redis.configuration;

import cn.ponfee.disjob.core.base.JobConstants;
import cn.ponfee.disjob.registry.AbstractRegistryProperties;
import lombok.Getter;
import lombok.Setter;
Expand All @@ -29,9 +28,10 @@
*/
@Getter
@Setter
@ConfigurationProperties(prefix = JobConstants.DISJOB_REGISTRY_KEY_PREFIX + ".redis")
@ConfigurationProperties(prefix = RedisRegistryProperties.KEY_PREFIX)
public class RedisRegistryProperties extends AbstractRegistryProperties {
private static final long serialVersionUID = -6079627443420731390L;
public static final String KEY_PREFIX = DISJOB_REGISTRY_KEY_PREFIX + ".redis";

/**
* Session timeout milliseconds
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package cn.ponfee.disjob.registry.zookeeper.configuration;

import cn.ponfee.disjob.core.base.JobConstants;
import cn.ponfee.disjob.registry.AbstractRegistryProperties;
import lombok.Getter;
import lombok.Setter;
Expand All @@ -31,9 +30,10 @@
*/
@Getter
@Setter
@ConfigurationProperties(prefix = JobConstants.DISJOB_REGISTRY_KEY_PREFIX + ".zookeeper")
@ConfigurationProperties(prefix = ZookeeperRegistryProperties.KEY_PREFIX)
public class ZookeeperRegistryProperties extends AbstractRegistryProperties {
private static final long serialVersionUID = -8395535372974631095L;
public static final String KEY_PREFIX = DISJOB_REGISTRY_KEY_PREFIX + ".zookeeper";

private String connectString = "localhost:2181";
private String username;
Expand Down
Loading

0 comments on commit 7dd7e36

Please sign in to comment.