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();
}
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);
}
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;
}
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();
}
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();
}
Aggregations