public static String getLockReservationName() { ThreadContext ctx = ThreadContext.getContext( true ); String ownerName = (String) ctx.get( PARTYLINE_LOCK_OWNER ); if ( ownerName == null ) { ownerName = "Context of: " + Thread.currentThread().getName(); ctx.put( PARTYLINE_LOCK_OWNER, ownerName ); } return ownerName; }
public ContextSensitiveWeakHashMap() { super(); this.uniqueId = UUID.randomUUID().toString(); this.finalizer = ctx -> { logger.trace( "Finalizer called for {}", uniqueId ); ctx.remove( uniqueId ); }; }
@Override public V put( K k, V v ) { V ret = super.put( k, v ); ThreadContext ctx = ThreadContext.getContext( false ); if ( ctx != null ) { Object obj = ctx.computeIfAbsent( uniqueId, o -> new HashSet<K>() ); Set<K> keys = (Set) obj; logger.trace( "Add key {} for {}", k, uniqueId ); keys.add( k ); // the k is referenced in ctx so that it will not be GC-ed until ctx clears it ctx.registerFinalizer( finalizer ); // register finalizer if not present } return ret; }
public void clearMergedPath( ArtifactStore originatingStore, Set<Group> groups, String path ) { Logger logger = LoggerFactory.getLogger( getClass() ); logger.debug( "Clearing merged path: {} from indexes of: {} (triggered by: {})", path, groups, originatingStore ); StoreKey key = originatingStore.getKey(); ThreadContext context = ThreadContext.getContext( true ); context.put( ORIGIN_KEY, key ); try { // the only time a group will have local storage of the path is when it has been merged // ...in which case we should try to delete it. indexManager.clearIndexedPathFrom( path, groups, null ); } finally { context.remove( ORIGIN_KEY ); } }
ThreadContext.clearContext(); ThreadContext threadContext = ThreadContext.getContext( true ); threadContext.put( ORIGINAL_THREAD_NAME, name ); threadContext.put( HTTP_REQUEST, hsr ); threadContext.put( METHOD_PATH_TIME, tn ); threadContext.put( CLIENT_ADDR, clientAddr ); ThreadContext.clearContext();
private Runnable wrapRunnable( Runnable runnable ) { ThreadContext ctx = ThreadContext.getContext( false ); return timeRunnable(()->{ ThreadContext old = ThreadContext.setContext( ctx ); Logger logger = LoggerFactory.getLogger( getClass() ); logger.debug( "Using ThreadContext: {} (saving: {}) in {}", ctx, old, Thread.currentThread().getName() ); try { runnable.run(); } finally { logger.debug( "Restoring ThreadContext: {} in: {}", old, Thread.currentThread().getName() ); ThreadContext.setContext( old ); } }); }
/** * Put to MDC / threadContext request IDs. */ private void putRequestIDs( HttpServletRequest hsr, ThreadContext threadContext, MDCManager mdcManager ) { /* We would always generate internalID and provide that in the MDC. * If the calling service supplies an externalID, we'd map that under its own key. * PreferredID should try to use externalID if it's available, and default over to using internalID if it's not. * What this gives us is a single key we can use to reference an ID for the request, * and whenever possible it'll reflect the externally supplied ID. */ String internalID = UUID.randomUUID().toString(); String externalID = hsr.getHeader( EXTERNAL_ID ); String preferredID = externalID != null ? externalID : internalID; mdcManager.putRequestIDs( internalID, externalID, preferredID ); /* * We should also put the same values in the ThreadContext map, so we can reference them from code without * having to go through the logging framework */ threadContext.put( INTERNAL_ID, internalID ); if ( externalID != null ) { threadContext.put( EXTERNAL_ID, externalID ); } threadContext.put( PREFERRED_ID, preferredID ); }
public static ThreadContext getContext( boolean create ) { ThreadContext threadContext = THREAD_LOCAL.get(); if ( threadContext == null && create ) { threadContext = new ThreadContext(); threadContext.mdcMap = MDC.getCopyOfContextMap(); THREAD_LOCAL.set( threadContext ); } return threadContext; }
private <T> Callable<T> wrapCallable( Callable<T> callable ) { ThreadContext ctx = ThreadContext.getContext( false ); return timeCallable((Callable<T>) ()->{ ThreadContext old = ThreadContext.setContext( ctx ); Logger logger = LoggerFactory.getLogger( getClass() ); logger.debug( "Using ThreadContext: {} (saving: {}) in {}", ctx, old, Thread.currentThread().getName() ); try { return callable.call(); } finally { logger.debug( "Restoring ThreadContext: {} in: {}", old, Thread.currentThread().getName() ); ThreadContext.setContext( old ); } }); } }
private <T> Collection<Callable<T>> wrapAll( Collection<? extends Callable<T>> collection ) { ThreadContext ctx = ThreadContext.getContext( false ); return collection.parallelStream().map( ( callable ) -> { ThreadContext old = ThreadContext.setContext( ctx ); Logger logger = LoggerFactory.getLogger( getClass() ); logger.debug( "Using ThreadContext: {} (saving: {}) in {}", ctx, old, Thread.currentThread().getName() ); return timeCallable((Callable<T>) () -> { try { return callable.call(); } finally { logger.debug( "Restoring ThreadContext: {} in: {}", old, Thread.currentThread().getName() ); ThreadContext.setContext( old ); } }); } ).collect( Collectors.toList() ); }