单元测试用例

This commit is contained in:
redkale
2023-12-05 17:44:18 +08:00
parent a0da13bc4f
commit 54f9b68b3d
3 changed files with 84 additions and 15 deletions

View File

@@ -3,6 +3,8 @@
*/
package org.redkale.scheduling;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.ref.WeakReference;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
@@ -154,22 +156,27 @@ public class ScheduledFactory {
}
protected Runnable createRunnable(final WeakReference ref, Method method) {
if (!Modifier.isPublic(method.getModifiers())) {
method.setAccessible(true);
}
return () -> {
try {
Object obj = ref.get();
if (obj != null) {
if (logger.isLoggable(Level.FINEST)) {
logger.log(Level.FINEST, "schedule task " + method.getDeclaringClass().getSimpleName() + "." + method.getName());
}
method.invoke(obj);
}
} catch (Exception e) {
logger.log(Level.SEVERE, "schedule task error", e);
try {
if (!Modifier.isPublic(method.getModifiers())) {
method.setAccessible(true);
}
};
MethodHandle mh = MethodHandles.lookup().unreflect(method);
return () -> {
try {
Object obj = ref.get();
if (obj != null) {
if (logger.isLoggable(Level.FINEST)) {
logger.log(Level.FINEST, "schedule task " + method.getDeclaringClass().getSimpleName() + "." + method.getName());
}
mh.invoke(obj);
}
} catch (Throwable t) {
logger.log(Level.SEVERE, "schedule task error", t);
}
};
} catch (IllegalAccessException e) {
throw new RedkaleException(e);
}
}
protected String getProperty(String value) {

View File

@@ -0,0 +1,31 @@
/*
*
*/
package org.redkale.test.scheduling;
import org.redkale.annotation.Scheduled;
import org.redkale.service.Service;
import org.redkale.util.Times;
/**
*
* @author zhangjx
*/
public class ScheduleService implements Service {
@Scheduled(cron = "0/1 * * * * ?")
public void task1() {
System.out.println(Times.nowMillis() + "每秒-----------执行task1");
}
@Scheduled(cron = "0/1 * * * * ?")
public String task2() {
System.out.println(Times.nowMillis() + "每秒*****执行task2");
return "";
}
@Scheduled(cron = "0/1 * * * * ?")
private void task3() {
System.out.println(Times.nowMillis() + "每秒执行task3");
}
}

View File

@@ -0,0 +1,31 @@
/*
*
*/
package org.redkale.test.scheduling;
import org.junit.jupiter.api.Test;
import org.redkale.scheduling.ScheduledFactory;
import org.redkale.util.Utility;
/**
*
* @author zhangjx
*/
public class ScheduleTest {
public static void main(String[] args) throws Throwable {
ScheduleTest test = new ScheduleTest();
test.run();
}
@Test
public void run() throws Exception {
ScheduledFactory factory = ScheduledFactory.create(null);
ScheduleService service = new ScheduleService();
factory.schedule(service);
Utility.sleep(3000);
factory.unschedule(service);
factory.destroy();
}
}