1616package org .commonjava .cdi .util .weft ;
1717
1818import org .commonjava .cdi .util .weft .exception .PoolOverloadException ;
19- import org .commonjava .o11yphant .metrics .api .MetricRegistry ;
20- import org .commonjava .o11yphant .metrics .api .Timer ;
2119import org .slf4j .Logger ;
2220import org .slf4j .LoggerFactory ;
2321
4038import java .util .function .Function ;
4139import java .util .stream .Collectors ;
4240
43- import static org .commonjava .o11yphant .metrics .util .NameUtils .name ;
44-
4541/**
4642 * Created by jdcasey on 1/3/17.
4743 */
4844public class PoolWeftExecutorService
4945 implements WeftExecutorService , ScheduledExecutorService
5046{
51- private static final String TIMER = "timer" ;
52-
53- private static final String METER = "meter" ;
54-
5547 private static final int DEFAULT_THREAD_COUNT = 2 ;
5648
5749 private static final float DEFAULT_LOAD_FACTOR = 10f ;
@@ -68,41 +60,33 @@ public class PoolWeftExecutorService
6860
6961 private final boolean loadSensitive ;
7062
71- private final MetricRegistry metricRegistry ;
72-
73- private final String metricPrefix ;
74-
7563 private Set <ThreadContextualizer > contextualizers ;
7664
7765 private final AtomicLong load = new AtomicLong ( 0L );
7866
7967
8068 public PoolWeftExecutorService ( String name , ThreadPoolExecutor delegate )
8169 {
82- this ( name , delegate , DEFAULT_THREAD_COUNT , DEFAULT_LOAD_FACTOR , DEFAULT_LOAD_SENSITIVE , null , null ,
70+ this ( name , delegate , DEFAULT_THREAD_COUNT , DEFAULT_LOAD_FACTOR , DEFAULT_LOAD_SENSITIVE ,
8371 Collections .emptySet () );
8472 }
8573
8674 public PoolWeftExecutorService ( final String name , ThreadPoolExecutor delegate , final Integer threadCount ,
87- final Float maxLoadFactor , boolean loadSensitive ,
88- final MetricRegistry metricRegistry , final String metricPrefix )
75+ final Float maxLoadFactor , boolean loadSensitive )
8976 {
90- this ( name , delegate , threadCount , maxLoadFactor , loadSensitive , metricRegistry , metricPrefix ,
77+ this ( name , delegate , threadCount , maxLoadFactor , loadSensitive ,
9178 Collections .emptySet () );
9279 }
9380
9481 public PoolWeftExecutorService ( final String name , ThreadPoolExecutor delegate , final Integer threadCount ,
9582 final Float maxLoadFactor , boolean loadSensitive ,
96- final MetricRegistry metricRegistry , final String metricPrefix ,
9783 Iterable <ThreadContextualizer > contextualizers )
9884 {
9985 this .name = name ;
10086 this .delegate = delegate ;
10187 this .threadCount = threadCount ;
10288 this .maxLoadFactor = maxLoadFactor ;
10389 this .loadSensitive = loadSensitive ;
104- this .metricRegistry = metricRegistry ;
105- this .metricPrefix = metricPrefix ;
10690 this .contextualizers = new HashSet <>();
10791 contextualizers .forEach ( c -> this .contextualizers .add ( c ) );
10892 }
@@ -175,7 +159,7 @@ private void verifyLoad()
175159 throw new PoolOverloadException ( getName (), getLoadFactor (), getCurrentLoad (), maxLoadFactor , getThreadCount () );
176160 }
177161 }
178-
162+
179163 @ Override
180164 public <T > Future <T > submit ( Callable <T > callable )
181165 {
@@ -313,52 +297,6 @@ private <T> ScheduledFuture<T> asScheduled( Function<ScheduledExecutorService, S
313297 }
314298 }
315299
316- private <T > Callable <T > timeCallable ( Callable <T > callable )
317- {
318- return (Callable <T >) ()->{
319- if ( metricRegistry != null )
320- {
321- metricRegistry .meter ( name ( metricPrefix , "call" , METER ) ).mark ();
322- Timer .Context context = metricRegistry .timer ( name ( metricPrefix , "call" , TIMER ) ).time ();
323- try
324- {
325- return callable .call ();
326- }
327- finally
328- {
329- context .stop ();
330- }
331- }
332- else
333- {
334- return callable .call ();
335- }
336- };
337- }
338-
339- private Runnable timeRunnable ( Runnable runnable )
340- {
341- return ()->{
342- if ( metricRegistry != null )
343- {
344- metricRegistry .meter ( name ( metricPrefix , "run" , METER ) ).mark ();
345- Timer .Context context = metricRegistry .timer ( name ( metricPrefix , "run" , TIMER ) ).time ();
346- try
347- {
348- runnable .run ();
349- }
350- finally
351- {
352- context .stop ();
353- }
354- }
355- else
356- {
357- runnable .run ();
358- }
359- };
360- }
361-
362300 private <T > Collection <Callable <T >> wrapAll ( Collection <? extends Callable <T >> collection )
363301 {
364302 ThreadContext ctx = ThreadContext .getContext ( false );
@@ -369,7 +307,7 @@ private <T> Collection<Callable<T>> wrapAll( Collection<? extends Callable<T>> c
369307 setContext ( extractedContext );
370308 Logger logger = LoggerFactory .getLogger ( getClass () );
371309 logger .debug ( "Using ThreadContext: {} (saving: {}) in {}" , ctx , old , Thread .currentThread ().getName () );
372- return timeCallable ( (Callable <T >) () -> {
310+ return (Callable <T >) () -> {
373311 try
374312 {
375313 return callable .call ();
@@ -381,7 +319,7 @@ private <T> Collection<Callable<T>> wrapAll( Collection<? extends Callable<T>> c
381319 clearBridgedContext ();
382320 load .decrementAndGet ();
383321 }
384- }) ;
322+ };
385323 } ).collect ( Collectors .toList () );
386324 }
387325
@@ -390,7 +328,7 @@ private Runnable wrapRunnable( Runnable runnable )
390328 ThreadContext ctx = ThreadContext .getContext ( false );
391329 Map <String , Object > extractedContext = extractContext ();
392330 load .incrementAndGet ();
393- return timeRunnable ( ()->{
331+ return ()->{
394332 ThreadContext old = ThreadContext .setContext ( ctx );
395333 setContext ( extractedContext );
396334 Logger logger = LoggerFactory .getLogger ( getClass () );
@@ -407,15 +345,15 @@ private Runnable wrapRunnable( Runnable runnable )
407345 clearBridgedContext ();
408346 load .decrementAndGet ();
409347 }
410- }) ;
348+ };
411349 }
412350
413351 private <T > Callable <T > wrapCallable ( Callable <T > callable )
414352 {
415353 ThreadContext ctx = ThreadContext .getContext ( false );
416354 Map <String , Object > extractedContext = extractContext ();
417355 load .incrementAndGet ();
418- return timeCallable ( (Callable <T >) ()->{
356+ return (Callable <T >) ()->{
419357 ThreadContext old = ThreadContext .setContext ( ctx );
420358 setContext ( extractedContext );
421359 Logger logger = LoggerFactory .getLogger ( getClass () );
@@ -431,7 +369,7 @@ private <T> Callable<T> wrapCallable( Callable<T> callable )
431369 clearBridgedContext ();
432370 load .decrementAndGet ();
433371 }
434- }) ;
372+ };
435373 }
436374
437375 private void clearBridgedContext ()
0 commit comments