Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Common context #600

Merged
merged 15 commits into from
May 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,11 @@
<artifactId>helidon-common-service-loader</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.common</groupId>
<artifactId>helidon-common-context</artifactId>
<version>${project.version}</version>
</dependency>

<!-- tracing -->
<dependency>
Expand Down
5 changes: 5 additions & 0 deletions common/configurable/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
<artifactId>helidon-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.common</groupId>
<artifactId>helidon-common-context</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.helidon.config</groupId>
<artifactId>helidon-config</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2019 Oracle and/or its affiliates. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,16 +17,19 @@
package io.helidon.common.configurable;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;

import io.helidon.common.context.Contexts;
import io.helidon.config.Config;

/**
* Supplier of a custom scheduled thread pool.
* The returned thread pool supports {@link io.helidon.common.context.Context} propagation.
*/
public final class ScheduledThreadPoolSupplier implements Supplier<ExecutorService> {
private static final int EXECUTOR_DEFAULT_CORE_POOL_SIZE = 16;
Expand All @@ -38,7 +41,7 @@ public final class ScheduledThreadPoolSupplier implements Supplier<ExecutorServi
private final boolean isDaemon;
private final String threadNamePrefix;
private final boolean prestart;
private volatile ScheduledThreadPoolExecutor instance;
private volatile ScheduledExecutorService instance;

private ScheduledThreadPoolSupplier(Builder builder) {
this.corePoolSize = builder.corePoolSize;
Expand Down Expand Up @@ -76,25 +79,31 @@ public static ScheduledThreadPoolSupplier create() {
return builder().build();
}

ScheduledThreadPoolExecutor getThreadPool() {
ScheduledThreadPoolExecutor result;
result = new ScheduledThreadPoolExecutor(corePoolSize,
new ThreadFactory() {
private final AtomicInteger value = new AtomicInteger();

@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(null,
r,
threadNamePrefix + value.incrementAndGet());
t.setDaemon(isDaemon);
return t;
}
});
if (prestart) {
result.prestartAllCoreThreads();
}
return result;
}

@Override
public synchronized ScheduledThreadPoolExecutor get() {
public synchronized ScheduledExecutorService get() {
if (null == instance) {
instance = new ScheduledThreadPoolExecutor(corePoolSize,
new ThreadFactory() {
private AtomicInteger value = new AtomicInteger();

@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(null,
r,
threadNamePrefix + value.incrementAndGet());
t.setDaemon(isDaemon);
return t;
}
});
if (prestart) {
instance.prestartAllCoreThreads();
}
instance = Contexts.wrap(getThreadPool());
}
return instance;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2019 Oracle and/or its affiliates. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,10 +24,12 @@
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;

import io.helidon.common.context.Contexts;
import io.helidon.config.Config;

/**
* Supplier of a custom thread pool.
* The returned thread pool supports {@link io.helidon.common.context.Context} propagation.
*/
public final class ThreadPoolSupplier implements Supplier<ExecutorService> {
private static final int EXECUTOR_DEFAULT_CORE_POOL_SIZE = 10;
Expand All @@ -45,7 +47,7 @@ public final class ThreadPoolSupplier implements Supplier<ExecutorService> {
private final boolean isDaemon;
private final String threadNamePrefix;
private final boolean prestart;
private volatile ThreadPoolExecutor instance;
private volatile ExecutorService instance;

private ThreadPoolSupplier(Builder builder) {
this.corePoolSize = builder.corePoolSize;
Expand Down Expand Up @@ -86,29 +88,35 @@ public static ThreadPoolSupplier create() {
return builder().build();
}

ThreadPoolExecutor getThreadPool() {
ThreadPoolExecutor result;
result = new ThreadPoolExecutor(corePoolSize,
maxPoolSize,
keepAliveMinutes,
TimeUnit.MINUTES,
new LinkedBlockingQueue<>(queueCapacity),
new ThreadFactory() {
private final AtomicInteger value = new AtomicInteger();

@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(null,
r,
threadNamePrefix + value.incrementAndGet());
t.setDaemon(isDaemon);
return t;
}
});
if (prestart) {
result.prestartAllCoreThreads();
}
return result;
}

@Override
public synchronized ThreadPoolExecutor get() {
public synchronized ExecutorService get() {
if (null == instance) {
instance = new ThreadPoolExecutor(corePoolSize,
maxPoolSize,
keepAliveMinutes,
TimeUnit.MINUTES,
new LinkedBlockingQueue<>(queueCapacity),
new ThreadFactory() {
private AtomicInteger value = new AtomicInteger();

@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(null,
r,
threadNamePrefix + value.incrementAndGet());
t.setDaemon(isDaemon);
return t;
}
});
if (prestart) {
instance.prestartAllCoreThreads();
}
instance = Contexts.wrap(getThreadPool());
}
return instance;
}
Expand Down
4 changes: 2 additions & 2 deletions common/configurable/src/main/java9/module-info.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2019 Oracle and/or its affiliates. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,9 +21,9 @@
*/
module io.helidon.common.configurable {
requires java.logging;

requires transitive io.helidon.config;
requires io.helidon.common;
requires io.helidon.common.context;

exports io.helidon.common.configurable;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2019 Oracle and/or its affiliates. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -39,18 +39,18 @@ class ScheduledThreadPoolSupplierTest {

@BeforeAll
static void initClass() {
defaultInstance = ScheduledThreadPoolSupplier.create().get();
defaultInstance = ScheduledThreadPoolSupplier.create().getThreadPool();

builtInstance = ScheduledThreadPoolSupplier.builder()
.threadNamePrefix("scheduled-thread-pool-unit-test-")
.corePoolSize(2)
.daemon(true)
.prestart(true)
.build()
.get();
.getThreadPool();

configuredInstance = ScheduledThreadPoolSupplier.create(Config.create()
.get("unit.scheduled-thread-pool")).get();
.get("unit.scheduled-thread-pool")).getThreadPool();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2019 Oracle and/or its affiliates. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -40,7 +40,7 @@ class ThreadPoolSupplierTest {

@BeforeAll
static void initClass() {
defaultInstance = ThreadPoolSupplier.create().get();
defaultInstance = ThreadPoolSupplier.create().getThreadPool();

builtInstance = ThreadPoolSupplier.builder()
.threadNamePrefix("thread-pool-unit-test-")
Expand All @@ -49,10 +49,10 @@ static void initClass() {
.prestart(true)
.queueCapacity(10)
.build()
.get();
.getThreadPool();

configuredInstance = ThreadPoolSupplier.create(Config.create().get("unit.thread-pool"))
.get();
.getThreadPool();
}

@Test
Expand Down
47 changes: 47 additions & 0 deletions common/context/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved.

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.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.helidon.common</groupId>
<artifactId>helidon-common-project</artifactId>
<version>1.0.4-SNAPSHOT</version>
</parent>
<artifactId>helidon-common-context</artifactId>
<name>Helidon Common Context</name>

<dependencies>
<dependency>
<groupId>io.helidon.common</groupId>
<artifactId>helidon-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Loading