Search in sources :

Example 6 with ContextsService

use of org.apache.webbeans.spi.ContextsService in project tomee by apache.

the class EnsureRequestScopeThreadLocalIsCleanUpTest method ensureRequestContextCanBeRestarted.

@Test
public void ensureRequestContextCanBeRestarted() throws Exception {
    final ApplicationComposers composers = new ApplicationComposers(EnsureRequestScopeThreadLocalIsCleanUpTest.class);
    composers.before(this);
    final CdiAppContextsService contextsService = CdiAppContextsService.class.cast(WebBeansContext.currentInstance().getService(ContextsService.class));
    final Context req1 = contextsService.getCurrentContext(RequestScoped.class);
    assertNotNull(req1);
    final Context session1 = contextsService.getCurrentContext(SessionScoped.class);
    assertNotNull(session1);
    contextsService.endContext(RequestScoped.class, null);
    contextsService.startContext(RequestScoped.class, null);
    final Context req2 = contextsService.getCurrentContext(RequestScoped.class);
    assertNotSame(req1, req2);
    final Context session2 = contextsService.getCurrentContext(SessionScoped.class);
    assertSame(session1, session2);
    composers.after();
    assertNull(contextsService.getCurrentContext(RequestScoped.class));
    assertNull(contextsService.getCurrentContext(SessionScoped.class));
}
Also used : WebBeansContext(org.apache.webbeans.config.WebBeansContext) Context(javax.enterprise.context.spi.Context) SessionScoped(javax.enterprise.context.SessionScoped) ContextsService(org.apache.webbeans.spi.ContextsService) RequestScoped(javax.enterprise.context.RequestScoped) ApplicationComposers(org.apache.openejb.testing.ApplicationComposers) Test(org.junit.Test)

Example 7 with ContextsService

use of org.apache.webbeans.spi.ContextsService in project tomee by apache.

the class JMS2AMQTest method cdi.

@Test
public void cdi() throws InterruptedException {
    final String text = TEXT + "3";
    final AtomicReference<Throwable> error = new AtomicReference<>();
    final CountDownLatch ready = new CountDownLatch(1);
    final CountDownLatch over = new CountDownLatch(1);
    new Thread() {

        {
            setName(JMS2AMQTest.class.getName() + ".cdi#receiver");
        }

        @Override
        public void run() {
            final ContextsService contextsService = WebBeansContext.currentInstance().getContextsService();
            // spec defines it for request scope an transaction scope
            contextsService.startContext(RequestScoped.class, null);
            try {
                ready.countDown();
                assertEquals(text, context.createConsumer(destination3).receiveBody(String.class, TimeUnit.MINUTES.toMillis(1)));
                // ensure we dont do a NPE if there is nothing to read
                assertNull(context.createConsumer(destination3).receiveBody(String.class, 100));
            } catch (final Throwable t) {
                error.set(t);
            } finally {
                contextsService.endContext(RequestScoped.class, null);
                over.countDown();
            }
        }
    }.start();
    ready.await(1, TimeUnit.MINUTES);
    // just to ensure we called receive already
    sleep(150);
    // now send the message
    try (final JMSContext context = cf.createContext()) {
        context.createProducer().send(destination3, text);
    } catch (final JMSRuntimeException ex) {
        fail(ex.getMessage());
    }
    over.await(1, TimeUnit.MINUTES);
    // ensure we got the message and no exception
    final Throwable exception = error.get();
    if (exception != null) {
        exception.printStackTrace();
    }
    assertNull(exception == null ? "ok" : exception.getMessage(), exception);
}
Also used : ContextsService(org.apache.webbeans.spi.ContextsService) AtomicReference(java.util.concurrent.atomic.AtomicReference) RequestScoped(javax.enterprise.context.RequestScoped) CountDownLatch(java.util.concurrent.CountDownLatch) JMSContext(javax.jms.JMSContext) JMSRuntimeException(javax.jms.JMSRuntimeException) Test(org.junit.Test)

Example 8 with ContextsService

use of org.apache.webbeans.spi.ContextsService in project tomee by apache.

the class RequestScopedThreadContextListener method contextEntered.

@Override
public void contextEntered(final ThreadContext oldContext, final ThreadContext newContext) {
    final BeanContext beanContext = newContext.getBeanContext();
    final WebBeansContext webBeansContext = beanContext.getModuleContext().getAppContext().getWebBeansContext();
    if (webBeansContext == null) {
        return;
    }
    final ContextsService contextsService = webBeansContext.getContextsService();
    final Context requestContext = CdiAppContextsService.class.cast(contextsService).getRequestContext(false);
    if (requestContext == null) {
        contextsService.startContext(RequestScoped.class, CdiAppContextsService.EJB_REQUEST_EVENT);
        newContext.set(DestroyContext.class, new DestroyContext(contextsService, newContext));
    }
}
Also used : WebBeansContext(org.apache.webbeans.config.WebBeansContext) BeanContext(org.apache.openejb.BeanContext) Context(javax.enterprise.context.spi.Context) ThreadContext(org.apache.openejb.core.ThreadContext) BeanContext(org.apache.openejb.BeanContext) ContextsService(org.apache.webbeans.spi.ContextsService) WebBeansContext(org.apache.webbeans.config.WebBeansContext)

Example 9 with ContextsService

use of org.apache.webbeans.spi.ContextsService in project deltaspike by apache.

the class OpenWebBeansContextControl method startConversationScope.

private void startConversationScope() {
    ContextsService contextsService = getContextsService();
    contextsService.startContext(ConversationScoped.class, null);
}
Also used : ContextsService(org.apache.webbeans.spi.ContextsService)

Example 10 with ContextsService

use of org.apache.webbeans.spi.ContextsService in project deltaspike by apache.

the class OpenWebBeansContextControl method startContexts.

@Override
public void startContexts() {
    ContextsService contextsService = getContextsService();
    startSingletonScope();
    startApplicationScope();
    startSessionScope();
    startRequestScope();
    startConversationScope();
}
Also used : ContextsService(org.apache.webbeans.spi.ContextsService)

Aggregations

ContextsService (org.apache.webbeans.spi.ContextsService)19 RequestScoped (javax.enterprise.context.RequestScoped)4 WebBeansContext (org.apache.webbeans.config.WebBeansContext)4 Test (org.junit.Test)4 AppContext (org.apache.openejb.AppContext)3 BeanContext (org.apache.openejb.BeanContext)3 IOException (java.io.IOException)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 SessionScoped (javax.enterprise.context.SessionScoped)2 Context (javax.enterprise.context.spi.Context)2 JMSContext (javax.jms.JMSContext)2 JMSRuntimeException (javax.jms.JMSRuntimeException)2 Context (javax.naming.Context)2 InitialContext (javax.naming.InitialContext)2 NamingException (javax.naming.NamingException)2 OpenEJBException (org.apache.openejb.OpenEJBException)2 OpenEJBRuntimeException (org.apache.openejb.OpenEJBRuntimeException)2 WebContext (org.apache.openejb.core.WebContext)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1