/** * Create an asynchronous event listener which uses a thread poool to process events. * * @param poolSize size of the event processing pool * @param timeOutSeconds time out in seconds when an idle thread is destroyed * @param isDaemon if the threads used to process should be daemon threads, * if false, then implementation should call stop() * to stop the thread pool */ public AsyncEventListener(int poolSize, long timeOutSeconds, final boolean isDaemon) { eventQueue = new LinkedBlockingQueue<>(); ThreadFactory factory = new BasicThreadFactory.Builder() .namingPattern(getName()+"_AsyncThread-%d") .daemon(isDaemon) .priority(Thread.NORM_PRIORITY) .build(); // fixed pool with min and max equal to poolSize processor = new ThreadPoolExecutor(poolSize, poolSize, timeOutSeconds, TimeUnit.SECONDS, eventQueue, factory); processor.allowCoreThreadTimeOut(true); }
@Test public void testAysncEventListenerPoolThreads(){ AsyncEventListener<QuerySuccess> ayncListener = new DummyAsncEventListener(); for(int i=0; i<10; i++){ try { //A pool thread is created each time an event is submitted until core pool size is reached which is 5 //for this test case. @see org.apache.lens.server.api.events.AsyncEventListener.processor ayncListener.onEvent(null); } catch (LensException e) { assert(false); //Not Expected } } //Verify the core pool Threads after the events have been fired ThreadGroup currentTG = Thread.currentThread().getThreadGroup(); int count = currentTG.activeCount(); Thread[] threads = new Thread[count]; currentTG.enumerate(threads); Set<String> aysncThreadNames = new HashSet<String>(); for(Thread t : threads){ if (t.getName().contains("DummyAsncEventListener_AsyncThread")){ aysncThreadNames.add(t.getName()); } } assertTrue(aysncThreadNames.containsAll(Arrays.asList( "DummyAsncEventListener_AsyncThread-1", "DummyAsncEventListener_AsyncThread-2", "DummyAsncEventListener_AsyncThread-3", "DummyAsncEventListener_AsyncThread-4", "DummyAsncEventListener_AsyncThread-5"))); }
@Override public synchronized void stop() { // unregister LensEventService eventService = LensServices.get().getService(LensEventService.NAME); if (eventService != null) { eventService.removeListener(queryStatusListener); } if (queryStatusListener != null) { queryStatusListener.stop(); } if (reporters != null) { for (ScheduledReporter reporter : reporters) { reporter.stop(); } } log.info("Stopped metrics service"); super.stop(); }
/** * Create an asynchronous event listener which uses a thread poool to process events. * * @param poolSize size of the event processing pool * @param timeOutSeconds time out in seconds when an idle thread is destroyed * @param isDaemon if the threads used to process should be daemon threads, * if false, then implementation should call stop() * to stop the thread pool */ public AsyncEventListener(int poolSize, long timeOutSeconds, final boolean isDaemon) { eventQueue = new LinkedBlockingQueue<>(); ThreadFactory factory = new BasicThreadFactory.Builder() .namingPattern(getName()+"_AsyncThread-%d") .daemon(isDaemon) .priority(Thread.NORM_PRIORITY) .build(); // fixed pool with min and max equal to poolSize processor = new ThreadPoolExecutor(poolSize, poolSize, timeOutSeconds, TimeUnit.SECONDS, eventQueue, factory); processor.allowCoreThreadTimeOut(true); }