Skip to content

Commit

Permalink
optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
ponfee committed Feb 15, 2024
1 parent 6b3fcef commit ae99e00
Show file tree
Hide file tree
Showing 25 changed files with 124 additions and 67 deletions.
4 changes: 2 additions & 2 deletions README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ disjob # Main project①
- [Start the Worker Spring-boot application](disjob-samples/disjob-samples-springboot-worker/src/main/java/cn/ponfee/disjob/samples/worker/WorkerApplication.java)
- [Start the Supervisor+Worker Spring-boot application](disjob-samples/disjob-samples-springboot-merged/src/main/java/cn/ponfee/disjob/samples/merged/MergedApplication.java)
- [Start the Supervisor Spring-boot application](disjob-samples/disjob-samples-springboot-supervisor/src/main/java/cn/ponfee/disjob/samples/supervisor/SupervisorApplication.java)
- [Start the Admin Spring-boot application](disjob-admin/ruoyi-admin/src/main/java/com/ruoyi/RuoYiApplication.java)
- [Start the Admin Spring-boot application](disjob-admin/ruoyi-admin/src/main/java/com/ruoyi/DisjobAdminApplication.java)

3. Login to the Admin

Expand Down Expand Up @@ -149,7 +149,7 @@ disjob # Main project①
```yaml
disjob.registry.consul:
namespace: consul_namespace
namespace: disjob_registry
host: localhost
port: 8500
token:
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ disjob # 主项目①
- [启动Worker单独部署的普通Java-main应用](disjob-samples/disjob-samples-frameless-worker/src/main/java/cn/ponfee/disjob/samples/worker/WorkerFramelessMain.java)
- [启动Supervisor+Worker合并部署的Spring-boot应用](disjob-samples/disjob-samples-springboot-merged/src/main/java/cn/ponfee/disjob/samples/merged/MergedApplication.java)
- [启动Supervisor单独部署的Spring-boot应用](disjob-samples/disjob-samples-springboot-supervisor/src/main/java/cn/ponfee/disjob/samples/supervisor/SupervisorApplication.java)
- [启动Admin管理后台的Spring-boot应用](disjob-admin/ruoyi-admin/src/main/java/com/ruoyi/RuoYiApplication.java)
- [启动Admin管理后台的Spring-boot应用](disjob-admin/ruoyi-admin/src/main/java/com/ruoyi/DisjobAdminApplication.java)

3. 登录管理后台

Expand Down Expand Up @@ -168,7 +168,7 @@ disjob # 主项目①
```yaml
disjob.registry.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 @@ -303,7 +303,7 @@ <h2 tabindex="-1" dir="auto"><a id="user-content-quick-start" class="anchor" ari
<li><a href="https://github.com/dromara/disjob/blob/master/disjob-samples/disjob-samples-frameless-worker/src/main/java/cn/ponfee/disjob/samples/worker/WorkerFramelessMain.java">启动Worker单独部署的普通Java-main应用</a></li>
<li><a href="https://github.com/dromara/disjob/blob/master/disjob-samples/disjob-samples-springboot-merged/src/main/java/cn/ponfee/disjob/samples/merged/MergedApplication.java">启动Supervisor+Worker合并部署的Spring-boot应用</a></li>
<li><a href="https://github.com/dromara/disjob/blob/master/disjob-samples/disjob-samples-springboot-supervisor/src/main/java/cn/ponfee/disjob/samples/supervisor/SupervisorApplication.java">启动Supervisor单独部署的Spring-boot应用</a></li>
<li><a href="https://github.com/dromara/disjob/blob/master/disjob-admin/ruoyi-admin/src/main/java/com/ruoyi/RuoYiApplication.java">启动Admin管理后台的Spring-boot应用</a></li>
<li><a href="https://github.com/dromara/disjob/blob/master/disjob-admin/ruoyi-admin/src/main/java/com/ruoyi/DisjobAdminApplication.java">启动Admin管理后台的Spring-boot应用</a></li>
</ul>
<ol start="3" dir="auto">
<li>登录管理后台</li>
Expand Down Expand Up @@ -380,11 +380,11 @@ <h2 tabindex="-1" dir="auto"><a id="user-content-user-guide" class="anchor" aria
<p dir="auto">例如:若使用Consul做配置中心时,可加如下配置</p>
</blockquote>
<div class="highlight highlight-source-yaml notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="disjob.registry.consul:
namespace: consul_namespace
namespace: disjob_registry
host: localhost
port: 8500
token:"><pre><span class="pl-ent">disjob.registry.consul</span>:
<span class="pl-ent">namespace</span>: <span class="pl-s">consul_namespace</span>
<span class="pl-ent">namespace</span>: <span class="pl-s">disjob_registry</span>
<span class="pl-ent">host</span>: <span class="pl-s">localhost</span>
<span class="pl-ent">port</span>: <span class="pl-c1">8500</span>
<span class="pl-ent">token</span>:</pre></div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

import static cn.ponfee.disjob.supervisor.dao.SupervisorDataSourceConfig.JDBC_TEMPLATE_SPRING_BEAN_NAME;
import static cn.ponfee.disjob.supervisor.dao.SupervisorDataSourceConfig.SPRING_BEAN_NAME_JDBC_TEMPLATE;

/**
* Disjob admin configuration
Expand All @@ -42,11 +42,11 @@
@ComponentScan("cn.ponfee.disjob.test.handler") // 加载一些测试的JobHandler,只用于demo演示使用(开发时建议删掉这行)
@EnableJacksonDateConfigurer // 解决日期反序列化报错的问题
@EnableSupervisor // disjob-admin必须启用Supervisor角色,即:必须加@EnableSupervisor注解
@EnableWorker // 若要取消worker角色可去掉@EnableWorker注解(生产建议Supervisor与Worker分开部署)
@EnableWorker // 若要取消worker角色可去掉@EnableWorker注解(生产建议Supervisor与Worker分开部署,即去掉@EnableWorker注解)
public class DisjobAdminConfiguration {

@Bean
public IdGenerator idGenerator(@Qualifier(JDBC_TEMPLATE_SPRING_BEAN_NAME) JdbcTemplate jdbcTemplate,
public IdGenerator idGenerator(@Qualifier(SPRING_BEAN_NAME_JDBC_TEMPLATE) JdbcTemplate jdbcTemplate,
@Value("${" + JobConstants.SPRING_WEB_SERVER_PORT + "}") int port,
@Value("${" + JobConstants.DISJOB_BOUND_SERVER_HOST + ":}") String boundHost) {
// serverTag = host:port
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public static <T> T execute(ThrowingSupplier<T, Throwable> action, int retryMaxC
return action.get();
} catch (InterruptedException e) {
LOG.error("Thread interrupted, skip retry.");
Thread.currentThread().interrupt();
throw e;
} catch (Throwable e) {
ex = e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ public final class ThreadPoolExecutors {
try {
executor.getQueue().put(task);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
ExceptionUtils.rethrow(e);
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ public void lock() {
Thread.sleep(computeSleepMillis(round));
} catch (InterruptedException e) {
LOG.error("Redis lock sleep occur interrupted exception.", e);
Thread.currentThread().interrupt();
ExceptionUtils.rethrow(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ private List<T> buildPath(List<T> parentPath, T nid) {
return null;
}

// already check duplicated, so cannot happen has circular dependencies state
// already check duplicated, so cannot happen exists circular dependencies
/*
if (IterableUtils.matchesAny(parentPath, nid::equals)) {
// 节点路径中已经包含了此节点,则视为环状
Expand All @@ -502,7 +502,8 @@ private <E extends TreeTrait<T, A, E>> void convert(Function<TreeNode<T, A>, E>

List<E> list = new LinkedList<>();
for (TreeNode<T, A> child : children) {
if (child.available || containsUnavailable) { // filter unavailable
// filter unavailable
if (child.available || containsUnavailable) {
E node = convert.apply(child);
child.convert(convert, node, containsUnavailable);
list.add(node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,10 @@ public static boolean isReachableHost(String host) {
// ignored
}

// https://stackoverflow.com/questions/11506321/how-to-ping-an-ip-address
boolean isWindows = org.apache.commons.lang3.SystemUtils.IS_OS_WINDOWS;
try {
Process process = Runtime.getRuntime().exec("ping -c 1 " + host);
Process process = Runtime.getRuntime().exec(String.format("ping -%s 1 %s", isWindows ? "n" : "c", host));
boolean exited = process.waitFor(PING_TIMEOUT, TimeUnit.MILLISECONDS);
return exited && process.exitValue() == 0;
} catch (Exception ignored) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2022-2024 Ponfee (http://www.ponfee.cn/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cn.ponfee.disjob.common.date;

import com.google.common.collect.Lists;
Expand All @@ -14,6 +30,11 @@

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* DateFormat Test
*
* @author Ponfee
*/
public class DateFormatTest {

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2022-2024 Ponfee (http://www.ponfee.cn/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cn.ponfee.disjob.common.date;

import cn.ponfee.disjob.common.util.Bytes;
Expand All @@ -9,6 +25,11 @@
import java.text.ParseException;
import java.util.Date;

/**
* DatePeriods Test
*
* @author Ponfee
*/
public class DatePeriodsTest {

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
@Disabled
public class CopyrightTest {

public static final String CURRENT_FILE_CLASS = "\npublic class " + CopyrightTest.class.getSimpleName() + " {\n";
private static final String OLD_COPYRIGHT_KEYWORD = "\n * Copyright 2022-2023 Ponfee (http://www.ponfee.cn/)\n";
private static final String NEW_COPYRIGHT_KEYWORD = "\n * Copyright 2022-2024 Ponfee (http://www.ponfee.cn/)\n";

Expand Down Expand Up @@ -115,11 +116,11 @@ private static void handleFile(Consumer<File> consumer) {
}

private boolean isOwnerCode(String sourceCode) {
if (sourceCode.contains("public class " + getClass().getSimpleName() + " {\n")) {
if (sourceCode.contains(CURRENT_FILE_CLASS)) {
// is current file: CopyrightTest.java
return true;
}
return sourceCode.contains(" * @author Ponfee\n") && StringUtils.countMatches(sourceCode, " @author ") == 1;
return sourceCode.contains("\n * @author Ponfee\n") && StringUtils.countMatches(sourceCode, " @author ") == 1;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ void testIsReachableHost() {
@Test
@Disabled
void testIsConnectable() {
assertTrue(NetUtils.isConnectableHostPort("www.baidu.com", 80, 100));
assertFalse(NetUtils.isConnectableHostPort("www.unknownhostname.com", 80, 100));
assertTrue(NetUtils.isConnectableHostPort("www.baidu.com", 80, 300));
assertTrue(NetUtils.isConnectableHostPort("www.ponfee.cn", 80, 300));
assertFalse(NetUtils.isConnectableHostPort("www.unknownhostnamexxxxxxx.com", 80, 300));
}

@Test
Expand Down Expand Up @@ -205,6 +206,12 @@ void testIgnoreGivenPrefixInterfaceName() {
}
}

@Test
void testFormat() {
assertEquals("ping -n 1 localhost", String.format("ping -%s 1 %s", "n", "localhost"));
assertEquals("ping -c 1 localhost", String.format("ping -%s 1 %s", "c", "localhost"));
}

private String getIgnoredInterfaces() {
return System.getProperty(NetUtils.IGNORED_NETWORK_INTERFACE);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 当使用database作为注册中心时使用该配置,同时需要在“application.yml”配置文件的“spring.profiles.include”项中增加“registry_database”,即“spring.profiles.include: xxx,registry_database”
disjob.registry.database:
namespace: mysql_namespace
namespace: disjob_registry
session-timeout-ms: 30000
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;

import static cn.ponfee.disjob.supervisor.dao.SupervisorDataSourceConfig.JDBC_TEMPLATE_SPRING_BEAN_NAME;
import static cn.ponfee.disjob.supervisor.dao.SupervisorDataSourceConfig.SPRING_BEAN_NAME_JDBC_TEMPLATE;

/**
* Disjob application based spring boot
Expand All @@ -53,7 +53,7 @@ public static void main(String[] args) {
}

@Bean
public IdGenerator idGenerator(@Qualifier(JDBC_TEMPLATE_SPRING_BEAN_NAME) JdbcTemplate jdbcTemplate,
public IdGenerator idGenerator(@Qualifier(SPRING_BEAN_NAME_JDBC_TEMPLATE) JdbcTemplate jdbcTemplate,
@Value("${" + JobConstants.SPRING_WEB_SERVER_PORT + "}") int port,
@Value("${" + JobConstants.DISJOB_BOUND_SERVER_HOST + ":}") String boundHost) {
return new DbDistributedSnowflake(jdbcTemplate, JobConstants.DISJOB_KEY_PREFIX, JobUtils.getLocalHost(boundHost) + Char.COLON + port);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;

import static cn.ponfee.disjob.supervisor.dao.SupervisorDataSourceConfig.JDBC_TEMPLATE_SPRING_BEAN_NAME;
import static cn.ponfee.disjob.supervisor.dao.SupervisorDataSourceConfig.SPRING_BEAN_NAME_JDBC_TEMPLATE;

/**
* Supervisor application based spring boot
Expand All @@ -49,7 +49,7 @@ public static void main(String[] args) {
}

@Bean
public IdGenerator idGenerator(@Qualifier(JDBC_TEMPLATE_SPRING_BEAN_NAME) JdbcTemplate jdbcTemplate,
public IdGenerator idGenerator(@Qualifier(SPRING_BEAN_NAME_JDBC_TEMPLATE) JdbcTemplate jdbcTemplate,
@Value("${" + JobConstants.SPRING_WEB_SERVER_PORT + "}") int port,
@Value("${" + JobConstants.DISJOB_BOUND_SERVER_HOST + ":}") String boundHost) {
return new DbDistributedSnowflake(jdbcTemplate, JobConstants.DISJOB_KEY_PREFIX, JobUtils.getLocalHost(boundHost) + Char.COLON + port);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package cn.ponfee.disjob.supervisor.application;

import cn.ponfee.disjob.common.base.SingletonClassConstraint;
import cn.ponfee.disjob.common.concurrent.ThreadPoolExecutors;
import cn.ponfee.disjob.common.exception.Throwables.ThrowingRunnable;
import cn.ponfee.disjob.core.param.supervisor.EventParam;
Expand All @@ -37,7 +38,7 @@
* @author Ponfee
*/
@Service
public class EventSubscribeService {
public class EventSubscribeService extends SingletonClassConstraint {
private static final Logger LOG = LoggerFactory.getLogger(EventSubscribeService.class);

private static final ConcurrentMap<Type, EventParam> MAP = new ConcurrentHashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package cn.ponfee.disjob.supervisor.application;

import cn.ponfee.disjob.common.base.SingletonClassConstraint;
import cn.ponfee.disjob.common.model.PageResponse;
import cn.ponfee.disjob.core.base.JobCodeMsg;
import cn.ponfee.disjob.core.enums.ExecuteState;
Expand Down Expand Up @@ -51,7 +52,7 @@
* @author Ponfee
*/
@Service
public class OpenapiService {
public class OpenapiService extends SingletonClassConstraint {

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

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

import static cn.ponfee.disjob.common.spring.TransactionUtils.assertOneAffectedRow;
import static cn.ponfee.disjob.common.spring.TransactionUtils.isOneAffectedRow;
import static cn.ponfee.disjob.supervisor.dao.SupervisorDataSourceConfig.TX_MANAGER_SPRING_BEAN_NAME;
import static cn.ponfee.disjob.supervisor.dao.SupervisorDataSourceConfig.SPRING_BEAN_NAME_TX_MANAGER;

/**
* Abstract job manager
Expand Down Expand Up @@ -99,7 +99,7 @@ public boolean updateJobNextScanTime(SchedJob schedJob) {

// ------------------------------------------------------------------database operation within spring transactional

@Transactional(transactionManager = TX_MANAGER_SPRING_BEAN_NAME, rollbackFor = Exception.class)
@Transactional(transactionManager = SPRING_BEAN_NAME_TX_MANAGER, rollbackFor = Exception.class)
public Long addJob(SchedJob job) throws JobException {
if (jobMapper.exists(job.getGroup(), job.getJobName())) {
throw new KeyExistsException("[" + job.getGroup() + "] already exists job name: " + job.getJobName());
Expand All @@ -115,7 +115,7 @@ public Long addJob(SchedJob job) throws JobException {
return job.getJobId();
}

@Transactional(transactionManager = TX_MANAGER_SPRING_BEAN_NAME, rollbackFor = Exception.class)
@Transactional(transactionManager = SPRING_BEAN_NAME_TX_MANAGER, rollbackFor = Exception.class)
public void updateJob(SchedJob job) throws JobException {
job.verifyBeforeUpdate();
job.checkAndDefaultSetting();
Expand Down Expand Up @@ -143,7 +143,7 @@ public void updateJob(SchedJob job) throws JobException {
assertOneAffectedRow(jobMapper.update(job), "Update sched job fail or conflict.");
}

@Transactional(transactionManager = TX_MANAGER_SPRING_BEAN_NAME, rollbackFor = Exception.class)
@Transactional(transactionManager = SPRING_BEAN_NAME_TX_MANAGER, rollbackFor = Exception.class)
public void deleteJob(long jobId) {
SchedJob job = jobMapper.get(jobId);
Assert.notNull(job, "Job id not found: " + jobId);
Expand Down
Loading

0 comments on commit ae99e00

Please sign in to comment.