You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.5 KiB

package com.bx.imserver.task;
2 years ago
import com.bx.imcommon.util.ThreadPoolExecutorFactory;
3 years ago
import com.bx.imserver.netty.IMServerGroup;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import javax.annotation.PreDestroy;
import java.util.concurrent.ExecutorService;
@Slf4j
2 years ago
public abstract class AbstractPullMessageTask implements CommandLineRunner {
2 years ago
private static final ExecutorService EXECUTOR_SERVICE = ThreadPoolExecutorFactory.getThreadPoolExecutor();
@Autowired
3 years ago
private IMServerGroup serverGroup;
@Override
public void run(String... args) {
2 years ago
EXECUTOR_SERVICE.execute(new Runnable() {
@SneakyThrows
@Override
public void run() {
try {
if (serverGroup.isReady()) {
pullMessage();
}
2 years ago
} catch (Exception e) {
log.error("任务调度异常", e);
}
if (!EXECUTOR_SERVICE.isShutdown()) {
Thread.sleep(100);
2 years ago
EXECUTOR_SERVICE.execute(this);
}
2 years ago
}
});
}
@PreDestroy
3 years ago
public void destroy() {
log.info("{}线程任务关闭", this.getClass().getSimpleName());
2 years ago
EXECUTOR_SERVICE.shutdown();
}
public abstract void pullMessage() throws InterruptedException;
}