2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.core.logging.api.context;
23 import org.openecomp.core.logging.api.BaseFactory;
26 * <p>Should be used to propagate a diagnostic context (for instance <a
27 * href="http://www.slf4j.org/manual.html#mdc">MDC</a>) to other threads.</p> <p>Applicable when
28 * creating a child thread directly, or submitting tasks for potentially postponed execution via an
29 * <a href="http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Executor.html">Executor</a>
30 * (including any of the <a href="http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html">executor
31 * services</a> and <a href="http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ForkJoinPool.html">ForkJoinPool</a>).</p>
32 * <p>The service used by this factory must implement {@link ContextPropagationService}.</p>
34 * @see ContextPropagationService
36 @SuppressWarnings("ThrowableInstanceNeverThrown")
37 public class TaskFactory extends BaseFactory {
39 private static final ContextPropagationService SERVICE;
40 private static final RuntimeException ERROR;
44 ContextPropagationService service = null;
45 RuntimeException error = null;
48 service = locateService(ContextPropagationService.class);
49 } catch (Throwable throwable) {
50 error = new RuntimeException("Failed to instantiate task factory", throwable);
58 * Modify a task so that a diagnostic context is propagated to the thread when the task runs. Done
59 * in a logging-framework specific way.
61 * @param task any Runnable that will run in a thread
62 * @return modified (wrapped) original task that runs the same business logic, but also takes care
63 of copying the diagnostic context for logging.
65 public static Runnable create(Runnable task) {
67 if (SERVICE == null) {
71 return SERVICE.create(task);