Search in sources :

Example 11 with TimerImpl

use of org.jboss.as.ejb3.timerservice.TimerImpl in project wildfly by wildfly.

the class EjbTimerXmlParser_1_0 method parseCalendarTimer.

private void parseCalendarTimer(XMLExtendedStreamReader reader, List<TimerImpl> timers) throws XMLStreamException {
    LoadableElements loadableElements = new LoadableElements();
    CalendarTimer.Builder builder = CalendarTimer.builder();
    builder.setAutoTimer(false).setPersistent(true);
    final Set<String> required = new HashSet<>(Arrays.asList(new String[] { TIMED_OBJECT_ID, TIMER_ID, TIMER_STATE, SCHEDULE_EXPR_SECOND, SCHEDULE_EXPR_MINUTE, SCHEDULE_EXPR_HOUR, SCHEDULE_EXPR_DAY_OF_WEEK, SCHEDULE_EXPR_DAY_OF_MONTH, SCHEDULE_EXPR_MONTH, SCHEDULE_EXPR_YEAR }));
    final ScheduleExpression scheduleExpression = new ScheduleExpression();
    for (int i = 0; i < reader.getAttributeCount(); ++i) {
        String attr = reader.getAttributeValue(i);
        String attrName = reader.getAttributeLocalName(i);
        required.remove(attrName);
        boolean handled = handleCommonAttributes(builder, reader, i);
        if (!handled) {
            switch(attrName) {
                case SCHEDULE_EXPR_SECOND:
                    scheduleExpression.second(attr);
                    break;
                case SCHEDULE_EXPR_MINUTE:
                    scheduleExpression.minute(attr);
                    break;
                case SCHEDULE_EXPR_HOUR:
                    scheduleExpression.hour(attr);
                    break;
                case SCHEDULE_EXPR_DAY_OF_WEEK:
                    scheduleExpression.dayOfWeek(attr);
                    break;
                case SCHEDULE_EXPR_DAY_OF_MONTH:
                    scheduleExpression.dayOfMonth(attr);
                    break;
                case SCHEDULE_EXPR_MONTH:
                    scheduleExpression.month(attr);
                    break;
                case SCHEDULE_EXPR_YEAR:
                    scheduleExpression.year(attr);
                    break;
                case SCHEDULE_EXPR_START_DATE:
                    scheduleExpression.start(new Date(Long.parseLong(attr)));
                    break;
                case SCHEDULE_EXPR_END_DATE:
                    scheduleExpression.end(new Date(Long.parseLong(attr)));
                    break;
                case SCHEDULE_EXPR_TIMEZONE:
                    scheduleExpression.timezone(attr);
                    break;
                default:
                    throw ParseUtils.unexpectedAttribute(reader, i);
            }
        }
    }
    if (!required.isEmpty()) {
        throw ParseUtils.missingRequired(reader, required);
    }
    builder.setScheduleExpression(scheduleExpression);
    while (reader.hasNext()) {
        switch(reader.nextTag()) {
            case END_ELEMENT:
                {
                    try {
                        if (loadableElements.info != null) {
                            builder.setInfo((Serializable) deserialize(loadableElements.info));
                        }
                        if (loadableElements.methodName != null) {
                            Method timeoutMethod = CalendarTimer.getTimeoutMethod(new TimeoutMethod(loadableElements.className, loadableElements.methodName, loadableElements.params.toArray(new String[loadableElements.params.size()])), classLoader);
                            if (timeoutMethod != null) {
                                builder.setTimeoutMethod(timeoutMethod);
                                timers.add(builder.build(timerService));
                            } else {
                                builder.setId("deleted-timer");
                                timers.add(builder.build(timerService));
                                EjbLogger.EJB3_TIMER_LOGGER.timerReinstatementFailed(builder.getTimedObjectId(), builder.getId(), null);
                            }
                        } else {
                            timers.add(builder.build(timerService));
                        }
                    } catch (Exception e) {
                        EjbLogger.EJB3_TIMER_LOGGER.timerReinstatementFailed(builder.getTimedObjectId(), builder.getId(), e);
                    }
                    return;
                }
            case START_ELEMENT:
                {
                    boolean handled = handleCommonElements(reader, loadableElements);
                    if (!handled) {
                        switch(reader.getName().getLocalPart()) {
                            case TIMEOUT_METHOD:
                                {
                                    builder.setAutoTimer(true);
                                    parseTimeoutMethod(reader, loadableElements);
                                    break;
                                }
                            default:
                                throw ParseUtils.unexpectedElement(reader);
                        }
                    }
                }
        }
    }
}
Also used : ScheduleExpression(javax.ejb.ScheduleExpression) Serializable(java.io.Serializable) CalendarTimer(org.jboss.as.ejb3.timerservice.CalendarTimer) TimeoutMethod(org.jboss.as.ejb3.timerservice.persistence.TimeoutMethod) Method(java.lang.reflect.Method) TimeoutMethod(org.jboss.as.ejb3.timerservice.persistence.TimeoutMethod) Date(java.util.Date) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException) HashSet(java.util.HashSet)

Example 12 with TimerImpl

use of org.jboss.as.ejb3.timerservice.TimerImpl in project wildfly by wildfly.

the class FileTimerPersistence method loadTimersFromFile.

private Map<String, TimerImpl> loadTimersFromFile(String timedObjectId, TimerServiceImpl timerService) {
    Map<String, TimerImpl> timers = new HashMap<>();
    String directory = getDirectory(timedObjectId);
    timers.putAll(LegacyFileStore.loadTimersFromFile(timedObjectId, timerService, directory, factory, configuration));
    for (Map.Entry<String, TimerImpl> entry : timers.entrySet()) {
        // write legacy timers into the new format
        writeFile(entry.getValue());
    // the legacy code handling code will write a marker file, to make sure that the old timers will not be loaded on next restart.
    }
    final File file = new File(directory);
    if (!file.exists()) {
        // no timers exist yet
        return timers;
    } else if (!file.isDirectory()) {
        EJB3_TIMER_LOGGER.failToRestoreTimers(file);
        return timers;
    }
    final XMLMapper mapper = createMapper(timerService);
    for (File timerFile : file.listFiles()) {
        if (!timerFile.getName().endsWith(".xml")) {
            continue;
        }
        FileInputStream in = null;
        try {
            in = new FileInputStream(timerFile);
            final XMLInputFactory inputFactory = INPUT_FACTORY;
            setIfSupported(inputFactory, XMLInputFactory.IS_VALIDATING, Boolean.FALSE);
            setIfSupported(inputFactory, XMLInputFactory.SUPPORT_DTD, Boolean.FALSE);
            final XMLStreamReader streamReader = inputFactory.createXMLStreamReader(in);
            try {
                List<TimerImpl> timerList = new ArrayList<>();
                mapper.parseDocument(timerList, streamReader);
                for (TimerImpl timer : timerList) {
                    if (timer.getId().equals("deleted-timer")) {
                        timerFile.delete();
                        break;
                    }
                    timers.put(timer.getId(), timer);
                }
            } finally {
                safeClose(in);
            }
        } catch (Exception e) {
            EJB3_TIMER_LOGGER.failToRestoreTimersFromFile(timerFile, e);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    EJB3_TIMER_LOGGER.failToCloseFile(e);
                }
            }
        }
    }
    return timers;
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ArrayList(java.util.ArrayList) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) SystemException(javax.transaction.SystemException) XMLMapper(org.jboss.staxmapper.XMLMapper) TimerImpl(org.jboss.as.ejb3.timerservice.TimerImpl) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) File(java.io.File) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 13 with TimerImpl

use of org.jboss.as.ejb3.timerservice.TimerImpl in project wildfly by wildfly.

the class DatabaseTimerPersistence method loadTimer.

/**
 * Loads a timer from database by its id and timed object id.
 *
 * @param timedObjectId the timed object id for the timer
 * @param timerId the timer id
 * @param timerService the active timer service
 * @return the timer loaded from database; null if nothing can be loaded
 */
public TimerImpl loadTimer(final String timedObjectId, final String timerId, final TimerServiceImpl timerService) {
    String loadTimer = sql.getProperty(LOAD_TIMER);
    Connection connection = null;
    PreparedStatement preparedStatement = null;
    ResultSet resultSet = null;
    TimerImpl timer = null;
    try {
        connection = dataSource.getConnection();
        preparedStatement = connection.prepareStatement(loadTimer);
        preparedStatement.setString(1, timedObjectId);
        preparedStatement.setString(2, timerId);
        preparedStatement.setString(3, partition);
        resultSet = preparedStatement.executeQuery();
        if (resultSet.next()) {
            Holder holder = timerFromResult(resultSet, timerService, timerId, null);
            if (holder != null) {
                timer = holder.timer;
            }
        }
    } catch (SQLException e) {
        EjbLogger.EJB3_TIMER_LOGGER.failToRestoreTimersForObjectId(timerId, e);
    } finally {
        safeClose(resultSet);
        safeClose(preparedStatement);
        safeClose(connection);
    }
    return timer;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) TimerImpl(org.jboss.as.ejb3.timerservice.TimerImpl)

Example 14 with TimerImpl

use of org.jboss.as.ejb3.timerservice.TimerImpl in project wildfly by wildfly.

the class DatabaseTimerPersistence method statementParameters.

private void statementParameters(final TimerImpl timerEntity, final PreparedStatement statement) throws SQLException {
    statement.setString(1, timerEntity.getId());
    statement.setString(2, timerEntity.getTimedObjectId());
    statement.setTimestamp(3, timestamp(timerEntity.getInitialExpiration()));
    statement.setLong(4, timerEntity.getInterval());
    statement.setTimestamp(5, timestamp(timerEntity.getNextExpiration()));
    statement.setTimestamp(6, timestamp(timerEntity.getPreviousRun()));
    statement.setString(7, null);
    statement.setString(8, serialize(timerEntity.getTimerInfo()));
    statement.setString(9, timerEntity.getState().name());
    if (timerEntity instanceof CalendarTimer) {
        final CalendarTimer c = (CalendarTimer) timerEntity;
        statement.setString(10, c.getScheduleExpression().getSecond());
        statement.setString(11, c.getScheduleExpression().getMinute());
        statement.setString(12, c.getScheduleExpression().getHour());
        statement.setString(13, c.getScheduleExpression().getDayOfWeek());
        statement.setString(14, c.getScheduleExpression().getDayOfMonth());
        statement.setString(15, c.getScheduleExpression().getMonth());
        statement.setString(16, c.getScheduleExpression().getYear());
        // WFLY-9054: Oracle ojdbc6/7 store a timestamp as '06-JUL-17 01.54.00.269000000 PM'
        // but expect 'YYYY-MM-DD hh:mm:ss.fffffffff' as all other DB
        statement.setString(17, schedulerDateAsString(c.getScheduleExpression().getStart()));
        statement.setString(18, schedulerDateAsString(c.getScheduleExpression().getEnd()));
        statement.setString(19, c.getScheduleExpression().getTimezone());
        statement.setBoolean(20, false);
        statement.setString(21, null);
        statement.setString(22, null);
        statement.setString(23, null);
        statement.setBoolean(24, true);
    } else {
        statement.setString(10, null);
        statement.setString(11, null);
        statement.setString(12, null);
        statement.setString(13, null);
        statement.setString(14, null);
        statement.setString(15, null);
        statement.setString(16, null);
        statement.setTimestamp(17, null);
        statement.setTimestamp(18, null);
        statement.setString(19, null);
        statement.setBoolean(20, false);
        statement.setString(21, null);
        statement.setString(22, null);
        statement.setString(23, null);
        statement.setBoolean(24, false);
    }
    statement.setString(25, partition);
    setNodeName(timerEntity.getState(), statement, 26);
}
Also used : CalendarTimer(org.jboss.as.ejb3.timerservice.CalendarTimer)

Example 15 with TimerImpl

use of org.jboss.as.ejb3.timerservice.TimerImpl in project wildfly by wildfly.

the class DatabaseTimerPersistence method timerFromResult.

/**
 * Obtains a {@link Holder} from a row in {@code ResultSet}.
 * Caller of this method must get the timer id from the {@code ResultSet}
 * in advance and pass in as param {@code timerId}.
 * Caller of this method may get the timer state from the {@code ResultSet}
 * in advance and pass in as param {@code timerState}.
 *
 * @param resultSet  the {@code ResultSet} from database query
 * @param timerService  the associated {@code TimerServiceImpl}
 * @param timerId  the timer id, not null
 * @param timerState  the timer state from current row in the {@code ResultSet}, may be null
 * @return the {@code Holder} instance containing the timer from current {@code ResultSet} row
 * @throws SQLException on errors reading from {@code ResultSet}
 */
private Holder timerFromResult(final ResultSet resultSet, final TimerServiceImpl timerService, final String timerId, final TimerState timerState) throws SQLException {
    boolean calendarTimer = resultSet.getBoolean(24);
    final String nodeName = resultSet.getString(25);
    boolean requiresReset = false;
    TimerImpl.Builder builder;
    if (calendarTimer) {
        CalendarTimer.Builder cb = CalendarTimer.builder();
        builder = cb;
        // set calendar timer specifics first
        final ScheduleExpression scheduleExpression = new ScheduleExpression();
        scheduleExpression.second(resultSet.getString(10));
        scheduleExpression.minute(resultSet.getString(11));
        scheduleExpression.hour(resultSet.getString(12));
        scheduleExpression.dayOfWeek(resultSet.getString(13));
        scheduleExpression.dayOfMonth(resultSet.getString(14));
        scheduleExpression.month(resultSet.getString(15));
        scheduleExpression.year(resultSet.getString(16));
        scheduleExpression.start(stringAsSchedulerDate(resultSet.getString(17), timerId));
        scheduleExpression.end(stringAsSchedulerDate(resultSet.getString(18), timerId));
        scheduleExpression.timezone(resultSet.getString(19));
        cb.setScheduleExpression(scheduleExpression);
        cb.setAutoTimer(resultSet.getBoolean(20));
        final String clazz = resultSet.getString(21);
        final String methodName = resultSet.getString(22);
        if (methodName != null) {
            final String paramString = resultSet.getString(23);
            final String[] params = paramString == null || paramString.isEmpty() ? EMPTY_STRING_ARRAY : TIMER_PARAM_1_ARRAY;
            final Method timeoutMethod = CalendarTimer.getTimeoutMethod(new TimeoutMethod(clazz, methodName, params), timerService.getTimedObjectInvoker().getValue().getClassLoader());
            if (timeoutMethod == null) {
                EjbLogger.EJB3_TIMER_LOGGER.timerReinstatementFailed(resultSet.getString(2), timerId, new NoSuchMethodException());
                return null;
            }
            cb.setTimeoutMethod(timeoutMethod);
        }
    } else {
        builder = TimerImpl.builder();
    }
    builder.setId(timerId);
    builder.setTimedObjectId(resultSet.getString(2));
    builder.setInitialDate(resultSet.getTimestamp(3));
    builder.setRepeatInterval(resultSet.getLong(4));
    builder.setNextDate(resultSet.getTimestamp(5));
    builder.setPreviousRun(resultSet.getTimestamp(6));
    // builder.setPrimaryKey(deSerialize(resultSet.getString(7)));
    builder.setInfo((Serializable) deSerialize(resultSet.getString(8)));
    builder.setTimerState(timerState != null ? timerState : TimerState.valueOf(resultSet.getString(9)));
    builder.setPersistent(true);
    TimerImpl ret = builder.build(timerService);
    if (isClearTimerInfoCache(ret)) {
        ret.setCachedTimerInfo(Object.class);
        EjbLogger.EJB3_TIMER_LOGGER.debugf("Cleared timer info for timer: %s", timerId);
    }
    if (nodeName != null && nodeName.equals(this.nodeName) && (ret.getState() == TimerState.IN_TIMEOUT || ret.getState() == TimerState.RETRY_TIMEOUT)) {
        requiresReset = true;
    }
    return new Holder(ret, requiresReset);
}
Also used : ScheduleExpression(javax.ejb.ScheduleExpression) CalendarTimer(org.jboss.as.ejb3.timerservice.CalendarTimer) TimeoutMethod(org.jboss.as.ejb3.timerservice.persistence.TimeoutMethod) Method(java.lang.reflect.Method) TimeoutMethod(org.jboss.as.ejb3.timerservice.persistence.TimeoutMethod) TimerImpl(org.jboss.as.ejb3.timerservice.TimerImpl)

Aggregations

TimerImpl (org.jboss.as.ejb3.timerservice.TimerImpl)11 CalendarTimer (org.jboss.as.ejb3.timerservice.CalendarTimer)5 IOException (java.io.IOException)4 SystemException (javax.transaction.SystemException)4 Method (java.lang.reflect.Method)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 ScheduleExpression (javax.ejb.ScheduleExpression)3 TimeoutMethod (org.jboss.as.ejb3.timerservice.persistence.TimeoutMethod)3 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 Serializable (java.io.Serializable)2 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 Date (java.util.Date)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2