Author: rmannibucau
Date: Thu Jun 1 15:31:42 2017
New Revision: 1797234
URL: http://svn.apache.org/viewvc?rev=1797234&view=rev
Log:
default executor handling
Modified:
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/WebBeansContext.java
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/event/EventImpl.java
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/event/NotificationManager.java
Modified:
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/WebBeansContext.java
URL:
http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/WebBeansContext.java?rev=1797234&r1=1797233&r2=1797234&view=diff
==============================================================================
---
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/WebBeansContext.java
(original)
+++
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/config/WebBeansContext.java
Thu Jun 1 15:31:42 2017
@@ -27,6 +27,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
+import java.util.concurrent.ExecutorService;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -105,7 +106,7 @@ public class WebBeansContext
private final ConversationManager conversationManager;
private ConversationService conversationService = null;
private final ApplicationBoundaryService applicationBoundaryService;
- private final NotificationManager notificationManager = new
NotificationManager(this);
+ private final NotificationManager notificationManager;
public WebBeansContext()
@@ -159,6 +160,8 @@ public class WebBeansContext
beanArchiveService = getService(BeanArchiveService.class);
conversationManager = new ConversationManager(this);
+ notificationManager = new NotificationManager(this);
+
// Allow the WebBeansContext itself to be looked up
managerMap.put(getClass(), this);
@@ -490,6 +493,20 @@ public class WebBeansContext
logger.log(Level.SEVERE, "Error while destroying SPI
service " + spiService.getClass().getName(), e);
}
}
+ else if (ExecutorService.class.isInstance(spiService))
+ {
+ ExecutorService es = ExecutorService.class.cast(spiService);
+ es.shutdownNow().forEach(r -> {
+ try
+ {
+ r.run();
+ }
+ catch (final RuntimeException re)
+ {
+ logger.warning(re.getMessage());
+ }
+ });
+ }
}
}
Modified:
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/event/EventImpl.java
URL:
http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/event/EventImpl.java?rev=1797234&r1=1797233&r2=1797234&view=diff
==============================================================================
---
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/event/EventImpl.java
(original)
+++
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/event/EventImpl.java
Thu Jun 1 15:31:42 2017
@@ -86,10 +86,7 @@ public class EventImpl<T> implements Eve
@Override
public <U extends T> CompletionStage<U> fireAsync(U event)
{
- Type eventType = event.getClass();
- webBeansContext.getWebBeansUtil().validEventType(eventType.getClass(),
metadata.getType());
- return webBeansContext.getNotificationManager().fireEvent(event,
metadata.select(eventType), false,
-
webBeansContext.getNotificationManager().getDefaultNotificationOptions());
+ return fireAsync(event,
webBeansContext.getNotificationManager().getDefaultNotificationOptions());
}
//X TODO OWB-1182 CDI 2.0
Modified:
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/event/NotificationManager.java
URL:
http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/event/NotificationManager.java?rev=1797234&r1=1797233&r2=1797234&view=diff
==============================================================================
---
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/event/NotificationManager.java
(original)
+++
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/event/NotificationManager.java
Thu Jun 1 15:31:42 2017
@@ -19,6 +19,8 @@
package org.apache.webbeans.event;
+import java.io.Closeable;
+import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.ParameterizedType;
@@ -26,6 +28,7 @@ import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Collection;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
@@ -38,9 +41,10 @@ import java.util.concurrent.CompletionEx
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.Executor;
import java.util.concurrent.ForkJoinPool;
+import java.util.concurrent.RejectedExecutionException;
import java.util.stream.Stream;
import javax.enterprise.context.RequestScoped;
@@ -141,12 +145,14 @@ public final class NotificationManager
this.defaultNotificationOptions =
NotificationOptions.ofExecutor(getDefaultExecutor());
}
- //X TODO move to some SPI and implement properly!
- private ExecutorService getDefaultExecutor()
+ private Executor getDefaultExecutor()
{
- // this is just for the start!
- //X must get implemented properly with configuration etc
- return Executors.newFixedThreadPool(5);
+ // here it would be nice to support to use a produced bean like
@Named("openwebbeansCdiExecutor")
+ // instead of a direct spi
+ //
+ // logic is: if an Executor is registered as a spi use it, otherwise
use JVM default one
+ final Executor service = webBeansContext.getService(Executor.class);
+ return service != null ? service : new
CloseableExecutor(ForkJoinPool.commonPool());
}
/**
@@ -929,4 +935,54 @@ public final class NotificationManager
}
}
}
+
+ private static final class CloseableExecutor implements Executor, Closeable
+ {
+ private final Executor delegate;
+ private final Collection<Runnable> tracker = new
CopyOnWriteArrayList<>();
+ private volatile boolean reject = false;
+
+ private CloseableExecutor(final Executor delegate)
+ {
+ this.delegate = delegate;
+ }
+
+ @Override
+ public void close() throws IOException
+ {
+ reject = true;
+ tracker.forEach(r -> {
+ try
+ {
+ r.run();
+ }
+ catch (final RuntimeException re)
+ {
+
WebBeansLoggerFacade.getLogger(NotificationManager.class).warning(re.getMessage());
+ }
+ });
+ }
+
+ @Override
+ public void execute(final Runnable command)
+ {
+ if (reject)
+ {
+ throw new RejectedExecutionException("CDI executor is
shutdown");
+ }
+
+ tracker.add(command);
+ delegate.execute(() ->
+ {
+ try
+ {
+ command.run();
+ }
+ finally
+ {
+ tracker.remove(command);
+ }
+ });
+ }
+ }
}