Search in sources :

Example 1 with TimerEntity

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

the class LegacyFileStore method loadTimersFromFile.

static Map<String, TimerImpl> loadTimersFromFile(final String timedObjectId, final TimerServiceImpl timerService, String directory, MarshallerFactory factory, MarshallingConfiguration configuration) {
    final Map<String, TimerImpl> timers = new HashMap<String, TimerImpl>();
    try {
        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;
        }
        File marker = new File(file, MIGRATION_MARKER);
        if (marker.exists()) {
            return timers;
        }
        Unmarshaller unmarshaller = factory.createUnmarshaller(configuration);
        for (File timerFile : file.listFiles()) {
            if (timerFile.getName().endsWith(".xml")) {
                continue;
            }
            FileInputStream in = null;
            try {
                in = new FileInputStream(timerFile);
                unmarshaller.start(new InputStreamByteInput(in));
                final TimerEntity entity = unmarshaller.readObject(TimerEntity.class);
                //we load the legacy timer entity class, and turn it into a timer state
                TimerImpl.Builder builder;
                if (entity instanceof CalendarTimerEntity) {
                    CalendarTimerEntity c = (CalendarTimerEntity) entity;
                    builder = CalendarTimer.builder().setScheduleExprSecond(c.getSecond()).setScheduleExprMinute(c.getMinute()).setScheduleExprHour(c.getHour()).setScheduleExprDayOfWeek(c.getDayOfWeek()).setScheduleExprDayOfMonth(c.getDayOfMonth()).setScheduleExprMonth(c.getMonth()).setScheduleExprYear(c.getYear()).setScheduleExprStartDate(c.getStartDate()).setScheduleExprEndDate(c.getEndDate()).setScheduleExprTimezone(c.getTimezone()).setAutoTimer(c.isAutoTimer()).setTimeoutMethod(CalendarTimer.getTimeoutMethod(c.getTimeoutMethod(), timerService.getTimedObjectInvoker().getValue().getClassLoader()));
                } else {
                    builder = TimerImpl.builder();
                }
                builder.setId(entity.getId()).setTimedObjectId(entity.getTimedObjectId()).setInitialDate(entity.getInitialDate()).setRepeatInterval(entity.getInterval()).setNextDate(entity.getNextDate()).setPreviousRun(entity.getPreviousRun()).setInfo(entity.getInfo()).setPrimaryKey(entity.getPrimaryKey()).setTimerState(entity.getTimerState()).setPersistent(true);
                timers.put(entity.getId(), builder.build(timerService));
                unmarshaller.finish();
            } catch (Exception e) {
                EJB3_TIMER_LOGGER.failToRestoreTimersFromFile(timerFile, e);
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        EJB3_TIMER_LOGGER.failToCloseFile(e);
                    }
                }
            }
        }
        if (!timers.isEmpty()) {
            FileOutputStream out = new FileOutputStream(marker);
            try {
                out.write(new Date().toString().getBytes());
            } finally {
                out.close();
            }
        }
    } catch (Exception e) {
        EJB3_TIMER_LOGGER.failToRestoreTimersForObjectId(timedObjectId, e);
    }
    return timers;
}
Also used : HashMap(java.util.HashMap) CalendarTimerEntity(org.jboss.as.ejb3.timerservice.persistence.CalendarTimerEntity) TimerEntity(org.jboss.as.ejb3.timerservice.persistence.TimerEntity) CalendarTimerEntity(org.jboss.as.ejb3.timerservice.persistence.CalendarTimerEntity) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) Date(java.util.Date) FileOutputStream(java.io.FileOutputStream) InputStreamByteInput(org.jboss.marshalling.InputStreamByteInput) TimerImpl(org.jboss.as.ejb3.timerservice.TimerImpl) Unmarshaller(org.jboss.marshalling.Unmarshaller) File(java.io.File)

Example 2 with TimerEntity

use of org.jboss.as.ejb3.timerservice.persistence.TimerEntity 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, serialize((Serializable) timerEntity.getPrimaryKey()));
    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());
        statement.setTimestamp(17, timestamp(c.getScheduleExpression().getStart()));
        statement.setTimestamp(18, timestamp(c.getScheduleExpression().getEnd()));
        statement.setString(19, c.getScheduleExpression().getTimezone());
        statement.setBoolean(20, c.isAutoTimer());
        if (c.isAutoTimer()) {
            statement.setString(21, c.getTimeoutMethod().getDeclaringClass().getName());
            statement.setString(22, c.getTimeoutMethod().getName());
            StringBuilder params = new StringBuilder();
            final Class<?>[] parameterTypes = c.getTimeoutMethod().getParameterTypes();
            for (int i = 0; i < parameterTypes.length; ++i) {
                params.append(parameterTypes[i].getName());
                if (i != parameterTypes.length - 1) {
                    params.append(";");
                }
            }
            statement.setString(23, params.toString());
        } else {
            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 : Serializable(java.io.Serializable) CalendarTimer(org.jboss.as.ejb3.timerservice.CalendarTimer)

Aggregations

File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 Serializable (java.io.Serializable)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 CalendarTimer (org.jboss.as.ejb3.timerservice.CalendarTimer)1 TimerImpl (org.jboss.as.ejb3.timerservice.TimerImpl)1 CalendarTimerEntity (org.jboss.as.ejb3.timerservice.persistence.CalendarTimerEntity)1 TimerEntity (org.jboss.as.ejb3.timerservice.persistence.TimerEntity)1 InputStreamByteInput (org.jboss.marshalling.InputStreamByteInput)1 Unmarshaller (org.jboss.marshalling.Unmarshaller)1