import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.CronTask;
import org.springframework.scheduling.config.IntervalTask;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.ScheduledMethodRunnable;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class StatSchedulerTest {
@Test
public void test() {
PrettyLog prettyLog = new PrettyLogImpl("test");
try {
TimeUnit.SECONDS.sleep(10);
} catch (Exception e) {
if (e != null) {
prettyLog.append("ERROR", e.toString());
}
e.printStackTrace();
} finally {
prettyLog.stop();
System.out.println(prettyLog.prettyPrint());
}
}
@TestConfiguration
static class SchedulingTestConfig implements SchedulingConfigurer {
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
PrettyLog prettyLog = new PrettyLogImpl("SchedulingTestConfig");
List<IntervalTask> taskList = taskRegistrar.getFixedDelayTaskList();
List<CronTask> cronTaskList = taskRegistrar.getCronTaskList();
List<IntervalTask> newTaskList = new ArrayList<>();
List<CronTask> newCronTaskList = new ArrayList<>();
prettyLog.start("IntervalTask");
for (IntervalTask task : taskList) {
ScheduledMethodRunnable runnable = (ScheduledMethodRunnable) task.getRunnable();
String methodName = runnable.getMethod().getName();
Object target = runnable.getTarget();
if (target instanceof SearchScheduler) {
if (methodName.equals("searchTest")) {
prettyLog.append("method",
target.getClass().getSimpleName() + "." + methodName + "(" + task.getInterval() + "ms)");
newTaskList.add(new IntervalTask(runnable, task.getInterval(), task.getInitialDelay()));
}
}
}
prettyLog.stop();
prettyLog.start("CronTask");
for (CronTask task : cronTaskList) {
ScheduledMethodRunnable runnable = (ScheduledMethodRunnable) task.getRunnable();
Method method = runnable.getMethod();
String methodName = method.getName();
Object target = runnable.getTarget();
if (target instanceof StatScheduler) {
if (methodName.equals("test1")) {
prettyLog.append("method",
target.getClass().getSimpleName() + "." + methodName + "(" + task.getExpression() + ")");
newCronTaskList.add(new CronTask(runnable, task.getExpression()));
}
}
}
prettyLog.stop();
taskRegistrar.setFixedDelayTasksList(newTaskList);
taskRegistrar.setCronTasksList(newCronTaskList);
prettyLog.stop();
System.out.println(prettyLog.prettyPrint());
}
}
}