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

io.github.ma1uta.matrix.bot.AbstractStandaloneBotPool Maven / Gradle / Ivy

The newest version!
/*
 * Copyright Anatoliy Sablin [email protected]
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.github.ma1uta.matrix.bot;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * Bot service.
 *
 * @param  bot configuration.
 * @param  bot dao.
 * @param  bot service.
 * @param  extra data.
 */
public abstract class AbstractStandaloneBotPool, S extends PersistentService, E> extends
    AbstractBotPool> {

    private static final Logger LOGGER = LoggerFactory.getLogger(AbstractStandaloneBotPool.class);

    private static final int TIMEOUT = 10;

    private final ExecutorService pool;

    public AbstractStandaloneBotPool(String displayName, S service, List>> commandClasses) {
        super(displayName, service, commandClasses);
        pool = Executors.newCachedThreadPool();
    }

    public ExecutorService getPool() {
        return pool;
    }

    @Override
    protected StandaloneBot createBotInstance(C config) {
        return new StandaloneBot<>(true, config, getService(), getCommandClasses());
    }

    @Override
    protected void submitBot(StandaloneBot bot) {
        getPool().submit(bot);
    }

    /**
     * Stop pool.
     *
     * @throws InterruptedException when cannot stop bot's thread.
     */
    @Override
    public void stop() throws InterruptedException {
        getPool().awaitTermination(TIMEOUT, TimeUnit.SECONDS);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy