Example usage for javax.ejb Timer getNextTimeout

List of usage examples for javax.ejb Timer getNextTimeout

Introduction

In this page you can find the example usage for javax.ejb Timer getNextTimeout.

Prototype

public Date getNextTimeout() throws java.lang.IllegalStateException, javax.ejb.NoSuchObjectLocalException,
        javax.ejb.NoMoreTimeoutsException, javax.ejb.EJBException;

Source Link

Document

Get the point in time at which the next timer expiration is scheduled to occur.

Usage

From source file:edu.harvard.iq.dvn.core.index.IndexServiceBean.java

public void createIndexNotificationTimer() {
    for (Iterator it = timerService.getTimers().iterator(); it.hasNext();) {
        Timer timer = (Timer) it.next();
        if (timer.getInfo().equals(INDEX_NOTIFICATION_TIMER)) {
            logger.info("Cannot create IndexNotificationTimer, timer already exists.");
            logger.info("IndexNotificationTimer next timeout is " + timer.getNextTimeout());
            return;
        }/*from ww  w  . java 2  s  .c  o  m*/
    }
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.DAY_OF_YEAR, 1);
    cal.set(Calendar.HOUR_OF_DAY, 15);

    logger.log(Level.INFO, "Indexer notification timer set for " + cal.getTime());
    Date initialExpiration = cal.getTime(); // First timeout is 1:00 AM of next day
    long intervalDuration = 1000 * 60 * 60 * 24; // repeat every 24 hours
    timerService.createTimer(initialExpiration, intervalDuration, INDEX_NOTIFICATION_TIMER);

}

From source file:org.cesecore.certificates.ocsp.OcspResponseGeneratorSessionBean.java

/**
 * Adds a timer to the bean//from ww w .  j av a  2s .c  o  m
 * 
 * @param id the id of the timer
 */
// We don't want the appserver to persist/update the timer in the same transaction if they are stored in different non XA DataSources. This method
// should not be run from within a transaction.
private Timer addTimer(long interval, Integer id) {
    if (log.isTraceEnabled()) {
        log.trace(">addTimer: " + id + ", interval: " + interval);
    }
    Timer ret = null;
    if (interval > 0) {
        ret = timerService.createTimer(interval, id);
        if (log.isTraceEnabled()) {
            log.trace("<addTimer: " + id + ", interval: " + interval + ", " + ret.getNextTimeout().toString());
        }
    }
    return ret;
}

From source file:org.ejbca.core.ejb.services.ServiceSessionBean.java

/**
 * Method implemented from the TimerObject and is the main method of this session bean. It calls the work object for each object.
 * //from   ww w. j a v  a 2 s . com
 * @param timer timer whose expiration caused this notification.
 */
@Timeout
// Glassfish 2.1.1:
// "Timeout method ....timeoutHandler(javax.ejb.Timer)must have TX attribute of TX_REQUIRES_NEW or TX_REQUIRED or TX_NOT_SUPPORTED"
// JBoss 5.1.0.GA: We cannot mix timer updates with our EJBCA DataSource transactions.
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public void timeoutHandler(Timer timer) {
    if (log.isTraceEnabled()) {
        log.trace(">ejbTimeout");
    }
    final long startOfTimeOut = System.currentTimeMillis();
    long serviceInterval = IInterval.DONT_EXECUTE;
    Integer timerInfo = (Integer) timer.getInfo();
    if (timerInfo.equals(SERVICELOADER_ID)) {
        if (log.isDebugEnabled()) {
            log.debug("Running the internal Service loader.");
        }
        load();
    } else {
        String serviceName = null;
        try {
            serviceName = serviceDataSession.findNameById(timerInfo);
        } catch (Throwable t) { // NOPMD: we really need to catch everything to not risk hanging somewhere in limbo
            log.warn("Exception finding service name: ", t); // if this throws, there is a failed database or similar
            // Unexpected error (probably database related). We need to reschedule the service w a default interval.
            addTimer(30 * 1000, timerInfo);
        }
        if (serviceName == null) {
            final String msg = intres.getLocalizedMessage("services.servicenotfound", timerInfo);
            log.info(msg);
        } else {
            // Get interval of worker
            try {
                serviceInterval = serviceSession.getServiceInterval(timerInfo);
            } catch (Throwable t) { // NOPMD: we really need to catch everything to not risk hanging somewhere in limbo
                log.warn("Exception getting service interval: ", t); // if this throws, there is a failed database or similar
                // Unexpected error (probably database related). We need to reschedule the service w a default interval.
                addTimer(30 * 1000, timerInfo);
            }
            // Reschedule timer
            IWorker worker = null;
            if (serviceInterval != IInterval.DONT_EXECUTE) {
                Timer nextTrigger = addTimer(serviceInterval * 1000, timerInfo);
                try {
                    // Try to acquire lock / see if this node should run
                    worker = serviceSession.getWorkerIfItShouldRun(timerInfo,
                            nextTrigger.getNextTimeout().getTime());
                } catch (Throwable t) { // NOPMD: we really need to catch everything to not risk hanging somewhere in limbo
                    if (log.isDebugEnabled()) {
                        log.debug("Exception: ", t); // Don't spam log with stacktraces in normal production cases
                    }
                }
                if (worker != null) {
                    try {
                        serviceSession.executeServiceInNoTransaction(worker, serviceName);
                    } catch (RuntimeException e) {
                        /*
                         * If the service worker fails with a RuntimeException we need to
                         * swallow this here. If we allow it to propagate outside the
                         * ejbTimeout method it is up to the application server config how it
                         * should be retried, but we have already scheduled a new try
                         * previously in this method. We still want to log this as an ERROR
                         * since it is some kind of catastrophic failure..
                         */
                        log.error("Service worker execution failed.", e);
                    }
                } else {
                    if (log.isDebugEnabled()) {
                        Object o = timerInfo;
                        if (serviceName != null) {
                            o = serviceName;
                        }
                        final String msg = intres.getLocalizedMessage("services.servicerunonothernode", o);
                        log.debug(msg);
                    }
                }
                if (System.currentTimeMillis() - startOfTimeOut > serviceInterval * 1000) {
                    log.warn("Service '" + serviceName + "' took longer than it's configured service interval ("
                            + serviceInterval + ")."
                            + " This can trigger simultanious service execution on several nodes in a cluster."
                            + " Increase interval or lower each invocations work load.");
                }
            }
        }
    }
    if (log.isTraceEnabled()) {
        log.trace("<ejbTimeout");
    }
}