我有两个任务,我在并行线程中运行,我正在解释为什么Watch
功能不起作用。如果您有任何见解,请告诉我。
任务1:获取广告连播的状态并显示当前状态。
任务2:密切关注新事件。这就是我想要更好地理解的内容。
每个任务都使用scheduledAtFixedRate()
每30秒执行一次。
预期行为:
任务1:我应该获取所有pod的列表及其当前状态(这样可行)。
任务2:我应该期待新事件的列表。
观察到的行为:
任务1:一切正常。我每隔30秒就会获得最新的pod状态。
任务2:它会转储第一个请求中的事件,但似乎它不会更新任何新事件。
代码: 任务1:
@Component
@Scope(value = org.springframework.beans.factory.config.ConfigurableBeanFactory.SCOPE_SINGLETON)
public class Task1 implements Runnable {
private ScheduledExecutorService scheduledExecutorService;
private CommandInvoker commandInvoker;
private static final int INITIAL_DELAY = 15;
private static final int POLLING_INTERVAL = 30;
@Autowired
public Task1 (CommandInvoker commandInvoker,
ScheduledExecutorService scheduledExecutorService) {
this.commandInvoker = commandInvoker;
this.scheduledExecutorService = scheduledExecutorService;
this.scheduledExecutorService.scheduleAtFixedRate(this, INITIAL_DELAY, POLLING_INTERVAL, TimeUnit.SECONDS);
}
@Override
public void run() {
System.out.println("===== STARTING TASK 1 POD HEALTH CHECK =======");
commandInvoker.getPodStatus();
}
}
任务2:
@Component
@Scope(value = org.springframework.beans.factory.config.ConfigurableBeanFactory.SCOPE_SINGLETON)
public class Task2 implements Runnable {
private ScheduledExecutorService scheduledExecutorService;
private CommandInvoker commandInvoker;
@Autowired
public Task2(CommandInvoker commandInvoker,
ScheduledExecutorService scheduledExecutorService) {
this.commandInvoker = commandInvoker;
this.scheduledExecutorService = scheduledExecutorService;
this.scheduledExecutorService.scheduleAtFixedRate(this, 30, 30, TimeUnit.SECONDS);
}
@Override
public void run() {
System.out.println("===== STARTING TASK 2 EVENT WATCH UPDATE =======");
commandInvoker.getWatchUpdates();
}
}
CommandInvoker:
@Component
public class CommandInvoker {
public void getPodStatus() {
try {
CoreV1Api api = new CoreV1Api();
V1PodList list = api.listPodForAllNamespaces(null,
null, null, null, null, null, null, null, null);
for( V1Pod pod : list.getItems() ) {
// THIS WORKS //
}
} catch ( ApiException e) {
throw new WhateverException ("Failed to handle watchlist event", e);
}
}
public void getWatchUpdates() {
CoreV1Api api = new CoreV1Api();
try {
Watch<V1Event> watch = Watch.createWatch(
apiClient,
api.listEventForAllNamespacesCall(null, null, null, null,
null, null, null, null, true, null, null),
new TypeToken<Watch.Response<V1Event>>() {}.getType());
watch.forEach( response -> {
V1Event event = response.object;
// THIS ONLY DUMPS EVENTS FROM FIRST CALL BUT NEVER GETS EXECUTED AGAIN
});
// I NEVER REACH HERE BUT I DON'T GET ANY UPDATES
} catch ( ApiException e) {
throw new K8ServerException("Failed to handle watchlist event", e);
}
}
}