Skip to content

Commit

Permalink
CDI startup callbacks are now able to declare a priority to define th…
Browse files Browse the repository at this point in the history
…e execution order (Bean registration startup is now executed before DB init)
  • Loading branch information
SimonDan committed Feb 15, 2020
1 parent d8c93b2 commit 70f0932
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,15 @@ public interface IStartupCallback
* Called after successful CDI initialization.
*/
void onCdiStartup();

/**
* A number identifying the priority of this startup callback.
* The default is zero. Callbacks with higher numbers will be executed earlier.
*
* @return a number indicating the priority of the startup callback
*/
default int priority()
{
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ void findCallbacks(@Observes ProcessBean<? extends IStartupCallback> pProcessedB
*/
void afterCdiStartup(@Observes @Initialized(ApplicationScoped.class) Object pObject)
{
startupCallbacks.forEach(pCallbackType -> CDI.current().select(pCallbackType).get().onCdiStartup());
startupCallbacks.stream()
.map(pCallbackType -> CDI.current().select(pCallbackType).get())
//Execute them in descending priority order
.sorted(Comparator.comparingInt(IStartupCallback::priority).reversed())
.forEach(IStartupCallback::onCdiStartup);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class CustomContextTest
@BeforeAll
public static void bootCdi()
{
cdiControl = CdiContainer.boot();
cdiControl = CdiContainer.boot(pConfig -> pConfig.addBeanClasses(SomeBeanLivingInCustomContext.class));
}

@AfterAll
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
import org.junit.jupiter.api.Test;

import javax.enterprise.context.ApplicationScoped;
import java.util.*;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;

/**
* Test for {@link IStartupCallback} that should be called after CDI boot.
Expand All @@ -14,14 +15,16 @@
*/
public class StartupCallbackTest
{
private static boolean called;
private static List<Class<? extends IStartupCallback>> calledOrder;

@Test
public void testCallbackRegisteredAndCalled()
{
called = false;
final ICdiControl cdiControl = CdiContainer.boot();
assertTrue(called);
calledOrder = new ArrayList<>();
final ICdiControl cdiControl = CdiContainer.boot(pConfig -> pConfig.addBeanClasses(Callback.class, CallbackWithHigherPriority.class));
assertEquals(2, calledOrder.size());
assertSame(CallbackWithHigherPriority.class, calledOrder.get(0));
assertSame(Callback.class, calledOrder.get(1));
cdiControl.shutdown();
}

Expand All @@ -32,7 +35,24 @@ static class Callback implements IStartupCallback
@Override
public void onCdiStartup()
{
called = true;
calledOrder.add(Callback.class);
}
}

@SuppressWarnings("unused")
@ApplicationScoped
static class CallbackWithHigherPriority implements IStartupCallback
{
@Override
public void onCdiStartup()
{
calledOrder.add(CallbackWithHigherPriority.class);
}

@Override
public int priority()
{
return 1;
}
}
}
2 changes: 1 addition & 1 deletion ojcms-cdi/src/test/resources/META-INF/beans.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd"
bean-discovery-mode="all">
bean-discovery-mode="none">
</beans>
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,10 @@ public void onCdiStartup()
CONTAINER_BEAN_TYPES.forEach((pBeanType, pContainerId) -> storage.registerPersistentBean(pBeanType, pContainerId, true));
SINGLE_BEAN_TYPES.forEach((pBeanType, pBeanId) -> storage.registerPersistentBean(pBeanType, pBeanId, false));
}

@Override
public int priority()
{
return 100;
}
}

0 comments on commit 70f0932

Please sign in to comment.