Skip to content
This repository was archived by the owner on May 28, 2018. It is now read-only.

Commit c4ba2da

Browse files
author
Adam Lindenthal
committed
MIC-5473: JerseyPublisher should use managed executor service by default
Custom thread naming not supported currently. Change-Id: Ie45e8e921825d28f45ee35450c8fb05b80beff9b
1 parent 9ac3345 commit c4ba2da

File tree

21 files changed

+703
-185
lines changed

21 files changed

+703
-185
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* http://glassfish.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package org.glassfish.jersey.client;
41+
42+
import java.lang.annotation.Documented;
43+
import java.lang.annotation.ElementType;
44+
import java.lang.annotation.Inherited;
45+
import java.lang.annotation.Retention;
46+
import java.lang.annotation.RetentionPolicy;
47+
import java.lang.annotation.Target;
48+
49+
import javax.inject.Qualifier;
50+
51+
/**
52+
* Injection qualifier that can be used to inject an {@link java.util.concurrent.ScheduledExecutorService}
53+
* instance used by Jersey client runtime to schedule background tasks.
54+
* <p>
55+
* The scheduled executor service instance injected using this injection qualifier can be customized
56+
* by registering a custom {@link org.glassfish.jersey.spi.ScheduledExecutorServiceProvider} implementation that is itself
57+
* annotated with the {@code &#64;ClientAsyncExecutor} annotation.
58+
* </p>
59+
*
60+
* @author Adam Lindenthal (adam.lindenthal at oracle.com)
61+
* @see ClientBackgroundSchedulerLiteral
62+
* @since 2.26
63+
*/
64+
@Target({ElementType.PARAMETER, ElementType.FIELD, ElementType.METHOD, ElementType.TYPE})
65+
@Retention(RetentionPolicy.RUNTIME)
66+
@Documented
67+
@Inherited
68+
@Qualifier
69+
public @interface ClientBackgroundScheduler {
70+
71+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* http://glassfish.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package org.glassfish.jersey.client;
41+
42+
import org.glassfish.jersey.internal.inject.AnnotationLiteral;
43+
44+
/**
45+
* {@link ClientBackgroundScheduler} annotation literal.
46+
* <p>
47+
* This class provides a {@link #INSTANCE constant instance} of the {@code @ClientBackgroundScheduler} annotation to be used
48+
* in method calls that require use of annotation instances.
49+
* </p>
50+
*
51+
* @author Adam Lindenthal (adam.lindenthal at oracle.com)
52+
* @since 2.26
53+
*/
54+
@SuppressWarnings("ClassExplicitlyAnnotation")
55+
public final class ClientBackgroundSchedulerLiteral extends AnnotationLiteral<ClientBackgroundScheduler>
56+
implements ClientBackgroundScheduler {
57+
58+
/**
59+
* An {@link ClientBackgroundScheduler} annotation instance.
60+
*/
61+
public static final ClientBackgroundScheduler INSTANCE = new ClientBackgroundSchedulerLiteral();
62+
63+
private ClientBackgroundSchedulerLiteral() {
64+
// prevents instantiation from the outside.
65+
}
66+
}

core-client/src/main/java/org/glassfish/jersey/client/ClientBinder.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
import org.glassfish.jersey.process.internal.RequestScope;
6666
import org.glassfish.jersey.process.internal.RequestScoped;
6767
import org.glassfish.jersey.spi.ExecutorServiceProvider;
68+
import org.glassfish.jersey.spi.ScheduledExecutorServiceProvider;
6869

6970
/**
7071
* Registers all binders necessary for {@link Client} runtime.
@@ -141,5 +142,7 @@ protected void configure() {
141142
bind(asyncThreadPoolSize).named("ClientAsyncThreadPoolSize");
142143
// DefaultClientAsyncExecutorProvider must be singleton scoped, so that @PreDestroy, which closes the executor, is called
143144
bind(DefaultClientAsyncExecutorProvider.class).to(ExecutorServiceProvider.class).in(Singleton.class);
145+
146+
bind(DefaultClientBackgroundSchedulerProvider.class).to(ScheduledExecutorServiceProvider.class).in(Singleton.class);
144147
}
145148
}

core-client/src/main/java/org/glassfish/jersey/client/ClientConfig.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,10 @@ ClientRuntime getRuntime() {
723723
return state.runtime.get();
724724
}
725725

726+
public ClientExecutor getClientExecutor() {
727+
return state.runtime.get();
728+
}
729+
726730
/**
727731
* Get the parent Jersey client this configuration is bound to.
728732
*
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* http://glassfish.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package org.glassfish.jersey.client;
41+
42+
import java.util.concurrent.Callable;
43+
import java.util.concurrent.Future;
44+
import java.util.concurrent.ScheduledFuture;
45+
import java.util.concurrent.TimeUnit;
46+
47+
/**
48+
* Executor for client async processing and background task scheduling.
49+
*
50+
* @author Adam Lindenthal (adam.lindenthal at oracle.com)
51+
* @since 2.26
52+
*/
53+
public interface ClientExecutor {
54+
/**
55+
* Submits a value-returning task for execution and returns a {@link Future} representing the pending results of the task.
56+
* The Future's {@code get()} method will return the task's result upon successful completion.
57+
*
58+
* @param task task to submit
59+
* @param <T> task's return type
60+
* @return a {@code Future} representing pending completion of the task
61+
* @throws {@link java.util.concurrent.RejectedExecutionException} if the task cannot be scheduled for execution
62+
* @throws {@link NullPointerException} if the task is null
63+
*/
64+
<T> Future<T> submit(Callable<T> task);
65+
66+
/**
67+
* Submits a {@link Runnable} task for execution and returns a {@link Future} representing that task. The Future's {@code
68+
* get()} method will return the given result upon successful completion.
69+
*
70+
* @param task the task to submit
71+
* @return a {@code Future} representing pending completion of the task
72+
* @throws {@link java.util.concurrent.RejectedExecutionException} if the task cannot be scheduled for execution
73+
* @throws {@link NullPointerException} if the task is null
74+
*/
75+
Future<?> submit(Runnable task);
76+
77+
/**
78+
* Submits a {@link Runnable} task for execution and returns a {@link Future} representing that task. The Future's {@code
79+
* get()} method will return the given result upon successful completion.
80+
*
81+
* @param task the task to submit
82+
* @param result the result to return
83+
* @param <T> result type
84+
* @return a {@code Future} representing pending completion of the task
85+
* @throws {@link java.util.concurrent.RejectedExecutionException} if the task cannot be scheduled for execution
86+
* @throws {@link NullPointerException} if the task is null
87+
*/
88+
<T> Future<T> submit(Runnable task, T result);
89+
90+
/**
91+
* Creates and executes a {@link ScheduledFuture} that becomes enabled after the given delay.
92+
*
93+
* @param callable the function to execute
94+
* @param delay the time from now to delay execution
95+
* @param unit the time unit of the delay parameter
96+
* @param <T> return type of the function
97+
* @return a {@code ScheduledFuture} that can be used to extract result or cancel
98+
* @throws {@link java.util.concurrent.RejectedExecutionException} if the task cannot be scheduled for execution
99+
* @throws {@link NullPointerException} if callable is null
100+
*/
101+
<T> ScheduledFuture<T> schedule(Callable<T> callable, long delay, TimeUnit unit);
102+
103+
/**
104+
* Creates and executes a one-shot action that becomes enabled after the given delay.
105+
*
106+
* @param command the task to execute
107+
* @param delay the time from now to delay execution
108+
* @param unit the time unit of the daly parameter
109+
* @return a scheduledFuture representing pending completion of the task and whose {@code get()} method will return {@code
110+
* null} upon completion
111+
*/
112+
ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit);
113+
114+
115+
}

core-client/src/main/java/org/glassfish/jersey/client/ClientProperties.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
33
*
4-
* Copyright (c) 2012-2015 Oracle and/or its affiliates. All rights reserved.
4+
* Copyright (c) 2012-2017 Oracle and/or its affiliates. All rights reserved.
55
*
66
* The contents of this file are subject to the terms of either the GNU
77
* General Public License Version 2 only ("GPL") or the Common Development
@@ -148,6 +148,32 @@ public final class ClientProperties {
148148
*/
149149
public static final String ASYNC_THREADPOOL_SIZE = "jersey.config.client.async.threadPoolSize";
150150

151+
/**
152+
* Scheduler thread pool size.
153+
* <p>
154+
* The value MUST be an instance of {@link java.lang.Integer}.
155+
* </p>
156+
* <p>
157+
* If the property is absent then thread pool used for background task scheduling will
158+
* be initialized as default scheduled thread pool executor, which creates new thread
159+
* for every new request, see {@link java.util.concurrent.Executors}. When a
160+
* value &gt;&nbsp;0 is provided, the created scheduled thread pool executor limited to that
161+
* number of threads will be utilized. Zero or negative values will be ignored.
162+
* </p>
163+
* <p>
164+
* Note that the property may be ignored if a custom {@link org.glassfish.jersey.spi.ExecutorServiceProvider}
165+
* is configured to execute background tasks scheduling in the client runtime (see
166+
* {@link org.glassfish.jersey.client.ClientBackgroundScheduler}).
167+
* </p>
168+
* <p>
169+
* A default value is not set.
170+
* </p>
171+
* <p>
172+
* The name of the configuration property is <tt>{@value}</tt>.
173+
* </p>
174+
*/
175+
public static final String BACKGROUND_SCHEDULER_THREADPOOL_SIZE = "jersey.config.client.backgroundScheduler.threadPoolSize";
176+
151177
/**
152178
* If {@link org.glassfish.jersey.client.filter.EncodingFilter} is
153179
* registered, this property indicates the value of Content-Encoding

0 commit comments

Comments
 (0)