Skip to content

Commit

Permalink
optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
ponfee committed Feb 5, 2024
1 parent 28103b5 commit e276439
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ spring.redis:

# 使用redis做注册中心
disjob.registry.redis:
namespace: redis_namespace
namespace: disjob_registry
session-timeout-ms: 60000
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static void main(String[] args) {

System.out.println("Embedded pszymczyk consul server starting...");
ConsulProcess consul = ConsulStarterBuilder.consulStarter()
.withConsulVersion("1.14.2")
.withConsulVersion("1.15.4")
.withConsulBinaryDownloadDirectory(createConsulBinaryDownloadDirectory())
.withHttpPort(8500)
.buildAndStart();
Expand All @@ -39,7 +39,7 @@ public static void main(String[] args) {
}

private static Path createConsulBinaryDownloadDirectory() {
File file = new File(MavenProjects.getProjectBaseDir() + "/target/consul/");
File file = new File(MavenProjects.getProjectBaseDir() + "/src/bin/consul/");
file.mkdirs();
return file.toPath();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,21 @@
* Embedded consul server based testcontainers.
* <a href="https://www.testcontainers.org/modules/consul/">testcontainers consul</a>
* 1、startup local docker environment
* 2、pull docker consul image: docker pull consul:1.14.2
* 2、pull docker consul image: docker pull consul:1.15.4
* 3、"consul:1.14.2" is docker {image-name:version}
*
* Other:
* 本地搜索:docker search consul --limit 20
* docker官网查看版本:https://hub.docker.com/_/consul/tags
*
* 查看latest的具体版本号:docker image inspect {image-name}:latest | grep -i version
* </pre>
*
* @author Ponfee
*/
public final class EmbeddedConsulServerTestcontainers {

private static final String CONSUL_DOCKER_IMAGE_NAME = "consul:1.14.2";
private static final String CONSUL_DOCKER_IMAGE_NAME = "consul:1.15.4";
private static final List<String> PORT_BINDINGS = Arrays.asList("8500:8500/tcp", "8502:8502/tcp");

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import io.etcd.jetcd.launcher.Etcd;
import io.etcd.jetcd.launcher.EtcdCluster;
import org.assertj.core.api.Assertions;

import java.util.Arrays;
import java.util.List;
Expand All @@ -25,7 +26,7 @@
public final class EmbeddedEtcdServerTestcontainers {

private static final String ETCD_DOCKER_IMAGE_NAME = "gcr.io/etcd-development/etcd:v3.5.4";
private static final List<String> PORT_BINDINGS = Arrays.asList("2379:2379", "2380:2380", "8080:8080");
private static final List<String> PORT_BINDINGS = Arrays.asList("2379:2379/tcp", "2380:2380/tcp", "8080:8080/tcp");

public static void main(String[] args) throws Exception {
EtcdCluster etcd = Etcd.builder()
Expand All @@ -42,6 +43,8 @@ public static void main(String[] args) throws Exception {
try {
System.out.println("Embedded docker etcd server starting...");
etcd.start();
Assertions.assertThat(etcd.containers()).hasSize(1);
Assertions.assertThat(etcd.containers().get(0).getPortBindings()).hasSameElementsAs(PORT_BINDINGS);
System.out.println("Embedded docker etcd server started!");
new CountDownLatch(1).await();
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

package cn.ponfee.disjob.samples.worker;

import cn.ponfee.disjob.common.base.LazyLoader;
import cn.ponfee.disjob.common.base.TimingWheel;
import cn.ponfee.disjob.common.collect.Collects;
import cn.ponfee.disjob.common.exception.Throwables.ThrowingRunnable;
Expand Down Expand Up @@ -63,8 +64,6 @@ public class WorkerFramelessMain {

private static final Logger LOG = LoggerFactory.getLogger(WorkerFramelessMain.class);

private static StringRedisTemplate stringRedisTemplate = null;

public static void main(String[] args) throws Exception {
printBanner();

Expand All @@ -91,14 +90,17 @@ public static void main(String[] args) throws Exception {
props.getInt(WORKER_KEY_PREFIX + ".timing-wheel-ring-size", 60)
);

LazyLoader<StringRedisTemplate> stringRedisTemplateLoader = LazyLoader.of(
() -> AbstractRedisTemplateCreator.create(DISJOB_KEY_PREFIX + ".redis.", props, null).getStringRedisTemplate()
);



// --------------------- create registry(select redis or consul) --------------------- //
WorkerRegistry workerRegistry;
{
// redis registry
workerRegistry = createRedisWorkerRegistry(JobConstants.DISJOB_REGISTRY_KEY_PREFIX + ".redis", props);
workerRegistry = createRedisWorkerRegistry(JobConstants.DISJOB_REGISTRY_KEY_PREFIX + ".redis", props, stringRedisTemplateLoader);

// consul registry
//workerRegistry = createConsulWorkerRegistry(JobConstants.DISJOB_REGISTRY_KEY_PREFIX + ".consul", props);
Expand All @@ -114,7 +116,7 @@ public static void main(String[] args) throws Exception {
WorkerRpcService workerRpcProvider = new WorkerRpcProvider(currentWorker, workerRegistry);
{
// redis dispatching
//taskReceiver = new RedisTaskReceiver(currentWorker, timingWheel, stringRedisTemplate) {
//taskReceiver = new RedisTaskReceiver(currentWorker, timingWheel, stringRedisTemplateLoader.get()) {
// @Override
// public boolean receive(ExecuteTaskParam param) {
// JobHandlerParser.parse(param, "jobHandler");
Expand Down Expand Up @@ -175,11 +177,12 @@ private static void printBanner() throws IOException {
System.out.println(banner);
}

private static WorkerRegistry createRedisWorkerRegistry(String keyPrefix, YamlProperties props) {
private static WorkerRegistry createRedisWorkerRegistry(String keyPrefix, YamlProperties props,
LazyLoader<StringRedisTemplate> stringRedisTemplateLoader) {
RedisRegistryProperties config = new RedisRegistryProperties();
config.setNamespace(props.getString(keyPrefix + ".namespace"));
config.setSessionTimeoutMs(props.getLong(keyPrefix + ".session-timeout-ms", 30000));
return new RedisWorkerRegistry(stringRedisTemplate(props), config);
return new RedisWorkerRegistry(stringRedisTemplateLoader.get(), config);
}

/*
Expand All @@ -193,12 +196,4 @@ private static WorkerRegistry createConsulWorkerRegistry(String keyPrefix, YamlP
}
*/

private static synchronized StringRedisTemplate stringRedisTemplate(YamlProperties props) {
if (stringRedisTemplate == null) {
String keyPrefix = DISJOB_KEY_PREFIX + ".redis.";
stringRedisTemplate = AbstractRedisTemplateCreator.create(keyPrefix, props, null).getStringRedisTemplate();
}
return stringRedisTemplate;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ disjob:
registry:
# redis做注册中心时需要配置
redis:
namespace: redis_namespace
namespace: disjob_registry
session-timeout-ms: 60000
# consul做注册中心时需要配置
consul:
namespace: consul_namespace
namespace: disjob_registry
host: localhost
port: 8500
token:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ spring.redis:

# 使用redis做注册中心
disjob.registry.redis:
namespace: redis_namespace
namespace: disjob_registry
session-timeout-ms: 60000
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public static void main(String[] args) throws Exception {
new org.h2.server.TcpServer().start();
//new org.h2.server.web.WebServer().start();
//new org.h2.server.pg.PgServer().start();
System.out.println("Embedded h2 database started!");

JdbcTemplate jdbcTemplate = DBUtils.createJdbcTemplate(jdbcUrl, username, password);

Expand All @@ -62,6 +61,7 @@ public static void main(String[] args) throws Exception {

System.out.println("\n--------------------------------------------------------testScript");
testScript(jdbcTemplate);
System.out.println("Embedded h2 database started!");

new CountDownLatch(1).await();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ public static void main(String[] args) throws Exception {

System.out.println("Embedded docker mysql starting...");
mySQLContainer.start();
System.out.println("Embedded docker mysql started!");
//mySQLContainer.execInContainer("mysqld --skip-grant-tables");

// scriptPath只用于打印日志,此处直接设置为空字符串
Expand All @@ -78,6 +77,7 @@ public static void main(String[] args) throws Exception {
System.out.println("\n--------------------------------------------------------testQuerySql");
DBUtils.testQuerySchedJob(jdbcTemplate);

System.out.println("Embedded docker mysql started!");
new CountDownLatch(1).await();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private void process() {
// check has available supervisors
if (!discoverySupervisor.hasDiscoveredServers()) {
if (System.currentTimeMillis() > nextLogTimeMillis) {
nextLogTimeMillis = System.currentTimeMillis() + 5000L;
this.nextLogTimeMillis = System.currentTimeMillis() + 5000L;
LOG.warn("Not found available supervisor.");
}
return;
Expand Down

0 comments on commit e276439

Please sign in to comment.