Skip to content

Commit

Permalink
optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
ponfee committed Feb 16, 2024
1 parent 933a445 commit 8044cde
Show file tree
Hide file tree
Showing 15 changed files with 66 additions and 284 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

import cn.ponfee.disjob.admin.util.PageUtils;
import cn.ponfee.disjob.common.base.TextTokenizer;
import cn.ponfee.disjob.common.concurrent.Threads;
import cn.ponfee.disjob.common.util.Jsons;
import cn.ponfee.disjob.common.util.Numbers;
import cn.ponfee.disjob.common.util.SleepWaitUtils;
import cn.ponfee.disjob.common.util.Strings;
import cn.ponfee.disjob.core.enums.RunState;
import cn.ponfee.disjob.core.exception.KeyNotExistsException;
Expand Down Expand Up @@ -176,7 +176,7 @@ public AjaxResult pause(@PathVariable("instanceId") Long instanceId) {
authorizeGroupService.authorizeInstance(getLoginName(), instanceId);

openapiService.pauseInstance(instanceId);
SleepWaitUtils.waitUntil(WAIT_SLEEP_ROUND, WAIT_SLEEP_MILLIS, () -> {
Threads.waitUntil(WAIT_SLEEP_ROUND, WAIT_SLEEP_MILLIS, () -> {
SchedInstanceResponse instance = openapiService.getInstance(instanceId, false);
return !RunState.Const.PAUSABLE_LIST.contains(RunState.of(instance.getRunState()));
});
Expand All @@ -194,7 +194,7 @@ public AjaxResult resume(@PathVariable("instanceId") Long instanceId) {
authorizeGroupService.authorizeInstance(getLoginName(), instanceId);

openapiService.resumeInstance(instanceId);
SleepWaitUtils.waitUntil(WAIT_SLEEP_ROUND, new long[]{500, 200}, () -> {
Threads.waitUntil(WAIT_SLEEP_ROUND, new long[]{500, 200}, () -> {
SchedInstanceResponse instance = openapiService.getInstance(instanceId, false);
return !RunState.PAUSED.equals(instance.getRunState());
});
Expand All @@ -212,7 +212,7 @@ public AjaxResult cancel(@PathVariable("instanceId") Long instanceId) {
authorizeGroupService.authorizeInstance(getLoginName(), instanceId);

openapiService.cancelInstance(instanceId);
SleepWaitUtils.waitUntil(WAIT_SLEEP_ROUND, WAIT_SLEEP_MILLIS, () -> {
Threads.waitUntil(WAIT_SLEEP_ROUND, WAIT_SLEEP_MILLIS, () -> {
SchedInstanceResponse instance = openapiService.getInstance(instanceId, false);
return RunState.of(instance.getRunState()).isTerminal();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@

package cn.ponfee.disjob.common.concurrent;

import cn.ponfee.disjob.common.exception.Throwables;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.function.BooleanSupplier;

/**
* Thread utilities
*
Expand Down Expand Up @@ -105,6 +108,29 @@ public static String getStackTrace(Thread thread) {
return buildStackTrace(thread.getStackTrace());
}

public static boolean waitUntil(int round, long[] sleepMillis, BooleanSupplier supplier) {
return waitUntil(round, sleepMillis, true, supplier);
}

public static boolean waitUntil(int round, long[] sleepMillis, boolean caught, BooleanSupplier supplier) {
int lastIndex = sleepMillis.length - 1;
for (int i = 0; i < round; i++) {
long sleepTime = sleepMillis[Math.min(i, lastIndex)];
if (sleepTime > 0) {
if (caught) {
Throwables.ThrowingRunnable.doCaught(() -> Thread.sleep(sleepTime));
} else {
Throwables.ThrowingRunnable.doChecked(() -> Thread.sleep(sleepTime));
}
}
if (supplier.getAsBoolean()) {
return true;
}
}

return false;
}

// ------------------------------------------------------------private methods

private static String buildStackTrace(StackTraceElement[] traces) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@
import org.springframework.util.Assert;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.URL;
import java.net.URLDecoder;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
Expand Down Expand Up @@ -416,7 +418,7 @@ public static Tuple2<Class<?>, Predicates> obtainClass(Object obj) {
*/
public static String getClassFilePath(Class<?> clazz) {
URL url = clazz.getProtectionDomain().getCodeSource().getLocation();
String path = new File(URLCodes.decodeURI(url.getPath(), Files.UTF_8)).getAbsolutePath();
String path = new File(decodeURL(url)).getAbsolutePath();

if (path.toLowerCase().endsWith(".jar")) {
path += "!";
Expand All @@ -431,8 +433,7 @@ public static String getClassFilePath(Class<?> clazz) {
* @return spec classpath
*/
public static String getClasspath(Class<?> clazz) {
URL url = clazz.getProtectionDomain().getCodeSource().getLocation();
String path = URLCodes.decodeURI(url.getPath(), Files.UTF_8);
String path = decodeURL(clazz.getProtectionDomain().getCodeSource().getLocation());
if (path.toLowerCase().endsWith(".jar")) {
path = path.substring(0, path.lastIndexOf("/") + 1);
}
Expand All @@ -445,9 +446,8 @@ public static String getClasspath(Class<?> clazz) {
* @return current main classpath
*/
public static String getClasspath() {
String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
path = URLCodes.decodeURI(new File(path).getAbsolutePath(), Files.UTF_8);
return path + File.separator;
URL url = Thread.currentThread().getContextClassLoader().getResource("");
return new File(decodeURL(url)).getAbsolutePath() + File.separator;
}

// -------------------------------------------------------------------------------------------private methods
Expand Down Expand Up @@ -584,4 +584,12 @@ private static String toString(Class<?>[] parameterTypes) {
: "(" + Joiner.on(", ").join(parameterTypes) + ")";
}

private static String decodeURL(URL url) {
try {
return URLDecoder.decode(url.getPath(), Files.UTF_8);
} catch (UnsupportedEncodingException e) {
return ExceptionUtils.rethrow(e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.springframework.aop.framework.Advised;
import org.springframework.aop.framework.AdvisedSupport;
import org.springframework.aop.support.AopUtils;
import org.springframework.cglib.proxy.Enhancer;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
Expand All @@ -29,7 +28,7 @@
*
* @author Ponfee
*/
public class ProxyUtils {
public final class ProxyUtils {

/**
* Creates jdk proxy instance
Expand Down Expand Up @@ -66,17 +65,6 @@ public static Object getTargetObject(Object object) throws Exception {
return object;
}

public static <T> T createBrokenProxy(Class<T> type, Class<?>[] argumentTypes, Object[] arguments) {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(type);
enhancer.setUseCache(true);
enhancer.setInterceptDuringConstruction(false);
enhancer.setCallback((org.springframework.cglib.proxy.InvocationHandler) (proxy, method, args) -> {
throw new UnsupportedOperationException("Broken proxy cannot execute method: " + method.toGenericString());
});
return (T) enhancer.create(argumentTypes, arguments);
}

private static Object getProxyTargetObject(Object proxy) throws Exception {
AdvisedSupport advisedSupport = (AdvisedSupport) Fields.get(proxy, "advised");
return advisedSupport.getTargetSource().getTarget();
Expand Down

This file was deleted.

137 changes: 0 additions & 137 deletions disjob-common/src/main/java/cn/ponfee/disjob/common/util/URLCodes.java

This file was deleted.

Loading

0 comments on commit 8044cde

Please sign in to comment.