Skip to content

Commit

Permalink
Enhance function service implementations to use Exception* functional…
Browse files Browse the repository at this point in the history
… interfaces from wildfly-common.
  • Loading branch information
pferraro committed Apr 24, 2017
1 parent a76a4ed commit 75728fd
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
4 changes: 4 additions & 0 deletions clustering/service/pom.xml
Expand Up @@ -46,6 +46,10 @@
<groupId>org.jboss.threads</groupId> <groupId>org.jboss.threads</groupId>
<artifactId>jboss-threads</artifactId> <artifactId>jboss-threads</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.wildfly.common</groupId>
<artifactId>wildfly-common</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.jboss.logging</groupId> <groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId> <artifactId>jboss-logging</artifactId>
Expand Down
Expand Up @@ -30,6 +30,7 @@
import org.jboss.msc.service.StartContext; import org.jboss.msc.service.StartContext;
import org.jboss.msc.service.StartException; import org.jboss.msc.service.StartException;
import org.jboss.msc.service.StopContext; import org.jboss.msc.service.StopContext;
import org.wildfly.common.function.ExceptionFunction;


/** /**
* Generic {@link Service} whose value is created and destroyed by contextual functions. * Generic {@link Service} whose value is created and destroyed by contextual functions.
Expand All @@ -39,7 +40,7 @@ public class FunctionalValueService<T, V> implements Service<V> {
private static final Logger LOGGER = Logger.getLogger(FunctionalValueService.class); private static final Logger LOGGER = Logger.getLogger(FunctionalValueService.class);


private final Function<T, V> mapper; private final Function<T, V> mapper;
private final Function<StartContext, T> factory; private final ExceptionFunction<StartContext, T, StartException> factory;
private final BiConsumer<StopContext, T> destroyer; private final BiConsumer<StopContext, T> destroyer;


private volatile T value; private volatile T value;
Expand All @@ -50,7 +51,7 @@ public class FunctionalValueService<T, V> implements Service<V> {
* @param factory a function that creates a value * @param factory a function that creates a value
* @param destroyer a consumer that destroys the value created by the factory function * @param destroyer a consumer that destroys the value created by the factory function
*/ */
public FunctionalValueService(Function<T, V> mapper, Function<StartContext, T> factory, BiConsumer<StopContext, T> destroyer) { public FunctionalValueService(Function<T, V> mapper, ExceptionFunction<StartContext, T, StartException> factory, BiConsumer<StopContext, T> destroyer) {
this.mapper = mapper; this.mapper = mapper;
this.factory = factory; this.factory = factory;
this.destroyer = destroyer; this.destroyer = destroyer;
Expand All @@ -65,7 +66,7 @@ public V getValue() {
public void start(StartContext context) throws StartException { public void start(StartContext context) throws StartException {
try { try {
this.value = this.factory.apply(context); this.value = this.factory.apply(context);
} catch (Throwable e) { } catch (RuntimeException | Error e) {
throw new StartException(e); throw new StartException(e);
} }
} }
Expand All @@ -75,7 +76,7 @@ public void stop(StopContext context) {
if (this.destroyer != null) { if (this.destroyer != null) {
try { try {
this.destroyer.accept(context, this.value); this.destroyer.accept(context, this.value);
} catch (Throwable e) { } catch (RuntimeException | Error e) {
LOGGER.warn(e.getLocalizedMessage(), e); LOGGER.warn(e.getLocalizedMessage(), e);
} finally { } finally {
this.value = null; this.value = null;
Expand Down
Expand Up @@ -29,6 +29,7 @@
import org.jboss.msc.service.StartContext; import org.jboss.msc.service.StartContext;
import org.jboss.msc.service.StartException; import org.jboss.msc.service.StartException;
import org.jboss.msc.service.StopContext; import org.jboss.msc.service.StopContext;
import org.wildfly.common.function.ExceptionConsumer;


/** /**
* Generic {@link Service} that provides no value. * Generic {@link Service} that provides no value.
Expand All @@ -37,15 +38,15 @@
public class FunctionalVoidService implements Service<Void> { public class FunctionalVoidService implements Service<Void> {
private static final Logger LOGGER = Logger.getLogger(FunctionalVoidService.class); private static final Logger LOGGER = Logger.getLogger(FunctionalVoidService.class);


private final Consumer<StartContext> starter; private final ExceptionConsumer<StartContext, StartException> starter;
private final Consumer<StopContext> stopper; private final Consumer<StopContext> stopper;


/** /**
* Create a new functional service. * Create a new functional service.
* @param starter * @param starter
* @param stopper * @param stopper
*/ */
public FunctionalVoidService(Consumer<StartContext> starter, Consumer<StopContext> stopper) { public FunctionalVoidService(ExceptionConsumer<StartContext, StartException> starter, Consumer<StopContext> stopper) {
this.starter = starter; this.starter = starter;
this.stopper = stopper; this.stopper = stopper;
} }
Expand All @@ -55,7 +56,7 @@ public void start(StartContext context) throws StartException {
if (this.starter != null) { if (this.starter != null) {
try { try {
this.starter.accept(context); this.starter.accept(context);
} catch (Throwable e) { } catch (RuntimeException | Error e) {
throw new StartException(e); throw new StartException(e);
} }
} }
Expand All @@ -66,7 +67,7 @@ public void stop(StopContext context) {
if (this.stopper != null) { if (this.stopper != null) {
try { try {
this.stopper.accept(context); this.stopper.accept(context);
} catch (Throwable e) { } catch (RuntimeException | Error e) {
LOGGER.warn(e.getLocalizedMessage(), e); LOGGER.warn(e.getLocalizedMessage(), e);
} }
} }
Expand Down
Expand Up @@ -37,6 +37,7 @@
<module name="org.jboss.modules"/> <module name="org.jboss.modules"/>
<module name="org.jboss.msc"/> <module name="org.jboss.msc"/>
<module name="org.jboss.threads"/> <module name="org.jboss.threads"/>
<module name="org.wildfly.common"/>
<module name="org.wildfly.security.elytron-private"/> <module name="org.wildfly.security.elytron-private"/>
</dependencies> </dependencies>
</module> </module>

0 comments on commit 75728fd

Please sign in to comment.