All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.lontime.extkafka.service.ConsumerContainer Maven / Gradle / Ivy

package com.github.lontime.extkafka.service;

import java.util.ArrayList;
import java.util.List;
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.commonj.utils.LoggerHelper;
import com.github.lontime.extkafka.configuration.ConsumerOption;
import com.github.lontime.extkafka.configuration.OptionResolver;

/**
 * @author lontime
 */
public class ConsumerContainer extends AbstractLifecycle {

    private List services = new ArrayList<>();

    @Override
    public void initialize() {
        final List options = OptionResolver.getInstance().getConsumers();
        if (CollectionHelper.isNotEmpty(options)) {
            addServices(options.stream().map(ConsumerProcessor::new).collect(Collectors.toList()));
        } else {
            LoggerHelper.warnv("Not found consumers!!");
        }
    }

    @Override
    public void start() {
        LoggerHelper.infov("ConsumerContainer starting, total size:{0}!!", services.size());
        if (CollectionHelper.isEmpty(services)) {
            return;
        }
        for (Processor service : services) {
            service.startAsync();
        }
        for (Processor service : services) {
            service.awaitRunningOther();
        }
        LoggerHelper.infov("ConsumerContainer started, total size:{0}!!", services.size());
    }

    @Override
    public void stop() {
        LoggerHelper.infov("ConsumerContainer shutdowning, total size:{0}!!", services.size());
        if (CollectionHelper.isEmpty(services)) {
            return;
        }
        for (Processor service : services) {
            service.stopAsync();
        }
        for (Processor service : services) {
            service.awaitTerminatedOther();
        }
        LoggerHelper.infov("ConsumerContainer had Terminated!!");
    }

    private void addService(Processor service) {
        this.services.add(service);
    }

    private void addServices(List services) {
        this.services.addAll(services);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy