优化ScheduleManagerService

This commit is contained in:
redkale
2024-06-25 15:07:39 +08:00
parent 978afdd829
commit c05be6d4f8
4 changed files with 40 additions and 8 deletions

View File

@@ -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());
}
}

View File

@@ -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");
}
}

View File

@@ -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";
}
}
}