Search in sources :

Example 1 with TimeoutMethod

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

the class DatabaseTimerPersistence method addAutoTimer.

private void addAutoTimer(final CalendarTimer timer) {
    String createTimer = sql.getProperty(CREATE_AUTO_TIMER);
    Connection connection = null;
    PreparedStatement statement = null;
    final String timerInfoString = serialize(timer.getTimerInfo());
    final Method timeoutMethod = timer.getTimeoutMethod();
    final String timeoutMethodClassName = timeoutMethod.getDeclaringClass().getName();
    final String timeoutMethodParam = timeoutMethod.getParameterCount() == 0 ? null : TIMER_PARAM_1;
    final ScheduleExpression exp = timer.getScheduleExpression();
    final String startDateString = schedulerDateAsString(exp.getStart());
    final String endDateString = schedulerDateAsString(exp.getEnd());
    try {
        connection = dataSource.getConnection();
        statement = connection.prepareStatement(createTimer);
        // insert values
        statement.setString(1, timer.getId());
        statement.setString(2, timer.getTimedObjectId());
        statement.setTimestamp(3, timestamp(timer.getNextExpiration()));
        statement.setString(4, timerInfoString);
        statement.setString(5, exp.getSecond());
        statement.setString(6, exp.getMinute());
        statement.setString(7, exp.getHour());
        statement.setString(8, exp.getDayOfWeek());
        statement.setString(9, exp.getDayOfMonth());
        statement.setString(10, exp.getMonth());
        statement.setString(11, exp.getYear());
        statement.setString(12, startDateString);
        statement.setString(13, endDateString);
        statement.setString(14, exp.getTimezone());
        statement.setBoolean(15, true);
        statement.setString(16, timeoutMethodClassName);
        statement.setString(17, timeoutMethod.getName());
        statement.setString(18, timeoutMethodParam);
        statement.setBoolean(19, true);
        statement.setString(20, partition);
        // where clause
        statement.setString(21, timer.getTimedObjectId());
        statement.setString(22, exp.getSecond());
        statement.setString(23, exp.getMinute());
        statement.setString(24, exp.getHour());
        statement.setString(25, exp.getDayOfWeek());
        statement.setString(26, exp.getDayOfMonth());
        statement.setString(27, exp.getMonth());
        statement.setString(28, exp.getYear());
        statement.setString(29, startDateString);
        statement.setString(30, startDateString);
        statement.setString(31, endDateString);
        statement.setString(32, endDateString);
        statement.setString(33, exp.getTimezone());
        statement.setString(34, exp.getTimezone());
        statement.setString(35, timeoutMethodClassName);
        statement.setString(36, timeoutMethod.getName());
        statement.setString(37, timeoutMethodParam);
        statement.setString(38, timeoutMethodParam);
        statement.setString(39, partition);
        int affectedRows = statement.executeUpdate();
        if (affectedRows < 1) {
            timer.setTimerState(TimerState.CANCELED, null);
        } else {
            synchronized (this) {
                knownTimerIds.get(timer.getTimedObjectId()).add(timer.getId());
            }
        }
    } catch (SQLException e) {
        throw new RuntimeException(e);
    } finally {
        safeClose(statement);
        safeClose(connection);
    }
}
Also used : ScheduleExpression(javax.ejb.ScheduleExpression) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Method(java.lang.reflect.Method) TimeoutMethod(org.jboss.as.ejb3.timerservice.persistence.TimeoutMethod)

Example 2 with TimeoutMethod

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

the class AbstractEJBComponentRuntimeHandler method executeReadAttribute.

protected void executeReadAttribute(final String attributeName, final OperationContext context, final T component, final PathAddress address) {
    final boolean hasPool = componentType.hasPool();
    final ModelNode result = context.getResult();
    final EJBComponentDescription componentDescription = component.getComponentDescription();
    if (COMPONENT_CLASS_NAME.getName().equals(attributeName)) {
        result.set(component.getComponentClass().getName());
    } else if (JNDI_NAMES.getName().equals(attributeName)) {
        for (ViewDescription view : componentDescription.getViews()) {
            for (String binding : view.getBindingNames()) {
                result.add(binding);
            }
        }
    } else if (BUSINESS_LOCAL.getName().equals(attributeName)) {
        for (final ViewDescription view : componentDescription.getViews()) {
            final EJBViewDescription ejbViewDescription = (EJBViewDescription) view;
            if (!ejbViewDescription.isEjb2xView() && ejbViewDescription.getMethodIntf() == MethodIntf.LOCAL) {
                result.add(ejbViewDescription.getViewClassName());
            }
        }
    } else if (BUSINESS_REMOTE.getName().equals(attributeName)) {
        for (final ViewDescription view : componentDescription.getViews()) {
            final EJBViewDescription ejbViewDescription = (EJBViewDescription) view;
            if (!ejbViewDescription.isEjb2xView() && ejbViewDescription.getMethodIntf() == MethodIntf.REMOTE) {
                result.add(ejbViewDescription.getViewClassName());
            }
        }
    } else if (TIMEOUT_METHOD.getName().equals(attributeName)) {
        final Method timeoutMethod = component.getTimeoutMethod();
        if (timeoutMethod != null) {
            result.set(timeoutMethod.toString());
        }
    } else if (ASYNC_METHODS.getName().equals(attributeName)) {
        final SessionBeanComponentDescription sessionBeanComponentDescription = (SessionBeanComponentDescription) componentDescription;
        final Set<MethodIdentifier> asynchronousMethods = sessionBeanComponentDescription.getAsynchronousMethods();
        for (MethodIdentifier m : asynchronousMethods) {
            result.add(m.getReturnType() + ' ' + m.getName() + '(' + String.join(", ", m.getParameterTypes()) + ')');
        }
    } else if (TRANSACTION_TYPE.getName().equals(attributeName)) {
        result.set(component.isBeanManagedTransaction() ? TransactionManagementType.BEAN.name() : TransactionManagementType.CONTAINER.name());
    } else if (SECURITY_DOMAIN.getName().equals(attributeName)) {
        EJBSecurityMetaData md = component.getSecurityMetaData();
        if (md != null && md.getSecurityDomainName() != null) {
            result.set(md.getSecurityDomainName());
        }
    } else if (RUN_AS_ROLE.getName().equals(attributeName)) {
        EJBSecurityMetaData md = component.getSecurityMetaData();
        if (md != null && md.getRunAs() != null) {
            result.set(md.getRunAs());
        }
    } else if (DECLARED_ROLES.getName().equals(attributeName)) {
        EJBSecurityMetaData md = component.getSecurityMetaData();
        if (md != null) {
            result.setEmptyList();
            Set<String> roles = md.getDeclaredRoles();
            if (roles != null) {
                for (String role : roles) {
                    result.add(role);
                }
            }
        }
    } else if (componentType.hasTimer() && TimerAttributeDefinition.INSTANCE.getName().equals(attributeName)) {
        TimerAttributeDefinition.addTimers(component, result);
    } else if (hasPool && POOL_AVAILABLE_COUNT.getName().equals(attributeName)) {
        final Pool<?> pool = componentType.getPool(component);
        if (pool != null) {
            result.set(pool.getAvailableCount());
        }
    } else if (hasPool && POOL_CREATE_COUNT.getName().equals(attributeName)) {
        final Pool<?> pool = componentType.getPool(component);
        if (pool != null) {
            result.set(pool.getCreateCount());
        }
    } else if (hasPool && POOL_NAME.getName().equals(attributeName)) {
        final String poolName = componentType.pooledComponent(component).getPoolName();
        if (poolName != null) {
            result.set(poolName);
        }
    } else if (hasPool && POOL_REMOVE_COUNT.getName().equals(attributeName)) {
        final Pool<?> pool = componentType.getPool(component);
        if (pool != null) {
            result.set(pool.getRemoveCount());
        }
    } else if (hasPool && POOL_CURRENT_SIZE.getName().equals(attributeName)) {
        final Pool<?> pool = componentType.getPool(component);
        if (pool != null) {
            result.set(pool.getCurrentSize());
        }
    } else if (hasPool && POOL_MAX_SIZE.getName().equals(attributeName)) {
        final Pool<?> pool = componentType.getPool(component);
        if (pool != null) {
            result.set(pool.getMaxSize());
        }
    } else {
        // Bug; we were registered for an attribute but there is no code for handling it
        throw EjbLogger.ROOT_LOGGER.unknownAttribute(attributeName);
    }
}
Also used : EJBViewDescription(org.jboss.as.ejb3.component.EJBViewDescription) Set(java.util.Set) EJBViewDescription(org.jboss.as.ejb3.component.EJBViewDescription) ViewDescription(org.jboss.as.ee.component.ViewDescription) Method(java.lang.reflect.Method) MethodIdentifier(org.jboss.invocation.proxy.MethodIdentifier) EJBSecurityMetaData(org.jboss.as.ejb3.security.EJBSecurityMetaData) EJBComponentDescription(org.jboss.as.ejb3.component.EJBComponentDescription) Pool(org.jboss.as.ejb3.pool.Pool) ModelNode(org.jboss.dmr.ModelNode) SessionBeanComponentDescription(org.jboss.as.ejb3.component.session.SessionBeanComponentDescription)

Example 3 with TimeoutMethod

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

the class TimerMethodMergingProcessor method handleAnnotations.

@Override
protected void handleAnnotations(final DeploymentUnit deploymentUnit, final EEApplicationClasses applicationClasses, final DeploymentReflectionIndex deploymentReflectionIndex, final Class<?> componentClass, final EJBComponentDescription description) throws DeploymentUnitProcessingException {
    final RuntimeAnnotationInformation<AutoTimer> scheduleAnnotationData = MethodAnnotationAggregator.runtimeAnnotationInformation(componentClass, applicationClasses, deploymentReflectionIndex, Schedule.class);
    final Set<Method> timerAnnotationData = MethodAnnotationAggregator.runtimeAnnotationPresent(componentClass, applicationClasses, deploymentReflectionIndex, Timeout.class);
    final Method timeoutMethod;
    if (timerAnnotationData.size() > 1) {
        throw EjbLogger.ROOT_LOGGER.componentClassHasMultipleTimeoutAnnotations(componentClass);
    } else if (timerAnnotationData.size() == 1) {
        timeoutMethod = timerAnnotationData.iterator().next();
    } else {
        timeoutMethod = null;
    }
    description.setTimeoutMethod(timeoutMethod);
    // now for the schedule methods
    for (Map.Entry<Method, List<AutoTimer>> entry : scheduleAnnotationData.getMethodAnnotations().entrySet()) {
        for (AutoTimer timer : entry.getValue()) {
            description.addScheduleMethod(entry.getKey(), timer);
        }
    }
}
Also used : AutoTimer(org.jboss.as.ejb3.timerservice.AutoTimer) List(java.util.List) Method(java.lang.reflect.Method) Map(java.util.Map)

Example 4 with TimeoutMethod

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

the class DatabaseTimerPersistence method timerFromResult.

private Holder timerFromResult(final ResultSet resultSet, final TimerServiceImpl timerService) throws SQLException {
    boolean calendarTimer = resultSet.getBoolean(24);
    final String nodeName = resultSet.getString(25);
    boolean requiresReset = false;
    TimerImpl.Builder builder = null;
    if (calendarTimer) {
        CalendarTimer.Builder cb = CalendarTimer.builder();
        builder = cb;
        //set calendar timer specifics first
        cb.setScheduleExprSecond(resultSet.getString(10));
        cb.setScheduleExprMinute(resultSet.getString(11));
        cb.setScheduleExprHour(resultSet.getString(12));
        cb.setScheduleExprDayOfWeek(resultSet.getString(13));
        cb.setScheduleExprDayOfMonth(resultSet.getString(14));
        cb.setScheduleExprMonth(resultSet.getString(15));
        cb.setScheduleExprYear(resultSet.getString(16));
        cb.setScheduleExprStartDate(resultSet.getTimestamp(17));
        cb.setScheduleExprEndDate(resultSet.getTimestamp(18));
        cb.setScheduleExprTimezone(resultSet.getString(19));
        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() ? new String[0] : paramString.split(";");
            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), resultSet.getString(1), new NoSuchMethodException());
                return null;
            }
            cb.setTimeoutMethod(timeoutMethod);
        }
    } else {
        builder = TimerImpl.builder();
    }
    builder.setId(resultSet.getString(1));
    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.valueOf(resultSet.getString(9)));
    builder.setPersistent(true);
    TimerImpl ret = builder.build(timerService);
    if (nodeName != null && (nodeName.equals(this.nodeName))) {
        if (ret.getState() == TimerState.IN_TIMEOUT || ret.getState() == TimerState.RETRY_TIMEOUT) {
            requiresReset = true;
        }
    }
    return new Holder(ret, requiresReset);
}
Also used : 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)

Example 5 with TimeoutMethod

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

the class CalendarTimer method getTimeoutMethod.

/**
 * Returns the {@link java.lang.reflect.Method}, represented by the {@link org.jboss.as.ejb3.timerservice.persistence.TimeoutMethod}
 * <p>
 * Note: This method uses the {@link Thread#getContextClassLoader()} to load the
 * relevant classes while getting the {@link java.lang.reflect.Method}
 * </p>
 *
 * @param timeoutMethodInfo  The timeout method
 * @param classLoader The class loader
 * @return timeout method matching {@code timeoutMethodInfo}
 */
public static Method getTimeoutMethod(TimeoutMethod timeoutMethodInfo, ClassLoader classLoader) {
    if (timeoutMethodInfo == null) {
        return null;
    }
    Class<?> timeoutMethodDeclaringClass;
    try {
        timeoutMethodDeclaringClass = Class.forName(timeoutMethodInfo.getDeclaringClass(), false, classLoader);
    } catch (ClassNotFoundException cnfe) {
        throw EjbLogger.EJB3_TIMER_LOGGER.failToLoadDeclaringClassOfTimeOut(timeoutMethodInfo.getDeclaringClass());
    }
    // now start looking for the method
    String timeoutMethodName = timeoutMethodInfo.getMethodName();
    Class<?> klass = timeoutMethodDeclaringClass;
    while (klass != null) {
        Method[] methods = klass.getDeclaredMethods();
        for (Method method : methods) {
            if (method.getName().equals(timeoutMethodName)) {
                if (timeoutMethodInfo.hasTimerParameter()) {
                    if (method.getParameterCount() == 1 && method.getParameterTypes()[0] == Timer.class) {
                        return method;
                    }
                } else if (method.getParameterCount() == 0) {
                    return method;
                }
            }
        // end: method name matching
        }
        // end: all methods in current klass
        klass = klass.getSuperclass();
    }
    // no match found
    return null;
}
Also used : Timer(javax.ejb.Timer) TimeoutMethod(org.jboss.as.ejb3.timerservice.persistence.TimeoutMethod) Method(java.lang.reflect.Method)

Aggregations

Method (java.lang.reflect.Method)7 TimeoutMethod (org.jboss.as.ejb3.timerservice.persistence.TimeoutMethod)5 ScheduleExpression (javax.ejb.ScheduleExpression)3 CalendarTimer (org.jboss.as.ejb3.timerservice.CalendarTimer)3 TimerImpl (org.jboss.as.ejb3.timerservice.TimerImpl)2 IOException (java.io.IOException)1 Serializable (java.io.Serializable)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 SQLException (java.sql.SQLException)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Timer (javax.ejb.Timer)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 ViewDescription (org.jboss.as.ee.component.ViewDescription)1 EJBComponentDescription (org.jboss.as.ejb3.component.EJBComponentDescription)1 EJBViewDescription (org.jboss.as.ejb3.component.EJBViewDescription)1