Search in sources :

Example 1 with ContextControl

use of org.apache.deltaspike.cdise.api.ContextControl in project deltaspike by apache.

the class ContainerCtrlTckTest method testParallelThreadExecution.

@Test
public void testParallelThreadExecution() throws Exception {
    final CdiContainer cc = CdiContainerLoader.getCdiContainer();
    Assert.assertNotNull(cc);
    cc.boot();
    cc.getContextControl().startContexts();
    final BeanManager bm = cc.getBeanManager();
    Assert.assertNotNull(bm);
    final AtomicInteger numErrors = new AtomicInteger(0);
    final ContextControl contextControl = cc.getContextControl();
    Runnable runnable = new Runnable() {

        @Override
        public void run() {
            try {
                contextControl.startContext(SessionScoped.class);
                contextControl.startContext(RequestScoped.class);
                Set<Bean<?>> beans = bm.getBeans(CarRepair.class);
                Bean<?> bean = bm.resolve(beans);
                CarRepair carRepair = (CarRepair) bm.getReference(bean, CarRepair.class, bm.createCreationalContext(bean));
                Assert.assertNotNull(carRepair);
                for (int i = 0; i < 100000; i++) {
                    // we need the threads doing something ;)
                    Assert.assertNotNull(carRepair.getCar());
                    Assert.assertNotNull(carRepair.getCar().getUser());
                    Assert.assertNull(carRepair.getCar().getUser().getName());
                }
                contextControl.stopContext(RequestScoped.class);
                contextControl.stopContext(SessionScoped.class);
            } catch (Throwable e) {
                log.log(Level.SEVERE, "An exception happened on a new worker thread", e);
                numErrors.incrementAndGet();
            }
        }
    };
    Thread[] threads = new Thread[NUM_THREADS];
    for (int i = 0; i < NUM_THREADS; i++) {
        threads[i] = new Thread(runnable);
    }
    for (int i = 0; i < NUM_THREADS; i++) {
        threads[i].start();
    }
    for (int i = 0; i < NUM_THREADS; i++) {
        threads[i].join();
    }
    Assert.assertEquals("An error happened while executing parallel threads", 0, numErrors.get());
    cc.shutdown();
}
Also used : Bean(javax.enterprise.inject.spi.Bean) CarRepair(org.apache.deltaspike.cdise.tck.beans.CarRepair) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContextControl(org.apache.deltaspike.cdise.api.ContextControl) BeanManager(javax.enterprise.inject.spi.BeanManager) CdiContainer(org.apache.deltaspike.cdise.api.CdiContainer) Test(org.junit.Test)

Example 2 with ContextControl

use of org.apache.deltaspike.cdise.api.ContextControl in project deltaspike by apache.

the class CdiServletRequestListener method requestDestroyed.

@Override
public void requestDestroyed(ServletRequestEvent servletRequestEvent) {
    LOG.log(Level.FINER, "Request done.");
    ContextControl contextControl = (ContextControl) servletRequestEvent.getServletRequest().getAttribute(CDI_REQ_CONTEXT);
    contextControl.stopContext(RequestScoped.class);
}
Also used : ContextControl(org.apache.deltaspike.cdise.api.ContextControl)

Example 3 with ContextControl

use of org.apache.deltaspike.cdise.api.ContextControl in project deltaspike by apache.

the class OpenWebBeansContainerControl method getContextControl.

@Override
public synchronized ContextControl getContextControl() {
    if (ctxCtrl == null) {
        BeanManager beanManager = getBeanManager();
        if (beanManager == null) {
            LOG.warning("If the CDI-container was started by the environment, you can't use this helper." + "Instead you can resolve ContextControl manually " + "(e.g. via BeanProvider.getContextualReference(ContextControl.class) ). " + "If the container wasn't started already, you have to use CdiContainer#boot before.");
            return null;
        }
        Set<Bean<?>> beans = beanManager.getBeans(ContextControl.class);
        ctxCtrlBean = (Bean<ContextControl>) beanManager.resolve(beans);
        ctxCtrlCreationalContext = getBeanManager().createCreationalContext(ctxCtrlBean);
        ctxCtrl = (ContextControl) getBeanManager().getReference(ctxCtrlBean, ContextControl.class, ctxCtrlCreationalContext);
    }
    return ctxCtrl;
}
Also used : ContextControl(org.apache.deltaspike.cdise.api.ContextControl) BeanManager(javax.enterprise.inject.spi.BeanManager) Bean(javax.enterprise.inject.spi.Bean)

Example 4 with ContextControl

use of org.apache.deltaspike.cdise.api.ContextControl in project deltaspike by apache.

the class SimpleSchedulerExample method main.

public static void main(String[] args) throws InterruptedException {
    CdiContainer cdiContainer = CdiContainerLoader.getCdiContainer();
    cdiContainer.boot();
    ContextControl contextControl = cdiContainer.getContextControl();
    contextControl.startContext(ApplicationScoped.class);
    GlobalResultHolder globalResultHolder = BeanProvider.getContextualReference(GlobalResultHolder.class);
    while (globalResultHolder.getCount() < 100) {
        Thread.sleep(500);
        LOG.info("current count: " + globalResultHolder.getCount());
    }
    LOG.info("completed!");
    contextControl.stopContext(ApplicationScoped.class);
    cdiContainer.shutdown();
}
Also used : ContextControl(org.apache.deltaspike.cdise.api.ContextControl) CdiContainer(org.apache.deltaspike.cdise.api.CdiContainer)

Example 5 with ContextControl

use of org.apache.deltaspike.cdise.api.ContextControl in project deltaspike by apache.

the class ConfigExample method main.

public static void main(String[] args) {
    CdiContainer cdiContainer = CdiContainerLoader.getCdiContainer();
    cdiContainer.boot();
    ContextControl contextControl = cdiContainer.getContextControl();
    contextControl.startContext(ApplicationScoped.class);
    SettingsBean settingsBean = BeanProvider.getContextualReference(SettingsBean.class, false);
    LOG.info("configured int-value #1: " + settingsBean.getIntProperty1());
    LOG.info("configured long-value #2: " + settingsBean.getProperty2());
    LOG.info("configured inverse-value #2: " + settingsBean.getInverseProperty());
    LOG.info("configured location (custom config): " + settingsBean.getLocationId().name());
    cdiContainer.shutdown();
}
Also used : ContextControl(org.apache.deltaspike.cdise.api.ContextControl) CdiContainer(org.apache.deltaspike.cdise.api.CdiContainer)

Aggregations

ContextControl (org.apache.deltaspike.cdise.api.ContextControl)9 Bean (javax.enterprise.inject.spi.Bean)4 BeanManager (javax.enterprise.inject.spi.BeanManager)4 CdiContainer (org.apache.deltaspike.cdise.api.CdiContainer)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 CarRepair (org.apache.deltaspike.cdise.tck.beans.CarRepair)1 DefaultEchoService (org.apache.deltaspike.example.echo.DefaultEchoService)1 EchoService (org.apache.deltaspike.example.echo.EchoService)1 OptionalService (org.apache.deltaspike.example.optional.OptionalService)1 Test (org.junit.Test)1