Search in sources :

Example 1 with EjbTimerServiceImpl

use of org.apache.openejb.core.timer.EjbTimerServiceImpl in project tomee by apache.

the class Assembler method initEjbs.

public List<BeanContext> initEjbs(final ClassLoader classLoader, final AppInfo appInfo, final AppContext appContext, final Set<Injection> injections, final List<BeanContext> allDeployments, final String webappId) throws OpenEJBException {
    final String globalTimersOn = SystemInstance.get().getProperty(OPENEJB_TIMERS_ON, "true");
    final EjbJarBuilder ejbJarBuilder = new EjbJarBuilder(props, appContext);
    for (final EjbJarInfo ejbJar : appInfo.ejbJars) {
        if (isSkip(appInfo, webappId, ejbJar)) {
            continue;
        }
        final HashMap<String, BeanContext> deployments = ejbJarBuilder.build(ejbJar, injections, classLoader);
        final JaccPermissionsBuilder jaccPermissionsBuilder = new JaccPermissionsBuilder();
        final PolicyContext policyContext = jaccPermissionsBuilder.build(ejbJar, deployments);
        jaccPermissionsBuilder.install(policyContext);
        final TransactionPolicyFactory transactionPolicyFactory = createTransactionPolicyFactory(ejbJar, classLoader);
        for (final BeanContext beanContext : deployments.values()) {
            beanContext.setTransactionPolicyFactory(transactionPolicyFactory);
        }
        final MethodTransactionBuilder methodTransactionBuilder = new MethodTransactionBuilder();
        methodTransactionBuilder.build(deployments, ejbJar.methodTransactions);
        final MethodConcurrencyBuilder methodConcurrencyBuilder = new MethodConcurrencyBuilder();
        methodConcurrencyBuilder.build(deployments, ejbJar.methodConcurrency);
        for (final BeanContext beanContext : deployments.values()) {
            containerSystem.addDeployment(beanContext);
        }
        // bind ejbs into global jndi
        jndiBuilder.build(ejbJar, deployments);
        // setup timers/asynchronous methods - must be after transaction attributes are set
        for (final BeanContext beanContext : deployments.values()) {
            if (beanContext.getComponentType() != BeanType.STATEFUL) {
                final Method ejbTimeout = beanContext.getEjbTimeout();
                boolean timerServiceRequired = false;
                if (ejbTimeout != null) {
                    // If user set the tx attribute to RequiresNew change it to Required so a new transaction is not started
                    if (beanContext.getTransactionType(ejbTimeout) == TransactionType.RequiresNew) {
                        beanContext.setMethodTransactionAttribute(ejbTimeout, TransactionType.Required);
                    }
                    timerServiceRequired = true;
                }
                for (final Iterator<Map.Entry<Method, MethodContext>> it = beanContext.iteratorMethodContext(); it.hasNext(); ) {
                    final Map.Entry<Method, MethodContext> entry = it.next();
                    final MethodContext methodContext = entry.getValue();
                    if (methodContext.getSchedules().size() > 0) {
                        timerServiceRequired = true;
                        final Method method = entry.getKey();
                        // TODO Need ?
                        if (beanContext.getTransactionType(method) == TransactionType.RequiresNew) {
                            beanContext.setMethodTransactionAttribute(method, TransactionType.Required);
                        }
                    }
                }
                if (timerServiceRequired && "true".equalsIgnoreCase(appInfo.properties.getProperty(OPENEJB_TIMERS_ON, globalTimersOn))) {
                    // Create the timer
                    final EjbTimerServiceImpl timerService = new EjbTimerServiceImpl(beanContext, newTimerStore(beanContext));
                    // Load auto-start timers
                    final TimerStore timerStore = timerService.getTimerStore();
                    for (final Iterator<Map.Entry<Method, MethodContext>> it = beanContext.iteratorMethodContext(); it.hasNext(); ) {
                        final Map.Entry<Method, MethodContext> entry = it.next();
                        final MethodContext methodContext = entry.getValue();
                        for (final ScheduleData scheduleData : methodContext.getSchedules()) {
                            timerStore.createCalendarTimer(timerService, (String) beanContext.getDeploymentID(), null, entry.getKey(), scheduleData.getExpression(), scheduleData.getConfig(), true);
                        }
                    }
                    beanContext.setEjbTimerService(timerService);
                } else {
                    beanContext.setEjbTimerService(new NullEjbTimerServiceImpl());
                }
            }
            // TODO ???
            for (final Iterator<Entry<Method, MethodContext>> it = beanContext.iteratorMethodContext(); it.hasNext(); ) {
                final Entry<Method, MethodContext> entry = it.next();
                if (entry.getValue().isAsynchronous() && beanContext.getTransactionType(entry.getKey()) == TransactionType.RequiresNew) {
                    beanContext.setMethodTransactionAttribute(entry.getKey(), TransactionType.Required);
                }
            }
            // if local bean or mdb generate proxy class now to avoid bottleneck on classloader later
            if (beanContext.isLocalbean() && !beanContext.getComponentType().isMessageDriven() && !beanContext.isDynamicallyImplemented()) {
                final List<Class> interfaces = new ArrayList<>(3);
                interfaces.add(Serializable.class);
                interfaces.add(IntraVmProxy.class);
                final BeanType type = beanContext.getComponentType();
                if (BeanType.STATEFUL.equals(type) || BeanType.MANAGED.equals(type)) {
                    interfaces.add(BeanContext.Removable.class);
                }
                beanContext.set(BeanContext.ProxyClass.class, new BeanContext.ProxyClass(beanContext, interfaces.toArray(new Class<?>[interfaces.size()])));
            }
        }
        // process application exceptions
        for (final ApplicationExceptionInfo exceptionInfo : ejbJar.applicationException) {
            try {
                final Class exceptionClass = classLoader.loadClass(exceptionInfo.exceptionClass);
                for (final BeanContext beanContext : deployments.values()) {
                    beanContext.addApplicationException(exceptionClass, exceptionInfo.rollback, exceptionInfo.inherited);
                }
            } catch (final ClassNotFoundException e) {
                logger.error("createApplication.invalidClass", e, exceptionInfo.exceptionClass, e.getMessage());
            }
        }
        allDeployments.addAll(deployments.values());
    }
    final List<BeanContext> ejbs = sort(allDeployments);
    for (final BeanContext b : ejbs) {
        // otherwise for ears we have duplicated beans
        if (appContext.getBeanContexts().contains(b)) {
            continue;
        }
        appContext.getBeanContexts().add(b);
    }
    return ejbs;
}
Also used : ScheduleData(org.apache.openejb.core.timer.ScheduleData) ArrayList(java.util.ArrayList) TimerStore(org.apache.openejb.core.timer.TimerStore) MemoryTimerStore(org.apache.openejb.core.timer.MemoryTimerStore) Entry(java.util.Map.Entry) JtaTransactionPolicyFactory(org.apache.openejb.core.transaction.JtaTransactionPolicyFactory) TransactionPolicyFactory(org.apache.openejb.core.transaction.TransactionPolicyFactory) MethodContext(org.apache.openejb.MethodContext) NullEjbTimerServiceImpl(org.apache.openejb.core.timer.NullEjbTimerServiceImpl) Method(java.lang.reflect.Method) EjbTimerServiceImpl(org.apache.openejb.core.timer.EjbTimerServiceImpl) NullEjbTimerServiceImpl(org.apache.openejb.core.timer.NullEjbTimerServiceImpl) BeanContext(org.apache.openejb.BeanContext) BeanType(org.apache.openejb.BeanType) Map(java.util.Map) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap)

Example 2 with EjbTimerServiceImpl

use of org.apache.openejb.core.timer.EjbTimerServiceImpl in project tomee by apache.

the class EntityContainer method cancelTimers.

private void cancelTimers(final ThreadContext threadContext) {
    final BeanContext beanContext = threadContext.getBeanContext();
    final Object primaryKey = threadContext.getPrimaryKey();
    // if we have a real timerservice, stop all timers. Otherwise, ignore...
    if (primaryKey != null) {
        final EjbTimerService timerService = beanContext.getEjbTimerService();
        if (timerService != null && timerService instanceof EjbTimerServiceImpl) {
            for (final Timer timer : beanContext.getEjbTimerService().getTimers(primaryKey)) {
                timer.cancel();
            }
        }
    }
}
Also used : BeanContext(org.apache.openejb.BeanContext) Timer(javax.ejb.Timer) EJBObject(javax.ejb.EJBObject) EJBLocalObject(javax.ejb.EJBLocalObject) EjbTimerServiceImpl(org.apache.openejb.core.timer.EjbTimerServiceImpl) EjbTimerService(org.apache.openejb.core.timer.EjbTimerService)

Example 3 with EjbTimerServiceImpl

use of org.apache.openejb.core.timer.EjbTimerServiceImpl in project tomee by apache.

the class EjbTimerImplSerializableTest method jobDataMapSerial.

@Test
public void jobDataMapSerial() throws Exception {
    final CalendarTimerData data = timerData();
    final EjbTimerServiceImpl timerService = (EjbTimerServiceImpl) timerService();
    data.setScheduler(timerService.getScheduler());
    // small hack for the test
    final Field preventSynch = TimerData.class.getDeclaredField("synchronizationRegistered");
    preventSynch.setAccessible(true);
    preventSynch.set(data, true);
    data.newTimer();
    final AbstractTrigger<?> trigger = (AbstractTrigger<?>) data.getTrigger();
    trigger.setJobName("my-job");
    trigger.setJobGroup("my-group");
    final JobDataMap triggerDataMap = trigger.getJobDataMap();
    triggerDataMap.put(EjbTimeoutJob.EJB_TIMERS_SERVICE, timerService);
    triggerDataMap.put(EjbTimeoutJob.TIMER_DATA, data);
    final byte[] serial = serialize(triggerDataMap);
    final JobDataMap map = (JobDataMap) deserialize(serial);
    assertTrue(map.containsKey(EjbTimeoutJob.EJB_TIMERS_SERVICE));
    assertTrue(map.containsKey(EjbTimeoutJob.TIMER_DATA));
}
Also used : CalendarTimerData(org.apache.openejb.core.timer.CalendarTimerData) Field(java.lang.reflect.Field) JobDataMap(org.apache.openejb.quartz.JobDataMap) AbstractTrigger(org.apache.openejb.quartz.impl.triggers.AbstractTrigger) EjbTimerServiceImpl(org.apache.openejb.core.timer.EjbTimerServiceImpl) Test(org.junit.Test)

Example 4 with EjbTimerServiceImpl

use of org.apache.openejb.core.timer.EjbTimerServiceImpl in project tomee by apache.

the class CmpContainer method cancelTimers.

private void cancelTimers(final ThreadContext threadContext) {
    final BeanContext beanContext = threadContext.getBeanContext();
    final Object primaryKey = threadContext.getPrimaryKey();
    // stop timers
    if (primaryKey != null && beanContext.getEjbTimerService() != null) {
        final EjbTimerService timerService = beanContext.getEjbTimerService();
        if (timerService != null && timerService instanceof EjbTimerServiceImpl) {
            for (final Timer timer : beanContext.getEjbTimerService().getTimers(primaryKey)) {
                timer.cancel();
            }
        }
    }
}
Also used : BeanContext(org.apache.openejb.BeanContext) Timer(javax.ejb.Timer) EJBLocalObject(javax.ejb.EJBLocalObject) EJBObject(javax.ejb.EJBObject) EjbTimerServiceImpl(org.apache.openejb.core.timer.EjbTimerServiceImpl) EjbTimerService(org.apache.openejb.core.timer.EjbTimerService)

Example 5 with EjbTimerServiceImpl

use of org.apache.openejb.core.timer.EjbTimerServiceImpl in project tomee by apache.

the class EjbTimerImplSerializableTest method serializeDeserialize.

@Test
public void serializeDeserialize() throws Exception {
    final EjbTimerService timer = timerService();
    assertNotNull(timer);
    assertThat(timer, instanceOf(EjbTimerServiceImpl.class));
    final byte[] serial = serialize(timer);
    final EjbTimerService timerDeserialized = (EjbTimerService) deserialize(serial);
    assertThat(timerDeserialized, instanceOf(EjbTimerServiceImpl.class));
    assertThat(((EjbTimerServiceImpl) timerDeserialized).getScheduler(), notNullValue());
    assertEqualsByReflection(timer, timerDeserialized, "deployment");
    assertEqualsByReflection(timer, timerDeserialized, "transacted");
    assertEqualsByReflection(timer, timerDeserialized, "retryAttempts");
}
Also used : EjbTimerServiceImpl(org.apache.openejb.core.timer.EjbTimerServiceImpl) EjbTimerService(org.apache.openejb.core.timer.EjbTimerService) Test(org.junit.Test)

Aggregations

EjbTimerServiceImpl (org.apache.openejb.core.timer.EjbTimerServiceImpl)5 BeanContext (org.apache.openejb.BeanContext)3 EjbTimerService (org.apache.openejb.core.timer.EjbTimerService)3 EJBLocalObject (javax.ejb.EJBLocalObject)2 EJBObject (javax.ejb.EJBObject)2 Timer (javax.ejb.Timer)2 Test (org.junit.Test)2 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 TreeMap (java.util.TreeMap)1 BeanType (org.apache.openejb.BeanType)1 MethodContext (org.apache.openejb.MethodContext)1 CalendarTimerData (org.apache.openejb.core.timer.CalendarTimerData)1 MemoryTimerStore (org.apache.openejb.core.timer.MemoryTimerStore)1 NullEjbTimerServiceImpl (org.apache.openejb.core.timer.NullEjbTimerServiceImpl)1 ScheduleData (org.apache.openejb.core.timer.ScheduleData)1