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

org.freedesktop.dbus.connections.config.ReceivingServiceConfigBuilder Maven / Gradle / Ivy

Go to download

Improved version of the DBus-Java library provided by freedesktop.org (https://dbus.freedesktop.org/doc/dbus-java/). This is the OSGi compliant bundle of all required libraries in one bundle.

There is a newer version: 5.1.1
Show newest version
package org.freedesktop.dbus.connections.config;

import org.freedesktop.dbus.connections.base.ReceivingService;
import org.freedesktop.dbus.connections.impl.BaseConnectionBuilder;
import org.freedesktop.dbus.connections.shared.ExecutorNames;
import org.freedesktop.dbus.connections.shared.IThreadPoolRetryHandler;
import org.freedesktop.dbus.utils.Util;
import org.slf4j.LoggerFactory;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;

/**
 * Configuration builder to configure {@link ReceivingService}.
 * Only intended to be used in combination with {@link BaseConnectionBuilder}
 *
 * @author hypfvieh
 *
 * @param  BaseConnectionBuilder type
 * @since 4.2.0 - 2022-07-14
 */
public final class ReceivingServiceConfigBuilder> {
    public static final int DEFAULT_HANDLER_RETRIES = 10;

    private static final ReceivingServiceConfig DEFAULT_CFG = new ReceivingServiceConfig();

    private static final IThreadPoolRetryHandler DEFAULT_RETRYHANDLER = new IThreadPoolRetryHandler() {
        private final AtomicInteger retries = new AtomicInteger(0);
        @Override
        public boolean handle(ExecutorNames _executor, Exception _ex) {
            if (retries.incrementAndGet() < DEFAULT_HANDLER_RETRIES) {
                return true;
            }
            LoggerFactory.getLogger(ReceivingService.class).error("Dropping runnable for {}, retry failed for more than {} iterations, cause:", _executor, DEFAULT_HANDLER_RETRIES, _ex);
            return false;
        }
    };

    private final Supplier connectionBuilder;
    private final ReceivingServiceConfig config = new ReceivingServiceConfig();

    public ReceivingServiceConfigBuilder(Supplier _bldr) {
        connectionBuilder = _bldr;
        config.setRetryHandler(DEFAULT_RETRYHANDLER);
    }

    /**
     * Set the size of the thread-pool used to handle signals from the bus.
     * Caution: Using thread-pool size > 1 may cause signals to be handled out-of-order
     * 

* Default: 1 * * @param _threads int >= 1 * @return this */ public ReceivingServiceConfigBuilder withSignalThreadCount(int _threads) { config.setSignalThreadPoolSize(Math.max(1, _threads)); return this; } /** * Set the size of the thread-pool used to handle error messages received on the bus. *

* Default: 1 * * @param _threads int >= 1 * @return this */ public ReceivingServiceConfigBuilder withErrorHandlerThreadCount(int _threads) { config.setErrorThreadPoolSize(Math.max(1, _threads)); return this; } /** * Set the size of the thread-pool used to handle methods calls previously sent to the bus. * The thread pool size has to be > 1 to handle recursive calls. *

* Default: 4 * * @param _threads int >= 1 * @return this */ public ReceivingServiceConfigBuilder withMethodCallThreadCount(int _threads) { config.setMethodCallThreadPoolSize(Math.max(1, _threads)); return this; } /** * Set the size of the thread-pool used to handle method return values received on the bus. *

* Default: 1 * * @param _threads int >= 1 * @return this */ public ReceivingServiceConfigBuilder withMethodReturnThreadCount(int _threads) { config.setMethodReturnThreadPoolSize(Math.max(1, _threads)); return this; } /** * Sets the thread priority of the created signal thread(s). *

* Default: {@link Thread#NORM_PRIORITY} ({@value Thread#NORM_PRIORITY}); * * @param _priority int >={@value Thread#MIN_PRIORITY} and <= {@value Thread#MAX_PRIORITY} * @return this * * @throws IllegalArgumentException when value is out ouf range (value < {@value Thread#MIN_PRIORITY} && > {@value Thread#MAX_PRIORITY}) */ public ReceivingServiceConfigBuilder withSignalThreadPriority(int _priority) { config.setSignalThreadPriority(Util.checkIntInRange(_priority, Thread.MIN_PRIORITY, Thread.MAX_PRIORITY)); return this; } /** * Sets the thread priority of the created signal thread(s). *

* Default: {@link Thread#NORM_PRIORITY} ({@value Thread#NORM_PRIORITY}); * * @param _priority int >={@value Thread#MIN_PRIORITY} and <= {@value Thread#MAX_PRIORITY} * @return this * * @throws IllegalArgumentException when value is out ouf range (value < {@value Thread#MIN_PRIORITY} && > {@value Thread#MAX_PRIORITY}) */ public ReceivingServiceConfigBuilder withErrorThreadPriority(int _priority) { config.setErrorThreadPriority(Util.checkIntInRange(_priority, Thread.MIN_PRIORITY, Thread.MAX_PRIORITY)); return this; } /** * Sets the thread priority of the created signal thread(s). *

* Default: {@link Thread#NORM_PRIORITY} ({@value Thread#NORM_PRIORITY}); * * @param _priority int >={@value Thread#MIN_PRIORITY} and <= {@value Thread#MAX_PRIORITY} * @return this * * @throws IllegalArgumentException when value is out ouf range (value < {@value Thread#MIN_PRIORITY} && > {@value Thread#MAX_PRIORITY}) */ public ReceivingServiceConfigBuilder withMethedCallThreadPriority(int _priority) { config.setMethodCallThreadPriority(Util.checkIntInRange(_priority, Thread.MIN_PRIORITY, Thread.MAX_PRIORITY)); return this; } /** * Sets the thread priority of the created signal thread(s). *

* Default: {@link Thread#NORM_PRIORITY} ({@value Thread#NORM_PRIORITY}); * * @param _priority int >={@value Thread#MIN_PRIORITY} and <= {@value Thread#MAX_PRIORITY} * @return this * * @throws IllegalArgumentException when value is out ouf range (value < {@value Thread#MIN_PRIORITY} && > {@value Thread#MAX_PRIORITY}) */ public ReceivingServiceConfigBuilder withMethodReturnThreadPriority(int _priority) { config.setMethodReturnThreadPriority(Util.checkIntInRange(_priority, Thread.MIN_PRIORITY, Thread.MAX_PRIORITY)); return this; } /** * Sets the retry handler which should be called when executing a runnable in {@link ReceivingService} thread pools fail. *

* Defaults to an implementation retrying executing the runnable up to ten times. * If null is given, retrying will be disabled (but error will be logged). * * @param _handler handler to use * @return this */ public ReceivingServiceConfigBuilder withRetryHandler(IThreadPoolRetryHandler _handler) { config.setRetryHandler(_handler); return this; } /** * Returns the configured {@link ReceivingServiceConfig} instance. * @return config never null */ public ReceivingServiceConfig build() { return config; } /** * Returns the used ConnectionBuilder for the connection for further configuration. * @return connection builder */ public R connectionConfig() { return connectionBuilder.get(); } /** * Returns the default configuration used for {@link ReceivingService}. * @return default config */ public static ReceivingServiceConfig getDefaultConfig() { return DEFAULT_CFG; } /** * Returns the default retry handler used for {@link ReceivingService}. * @return default handler */ public static IThreadPoolRetryHandler getDefaultRetryHandler() { return DEFAULT_RETRYHANDLER; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy