aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/vespa/model/container/ThreadPoolExecutorComponent.java
blob: aac73ce7636bc94977705a74b0c9923170678a6a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.container;

import com.yahoo.container.bundle.BundleInstantiationSpecification;
import com.yahoo.container.handler.ThreadpoolConfig;
import com.yahoo.container.handler.threadpool.ContainerThreadPool;
import com.yahoo.osgi.provider.model.ComponentModel;
import com.yahoo.vespa.model.container.component.SimpleComponent;

import java.time.Duration;

/**
 * Component definition for a {@link java.util.concurrent.Executor} using {@link ContainerThreadPool}.
 *
 * @author bjorncs
 */
public class ThreadPoolExecutorComponent extends SimpleComponent implements ThreadpoolConfig.Producer {

    private final String name;
    private final Integer maxPoolSize;
    private final Integer corePoolSize;
    private final Duration keepAliveTime;
    private final Integer queueSize;
    private final Duration maxThreadExecutionTime;

    private ThreadPoolExecutorComponent(Builder builder) {
        super(new ComponentModel(
                BundleInstantiationSpecification.getFromStrings(
                        "threadpool@" + builder.name,
                        ContainerThreadPool.class.getName(),
                        null)));
        this.name = builder.name;
        this.maxPoolSize = builder.maxPoolSize;
        this.corePoolSize = builder.corePoolSize;
        this.keepAliveTime = builder.keepAliveTime;
        this.queueSize = builder.queueSize;
        this.maxThreadExecutionTime = builder.maxThreadExecutionTime;
    }

    @Override
    public void getConfig(ThreadpoolConfig.Builder builder) {
        builder.name(this.name);
        if (maxPoolSize != null) builder.maxthreads(maxPoolSize);
        if (corePoolSize != null) builder.corePoolSize(corePoolSize);
        if (keepAliveTime != null) builder.keepAliveTime(keepAliveTime.toMillis() / 1000D);
        if (queueSize != null) builder.queueSize(queueSize);
        if (maxThreadExecutionTime != null) builder.maxThreadExecutionTimeSeconds((int)maxThreadExecutionTime.toMillis() / 1000);
    }

    public static class Builder {

        private final String name;
        private Integer maxPoolSize;
        private Integer corePoolSize;
        private Duration keepAliveTime;
        private Integer queueSize;
        private Duration maxThreadExecutionTime;

        public Builder(String name) { this.name = name; }

        public Builder maxPoolSize(int size) { this.maxPoolSize = size; return this; }
        public Builder corePoolSize(int size) { this.corePoolSize = size; return this; }
        public Builder keepAliveTime(Duration time) { this.keepAliveTime = time; return this; }
        public Builder queueSize(int size) { this.queueSize = size; return this; }
        public Builder maxThreadExecutionTime(Duration time) { this.maxThreadExecutionTime = time; return this; }

        public ThreadPoolExecutorComponent build() { return new ThreadPoolExecutorComponent(this); }

    }
}