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

com.quartzdesk.api.agent.log.WorkerThreadLoggingInterceptorRegistry Maven / Gradle / Ivy

Go to download

QuartzDesk Public API library required for QuartzDesk Standard and Enterprise edition installations. This library must be placed on the classpath of the Quartz scheduler based application that is managed by QuartzDesk. It is important that this library is loaded by the same classloader that loads the Quartz scheduler API used by the application.

There is a newer version: 5.0.3
Show newest version
/*
 * Copyright (c) 2013-2019 QuartzDesk.com. All Rights Reserved.
 * QuartzDesk.com PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package com.quartzdesk.api.agent.log;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * A registry that can be used to turn on/off interception of log messages produced by worker threads spawned
 * and used by executing jobs. The registry is to be used like so:
 * 
 *
 * public class MyCallable
 *     implements Callable<Foo>
 * {
 *   private Thread jobThread;
 *
 *   // @param jobThread the main job execution thread that will be using this Callable instance.
 *   public MyCallable( Thread jobThread )
 *   {
 *     this.jobThread = jobThread;
 *   }
 *
 *   public Foo call()
 *       throws Exception
 *   {
 *     // Start intercepting logging events from the current worker thread (returned by Thread.currentThread())
 *     // and associate these events with the specified main job execution thread.
 *     WorkerThreadLoggingInterceptorRegistry.INSTANCE.startIntercepting( jobThread );
 *     try
 *     {
 *       // some business logic and logging
 *       return foo;
 *     }
 *     finally
 *     {
 *       // Stop intercepting logging events from the current worker thread.
 *       WorkerThreadLoggingInterceptorRegistry.INSTANCE.stopIntercepting();
 *     }
 *   }
 * }
 *
 * 
* * @version $Id:$ */ public class WorkerThreadLoggingInterceptorRegistry { public static final WorkerThreadLoggingInterceptorRegistry INSTANCE = new WorkerThreadLoggingInterceptorRegistry(); /** * The actual registry that maps a worker thread onto the main job execution thread. */ private Map registry = new ConcurrentHashMap<>(); /** * Private constructor to prevent external instantiation. */ private WorkerThreadLoggingInterceptorRegistry() { } /** * Returns the job thread ID associated with the specified worker thread ID. * * @param workerThreadId a worker thread ID. * @return the job thread ID. */ public Long getJobThreadForWorkerThread( Long workerThreadId ) { return registry.get( workerThreadId ); } /** * Starts intercepting log messages produced by the current thread (as returned by {@link Thread#currentThread()}) * that is used by the specified job thread. * * @param jobThread a job thread. */ public void startIntercepting( Thread jobThread ) { Thread currentThread = Thread.currentThread(); if ( currentThread == jobThread ) { throw new IllegalStateException( "The job thread argument must not be the current worker thread. It must be the main job thread that spawned or is using the current worker thread." ); } registry.put( currentThread.getId(), jobThread.getId() ); } /** * Stops intercepting log messages produced by the current thread (as returned by {@link Thread#currentThread()}). */ public void stopIntercepting() { registry.remove( Thread.currentThread().getId() ); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy