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.

63 lines
1.7 KiB

package com.bx.imserver.task;
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 javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
3 years ago
import java.util.concurrent.*;
@Slf4j
3 years ago
public abstract class AbstractPullMessageTask {
private int threadNum = 1;
private ExecutorService executorService;
@Autowired
3 years ago
private IMServerGroup serverGroup;
3 years ago
public AbstractPullMessageTask() {
this.threadNum = 1;
}
3 years ago
public AbstractPullMessageTask(int threadNum) {
this.threadNum = threadNum;
}
@PostConstruct
3 years ago
public void init() {
// 初始化定时器
executorService = Executors.newFixedThreadPool(threadNum);
3 years ago
for (int i = 0; i < threadNum; i++) {
executorService.execute(new Runnable() {
@SneakyThrows
@Override
public void run() {
3 years ago
try {
if (serverGroup.isReady()) {
pullMessage();
}
3 years ago
} catch (Exception e) {
log.error("任务调度异常", e);
Thread.sleep(200);
}
3 years ago
if (!executorService.isShutdown()) {
executorService.execute(this);
}
}
});
}
}
@PreDestroy
3 years ago
public void destroy() {
log.info("{}线程任务关闭", this.getClass().getSimpleName());
executorService.shutdown();
}
public abstract void pullMessage();
}