优化ScheduleManagerService
This commit is contained in:
@@ -36,8 +36,9 @@ import org.redkale.annotation.Resource;
|
||||
import org.redkale.annotation.ResourceType;
|
||||
import org.redkale.boot.Application;
|
||||
import org.redkale.net.sncp.Sncp;
|
||||
import org.redkale.scheduled.ScheduledEvent;
|
||||
import org.redkale.scheduled.Scheduled;
|
||||
import org.redkale.scheduled.ScheduledEvent;
|
||||
import org.redkale.scheduled.ScheduledManager;
|
||||
import org.redkale.service.LoadMode;
|
||||
import org.redkale.service.Local;
|
||||
import org.redkale.service.Service;
|
||||
@@ -45,7 +46,6 @@ import org.redkale.util.AnyValue;
|
||||
import org.redkale.util.RedkaleClassLoader;
|
||||
import org.redkale.util.RedkaleException;
|
||||
import org.redkale.util.Utility;
|
||||
import org.redkale.scheduled.ScheduledManager;
|
||||
|
||||
/**
|
||||
* 定时任务管理器
|
||||
@@ -426,7 +426,7 @@ public class ScheduleManagerService implements ScheduledManager, Service {
|
||||
doing.set(false);
|
||||
}
|
||||
if (ref.get() == null) {
|
||||
stop();
|
||||
super.stop();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -471,7 +471,7 @@ public class ScheduleManagerService implements ScheduledManager, Service {
|
||||
} finally {
|
||||
doing.set(false);
|
||||
}
|
||||
start();
|
||||
schedule();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -480,6 +480,12 @@ public class ScheduleManagerService implements ScheduledManager, Service {
|
||||
return;
|
||||
}
|
||||
if (started.compareAndSet(false, true)) {
|
||||
schedule();
|
||||
}
|
||||
}
|
||||
|
||||
private void schedule() {
|
||||
if (started.get()) {
|
||||
LocalDateTime now = zoneId == null ? LocalDateTime.now() : LocalDateTime.now(zoneId);
|
||||
LocalDateTime next = cron.next(now);
|
||||
Duration delay = Duration.between(now, next);
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
*/
|
||||
package org.redkale.test.scheduled;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.redkale.scheduled.spi.ScheduleManagerService;
|
||||
import org.redkale.util.Utility;
|
||||
@@ -19,10 +20,16 @@ public class ScheduledTest {
|
||||
public void run() throws Exception {
|
||||
ScheduleManagerService manager = ScheduleManagerService.create(null);
|
||||
manager.init(null);
|
||||
ScheduledService service = new ScheduledService();
|
||||
ScheduledTestService service = new ScheduledTestService();
|
||||
manager.schedule(service);
|
||||
Utility.sleep(3000);
|
||||
System.out.println("开始执行");
|
||||
Utility.sleep(2000);
|
||||
manager.stop("task2");
|
||||
Utility.sleep(1010);
|
||||
manager.unschedule(service);
|
||||
manager.destroy(null);
|
||||
Assertions.assertEquals(3, service.count1.get());
|
||||
Assertions.assertEquals(2, service.count2.get());
|
||||
Assertions.assertEquals(3, service.count3.get());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,26 +3,33 @@
|
||||
*/
|
||||
package org.redkale.test.scheduled;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import org.redkale.scheduled.Scheduled;
|
||||
import org.redkale.service.Service;
|
||||
import org.redkale.util.Times;
|
||||
|
||||
/** @author zhangjx */
|
||||
public class ScheduledService implements Service {
|
||||
public class ScheduledTestService implements Service {
|
||||
public AtomicInteger count1 = new AtomicInteger();
|
||||
public AtomicInteger count2 = new AtomicInteger();
|
||||
public AtomicInteger count3 = new AtomicInteger();
|
||||
|
||||
@Scheduled(cron = "0/1 * * * * ?")
|
||||
public void task1() {
|
||||
count1.incrementAndGet();
|
||||
System.out.println(Times.nowMillis() + "每秒-----------执行task1");
|
||||
}
|
||||
|
||||
@Scheduled(cron = "0/1 * * * * ?")
|
||||
@Scheduled(name = "task2", cron = "0/1 * * * * ?")
|
||||
public String task2() {
|
||||
count2.incrementAndGet();
|
||||
System.out.println(Times.nowMillis() + "每秒*****执行task2");
|
||||
return "";
|
||||
}
|
||||
|
||||
@Scheduled(cron = "0/1 * * * * ?")
|
||||
private void task3() {
|
||||
count3.incrementAndGet();
|
||||
System.out.println(Times.nowMillis() + "每秒执行task3");
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.security.Key;
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.spec.*;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.redkale.util.*;
|
||||
|
||||
/** @author zhangjx */
|
||||
@@ -126,5 +127,16 @@ public class UntilTestMain {
|
||||
+ Attribute.create(TestBean.class.getDeclaredField("map")).genericType());
|
||||
System.out.println();
|
||||
System.out.println();
|
||||
|
||||
Attribute attr = Attribute.createGetter(TestAttr.class, "bean");
|
||||
TestAttr obj = new TestAttr();
|
||||
Assertions.assertEquals("jaja", attr.get(obj));
|
||||
}
|
||||
|
||||
public static class TestAttr {
|
||||
|
||||
public String bean() {
|
||||
return "jaja";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user