com.github.lontime.extredisson.container.ServiceContainer Maven / Gradle / Ivy
The newest version!
package com.github.lontime.extredisson.container;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import com.github.lontime.base.commonj.components.AbstractLifecycle;
import com.github.lontime.base.commonj.components.Processor;
import com.github.lontime.base.commonj.utils.CollectionHelper;
import com.github.lontime.base.logging.GLogger;
import com.github.lontime.extredisson.configuration.ConsumerOption;
import com.github.lontime.extredisson.configuration.OptionResolver;
/**
* Stream Listener.
* 消费者名字 必须通过手动创建
* 读取配置文件
*
* @author lontime
* @since 1.0
*/
public class ServiceContainer extends AbstractLifecycle {
private List services = new ArrayList<>();
@Override
public void initialize() {
addService(new NodeService());
addService(new WatchService());
final List options = OptionResolver.getInstance().getConsumers();
if (CollectionHelper.isEmpty(options)) {
return;
}
addServices(options.stream().filter(s -> Objects.nonNull(s.getTopic()))
.filter(s -> !s.getInterval().isZero())
.map(DelayedConsumerProcessor::new).collect(Collectors.toList()));
addServices(options.stream().filter(s -> Objects.nonNull(s.getTopic()))
.filter(s -> s.getInterval().isZero())
.map(UndelayedConsumerProcessor::new).collect(Collectors.toList()));
}
@Override
public void start() {
GLogger.defaults().infov("ServiceContainer starting, total size:{0}!!", services.size());
for (Processor service : services) {
service.startAsync();
}
for (Processor service : services) {
service.awaitRunningOther();
}
GLogger.defaults().infov("ServiceContainer started, total size:{0}!!", services.size());
}
@Override
public void stop() {
GLogger.defaults().infov("ServiceContainer shutdowning, total size:{0}!!", services.size());
for (Processor service : services) {
service.stopAsync();
}
for (Processor service : services) {
service.awaitTerminatedOther();
}
GLogger.defaults().infov("ServiceContainer had Terminated!!");
}
private void addService(Processor service) {
this.services.add(service);
}
private void addServices(List services) {
this.services.addAll(services);
}
}