[
https://issues.apache.org/jira/browse/DELTASPIKE-1199?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15451456#comment-15451456
]
Martin Kouba edited comment on DELTASPIKE-1199 at 8/31/16 7:36 AM:
-------------------------------------------------------------------
Hi Seto,
the problem is the Weld impl of ContextControl does not really start/stop the
application context. {{startApplicationScope()}} is actually a no-op because in
Weld the application context is always active
(https://github.com/apache/deltaspike/blob/master/deltaspike/cdictrl/impl-weld/src/main/java/org/apache/deltaspike/cdise/weld/WeldContextControl.java#L134).{{stopApplicationScope()}}
actually invalidates the context and destroys all bean instances
(https://github.com/apache/deltaspike/blob/master/deltaspike/cdictrl/impl-weld/src/main/java/org/apache/deltaspike/cdise/weld/WeldContextControl.java#L140).
That's why you're observing this odd behavior:
{code:java}
public class Test {
public static void main(String[] args) {
CdiContainer cdiContainer = CdiContainerLoader.getCdiContainer();
cdiContainer.boot(); // Fires ContainerInitialized and Kernel bean
instance is created to receive notification
ContextControl contextControl = cdiContainer.getContextControl();
contextControl.startContext(ApplicationScoped.class); // Does nothing
contextControl.stopContext(ApplicationScoped.class); // Destroys
application context - Kernel bean instance is destroyed
cdiContainer.shutdown(); // Fires ContainerShutdown, a new Kernel bean
instance is created to receive notification
// Application context is destroyed and deactivated automatically
}
}
{code}
was (Author: mkouba):
Hi Seto,
the problem is the Weld impl of ContextControl does not really start/stop the
application context. {{startApplicationScope()}} is actually a no-op because in
Weld the application context is always active
(https://github.com/apache/deltaspike/blob/master/deltaspike/cdictrl/impl-weld/src/main/java/org/apache/deltaspike/cdise/weld/WeldContextControl.java#L134).{{stopApplicationScope()}}
actually invalidates the context and destroys all bean instances
(https://github.com/apache/deltaspike/blob/master/deltaspike/cdictrl/impl-weld/src/main/java/org/apache/deltaspike/cdise/weld/WeldContextControl.java#L140).
That's why you're observing this odd behavior:
{code:java}
public class Test {
public static void main(String[] args) {
CdiContainer cdiContainer = CdiContainerLoader.getCdiContainer();
cdiContainer.boot(); // Fires ContainerInitialized and Kernel bean
instance is created to receive notification
ContextControl contextControl = cdiContainer.getContextControl();
contextControl.startContext(ApplicationScoped.class); // Does nothing
contextControl.stopContext(ApplicationScoped.class); // Destroys
application context - Kernel bean instance is destroyed
cdiContainer.shutdown(); // Fires ContainerShutdown, a new Kernel bean
instance is created to receive notification
}
}
{code}
> Problems with ContextControl and Weld ContainerInitialized,
> ContainerShutdown event.
> -------------------------------------------------------------------------------------
>
> Key: DELTASPIKE-1199
> URL: https://issues.apache.org/jira/browse/DELTASPIKE-1199
> Project: DeltaSpike
> Issue Type: Bug
> Components: CdiControl
> Affects Versions: 1.7.1
> Reporter: Seto
>
> No replies in user mail list. So I submit an issue here. And I think it's a
> bug as well.
> I have an @ApplicationScoped bean. It observes ContainerInitialized and
> ContainerShutdown event.
> {code:title=Kernel.java|borderStyle=solid}
> public Kernel(){
> System.out.println("Kernel constructed");
> }
> public void onContainerInitialized(@Observes ContainerInitialized event,
> @Parameters List<String> parameters) {
> System.out.println("container initialized");
> }
> public void onContainerShutdown(@Observes ContainerShutdown event){
> System.out.println("container shutdown");
> }
> {code}
> Then the kernel is constructed twice with the code below, one after boot, one
> after shutdown.
> {code:title=Test.java|borderStyle=solid}
> public class Test {
> public static void main(String[] args) {
> CdiContainer cdiContainer = CdiContainerLoader.getCdiContainer();
> cdiContainer.boot();
> // Starting the application-context enables use of @ApplicationScoped
> beans
> ContextControl contextControl = cdiContainer.getContextControl();
> contextControl.startContext(ApplicationScoped.class);
> // You can use CDI here
> contextControl.stopContext(ApplicationScoped.class);
> cdiContainer.shutdown();
> }
> }
> {code}
> If I remove the ContextControl related code. Then it is constructed only once
> after boot.
> If I keep the ContextControl related code and remove the observation of
> ContainerInitialized. Then it is constructed only after shutdown.
> If I keep the ContextControl related code and remove the observation of
> ContainerShutdown. Then it is constructed only after boot.
> What I expect is it is constructed only once after boot even I keep the
> ContextControl related code.
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)