Search in sources :

Example 6 with BaseContainer

use of com.sun.ejb.containers.BaseContainer in project Payara by payara.

the class PersistentEJBTimerService method _restoreTimers.

/**
 * The portion of timer restoration that deals with registering the
 * JDK timer tasks and checking for missed expirations.
 * @return the Set of restored timers
 */
private Set<TimerState> _restoreTimers(Set<TimerState> timersEligibleForRestoration) {
    // Do timer restoration in two passes.  The first pass updates
    // the timer cache with each timer.  The second pass schedules
    // the JDK timer tasks.
    Map timersToRestore = new HashMap();
    Set timerIdsToRemove = new HashSet();
    Set<TimerState> result = new HashSet<TimerState>();
    for (TimerState timer : timersEligibleForRestoration) {
        TimerPrimaryKey timerId = getPrimaryKey(timer);
        if (getTimerState(timerId) != null) {
            // Already restored. Add it to the result but do nothing else.
            logger.log(Level.FINE, "@@@ Timer already restored: " + timer);
            result.add(timer);
            continue;
        }
        long containerId = timer.getContainerId();
        // Timer might refer to an obsolete container.
        BaseContainer container = getContainer(containerId);
        if (container != null) {
            // Update applicationId if it is null (from previous version)
            long appid = timer.getApplicationId();
            if (appid == 0) {
                timer.setApplicationId(container.getApplicationId());
            }
            // End update
            Date initialExpiration = timer.getInitialExpiration();
            // Create an instance of RuntimeTimerState.
            // Only access timedObjectPrimaryKey if timed object is
            // an entity bean.  That allows us to lazily load the underlying
            // blob for stateless session and message-driven bean timers.
            Object timedObjectPrimaryKey = null;
            if (container.getContainerType() == BaseContainer.ContainerType.ENTITY) {
                timedObjectPrimaryKey = timer.getTimedObjectPrimaryKey();
            }
            RuntimeTimerState timerState = new RuntimeTimerState(timerId, initialExpiration, timer.getIntervalDuration(), container, timedObjectPrimaryKey, timer.getTimerSchedule(), // Don't need to store the info ref for persistent timer
            null, true);
            timerCache_.addTimer(timerId, timerState);
            // If a single-action timer is still in the database it never
            // successfully delivered, so always reschedule a timer task
            // for it.  For periodic timers, we use the last known
            // expiration time to decide whether we need to fire one
            // ejbTimeout to make up for any missed ones.
            Date expirationTime = initialExpiration;
            Date now = new Date();
            if (timerState.isPeriodic()) {
                // lastExpiration time, or null if we either aren't
                // tracking last expiration or an expiration hasn't
                // occurred yet for this timer.
                Date lastExpiration = timer.getLastExpiration();
                EJBTimerSchedule ts = timer.getTimerSchedule();
                if ((lastExpiration == null) && now.after(initialExpiration)) {
                    if (!timerState.isExpired()) {
                        // This timer didn't even expire one time.
                        logger.log(Level.INFO, "Rescheduling missed expiration for " + "periodic timer " + timerState + ". Timer expirations should " + " have been delivered starting at " + initialExpiration);
                    }
                // keep expiration time at initialExpiration.  That
                // will force an ejbTimeout almost immediately. After
                // that the timer will return to fixed rate expiration.
                } else if ((lastExpiration != null) && ((ts != null && ts.getNextTimeout(lastExpiration).getTimeInMillis() < now.getTime()) || ((ts == null) && now.getTime() - lastExpiration.getTime() > timer.getIntervalDuration()))) {
                    // Schedule-based timer is periodic
                    logger.log(Level.INFO, "Rescheduling missed expiration for " + "periodic timer " + timerState + ".  Last timer expiration " + "occurred at " + lastExpiration);
                // Timer expired at least once and at least one
                // missed expiration has occurred.
                // keep expiration time at initialExpiration.  That
                // will force an ejbTimeout almost immediately. After
                // that the timer will return to fixed rate expiration.
                } else {
                    // In this case, at least one expiration has occurred
                    // but that was less than one period ago so there were
                    // no missed expirations.
                    expirationTime = calcNextFixedRateExpiration(timerState);
                }
            } else {
                if (now.after(initialExpiration)) {
                    logger.log(Level.INFO, "Rescheduling missed expiration for " + "single-action timer " + timerState + ". Timer expiration should " + " have been delivered at " + initialExpiration);
                }
            }
            if (expirationTime == null) {
                // Schedule-based timer will never expire again - remove it.
                logger.log(Level.INFO, "Removing schedule-based timer " + timerState + " that will never expire again");
                timerIdsToRemove.add(timerId);
            } else {
                timersToRestore.put(timerState, expirationTime);
                result.add(timer);
            }
        } else {
            // Timed object's container no longer exists - remember its id.
            logger.log(Level.FINE, "Skipping timer " + timerId + " for container that is not up: " + containerId);
        }
    }
    if (timerIdsToRemove.size() > 0) {
        timerLocal_.remove(timerIdsToRemove);
    }
    for (Iterator entries = timersToRestore.entrySet().iterator(); entries.hasNext(); ) {
        Map.Entry next = (Map.Entry) entries.next();
        RuntimeTimerState nextTimer = (RuntimeTimerState) next.getKey();
        TimerPrimaryKey timerId = nextTimer.getTimerId();
        Date expiration = (Date) next.getValue();
        scheduleTask(timerId, expiration);
        logger.log(Level.FINE, "EJBTimerService.restoreTimers(), scheduling timer " + nextTimer);
    }
    logger.log(Level.FINE, "DONE EJBTimerService.restoreTimers()");
    return result;
}
Also used : TimerPrimaryKey(com.sun.ejb.containers.TimerPrimaryKey) RuntimeTimerState(com.sun.ejb.containers.RuntimeTimerState) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) EJBTimerSchedule(com.sun.ejb.containers.EJBTimerSchedule) Date(java.util.Date) BaseContainer(com.sun.ejb.containers.BaseContainer) Iterator(java.util.Iterator) RuntimeTimerState(com.sun.ejb.containers.RuntimeTimerState) Map(java.util.Map) HashMap(java.util.HashMap) HashSet(java.util.HashSet)

Example 7 with BaseContainer

use of com.sun.ejb.containers.BaseContainer in project Payara by payara.

the class JCDIServiceImpl method _createJCDIInjectionContext.

// instance could be null. If null, create a new one
@SuppressWarnings("unchecked")
private <T> JCDIInjectionContext<T> _createJCDIInjectionContext(EjbDescriptor ejb, T instance, Map<Class<?>, Object> ejbInfo) {
    BaseContainer baseContainer = null;
    EJBContextImpl ejbContext = null;
    JCDIInjectionContextImpl<T> jcdiCtx = null;
    CreationalContext<T> creationalContext = null;
    if (ejbInfo != null) {
        baseContainer = (BaseContainer) ejbInfo.get(BaseContainer.class);
        ejbContext = (EJBContextImpl) ejbInfo.get(EJBContextImpl.class);
    }
    BundleDescriptor topLevelBundleDesc = (BundleDescriptor) ejb.getEjbBundleDescriptor().getModuleDescriptor().getDescriptor();
    // First get BeanDeploymentArchive for this ejb
    BeanDeploymentArchive bda = getBDAForBeanClass(topLevelBundleDesc, ejb.getEjbClassName());
    WeldBootstrap bootstrap = weldDeployer.getBootstrapForApp(ejb.getEjbBundleDescriptor().getApplication());
    WeldManager weldManager = bootstrap.getManager(bda);
    // when calling _createJCDIInjectionContext
    if (weldManager == null) {
        logger.severe("The reference for weldManager is not available, this is an un-sync state of the container");
        return null;
    }
    org.jboss.weld.ejb.spi.EjbDescriptor<T> ejbDesc = weldManager.getEjbDescriptor(ejb.getName());
    // get or create the ejb's creational context
    if (null != ejbInfo) {
        jcdiCtx = (JCDIInjectionContextImpl<T>) ejbInfo.get(JCDIService.JCDIInjectionContext.class);
    }
    if (null != jcdiCtx) {
        creationalContext = jcdiCtx.getCreationalContext();
    }
    if (null != jcdiCtx && creationalContext == null) {
        // The creational context may have been created by interceptors because they are created first
        // (see createInterceptorInstance below.)
        // And we only want to create the ejb's creational context once or we will have a memory
        // leak there too.
        Bean<T> bean = weldManager.getBean(ejbDesc);
        creationalContext = weldManager.createCreationalContext(bean);
        jcdiCtx.setCreationalContext(creationalContext);
    }
    // Create the injection target
    InjectionTarget<T> it = null;
    if (ejbDesc.isMessageDriven()) {
        // message driven beans are non-contextual and therefore createInjectionTarget is not appropriate
        it = createMdbInjectionTarget(weldManager, ejbDesc);
    } else {
        it = weldManager.createInjectionTarget(ejbDesc);
    }
    if (null != jcdiCtx) {
        jcdiCtx.setInjectionTarget(it);
    }
    // JJS: 7/20/17 We must perform the around_construct interception because Weld does not know about
    // interceptors defined by descriptors.
    WeldCreationalContext<T> weldCreationalContext = (WeldCreationalContext<T>) creationalContext;
    weldCreationalContext.setConstructorInterceptionSuppressed(true);
    JCDIAroundConstructCallback<T> aroundConstructCallback = new JCDIAroundConstructCallback<>(baseContainer, ejbContext);
    weldCreationalContext.registerAroundConstructCallback(aroundConstructCallback);
    if (null != jcdiCtx) {
        jcdiCtx.setJCDIAroundConstructCallback(aroundConstructCallback);
    }
    T beanInstance = instance;
    if (null != jcdiCtx) {
        jcdiCtx.setInstance(beanInstance);
    }
    return jcdiCtx;
// Injection is not performed yet. Separate injectEJBInstance() call is required.
}
Also used : JCDIService(com.sun.enterprise.container.common.spi.JCDIService) EJBContextImpl(com.sun.ejb.containers.EJBContextImpl) WeldBootstrap(org.jboss.weld.bootstrap.WeldBootstrap) javax.enterprise.inject.spi(javax.enterprise.inject.spi) WeldManager(org.jboss.weld.manager.api.WeldManager) BaseContainer(com.sun.ejb.containers.BaseContainer) BundleDescriptor(com.sun.enterprise.deployment.BundleDescriptor) WeldCreationalContext(org.jboss.weld.contexts.WeldCreationalContext) BeanDeploymentArchive(org.jboss.weld.bootstrap.spi.BeanDeploymentArchive)

Aggregations

BaseContainer (com.sun.ejb.containers.BaseContainer)7 EJBTimerSchedule (com.sun.ejb.containers.EJBTimerSchedule)3 RuntimeTimerState (com.sun.ejb.containers.RuntimeTimerState)3 TimerPrimaryKey (com.sun.ejb.containers.TimerPrimaryKey)3 Date (java.util.Date)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 Map (java.util.Map)3 IMap (com.hazelcast.map.IMap)2 NamingException (javax.naming.NamingException)2 EJBContextImpl (com.sun.ejb.containers.EJBContextImpl)1 EjbContainerUtil (com.sun.ejb.containers.EjbContainerUtil)1 GenericEJBLocalHome (com.sun.ejb.containers.GenericEJBLocalHome)1 RemoteBusinessWrapperBase (com.sun.ejb.containers.RemoteBusinessWrapperBase)1 JCDIService (com.sun.enterprise.container.common.spi.JCDIService)1 BundleDescriptor (com.sun.enterprise.deployment.BundleDescriptor)1 EjbDescriptor (com.sun.enterprise.deployment.EjbDescriptor)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1