当前位置: 首页 > 知识库问答 >
问题:

Spring未检测@计划方法

程钧
2023-03-14

我正在运行Spring 3.2.3。RELEASE和我在@Service修饰类中有几个方法,它们是计划任务,因此用@Scheduled注释修饰。

所有Springbean都在容器中被检测和实例化,但是从不调用@Scheduled注释。

我有几个应用程序上下文,但主文件描述如下:

  <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <import resource="classpath:core-properties.xml" />
    <import resource="classpath:core-datasource.xml" />
    <import resource="classpath:core-task-scheduler.xml" />

    <context:component-scan base-package="com.atlaschase.project.core">
        <context:exclude-filter type="regex"
            expression="com.atlaschase.project.core.services.jms.*" />
        <context:exclude-filter type="regex"
            expression="com.atlaschase.project.core.services.processor.*" />
        <context:exclude-filter type="regex"
            expression="com.atlaschase.project.core.services.remote.*" />
        <context:exclude-filter type="regex"
            expression="com.atlaschase.project.core.bootstrap.populators.*" />
    </context:component-scan>

    <bean id="bufferPopulator" class="com.atlaschase.project.core.services.jms.buffer.BufferPopulator"/>

    <bean id="eventBuffer" class="com.atlaschase.project.core.services.jms.buffer.EventBuffer"/>

    <bean id="processorEventHandler" class="com.atlaschase.project.core.services.jms.buffer.ProcessorEventHandler"/>

另一个重要文件作为“core task scheduler.xml”导入。其配置如下:

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <task:annotation-driven executor="myExecutor" scheduler="myScheduler"/>

    <task:executor id="myExecutor" pool-size="1"/>

    <task:scheduler id="myScheduler" pool-size="5"/>

</beans>

最初,executor引用(如上)是在主应用程序上下文中定义的,但我在阅读了其他类似问题已通过这种方式解决后,将其拆分为导入。不幸的是,这没有帮助。我还把进口申报单挪动了一下,但这似乎没有任何效果。

在Spring应用程序上下文中直接声明调度方法效果很好,但是我非常喜欢使用注释。

下面是带注释的类:

**
 * Service that batches the saving of position updates into a periodically executed
 * process. 
 */
@Service
public class PositionAggregatorService {

    static Logger logger = Logger.getLogger(AircraftPositionAggregatorService.class);

    // The service will not execute within 1 minute of a previous batch attempt.
    // This paramater is updated by a message from another node indicating that
    // they are commencing a batch save of position updates.
    private DateTime blockingTime; 

    private Map<Long, List<Position>> positions;

    @Autowired
    private PositionDao positionDao;

    @Autowired
    private InternalPublisher internalEventPublisher;

    @PostConstruct
    private void initialise(){
        positions = new ConcurrentHashMap<Long, List<Position>>();
    }

    /*
     * Scheduled to execute every 10 minutes as long current time is not
     * before the blocking time threshold.
     * 
     * */
    @Scheduled(fixedRate=6000)
    public void batchSavePositionUpdates(){

        if(blockingTime != null){
            if(DateTime.now(DateTimeZone.UTC).isBefore(blockingTime)){
                return;
            }
        }

        PositionPersistStartedNotification started = new PositionPersistStartedNotification(DateTime.now(DateTimeZone.UTC), DateTime.now(DateTimeZone.UTC).plusMinutes(2));
        internalEventPublisher.publishPositionPersistStartedNotification(started);

        DateTime mostRecentUpdateTime = null;

        List<Position> positionsToSave = new ArrayList<Position>();

        for(Long flightId : positions.keySet()){

            List<Position> positionList = positions.get(flightId);

            for(Position position : positionList){
                if(mostRecentUpdateTime == null){
                    mostRecentUpdateTime = new DateTime(position.getTime());
                }
                else{
                    DateTime positionTime = new DateTime(position.getTime());
                    if(positionTime.isAfter(mostRecentUpdateTime)){
                        mostRecentUpdateTime = positionTime;
                    }
                }
                positionsToSave.add(position);
            }
        }

        Boolean successfulSave = false;

        try{
            positionDao.save(positionsToSave);
            successfulSave = true;
            logger.info(positionsToSave.size() + " Position Updates saved successfully");
        }catch(Exception exception){
            logger.error("Failed to persist latest position updates to database");
            logger.error(exception);
        }


        if(successfulSave){
            removeSavedPositions(mostRecentUpdateTime);
            PositionPersistEndedNotification ended = new PositionPersistEndedNotification(DateTime.now(DateTimeZone.UTC), mostRecentUpdateTime);
            internalEventPublisher.publishPositionPersistEndedNotification(ended);
        }

    }
}

任何帮助都将不胜感激。

共有2个答案

公孙棋
2023-03-14

通过侦听要加载的上下文,然后在ContextReFreshedEvent上执行方法来启动应用程序。

当我从应用程序中删除这个ApplicationEventListener并简单地调用bean上的公共方法来启动服务(而不是依赖ApplicationEventListener)时,应用程序就会正常启动,所有@Scheduled注释都按预期工作。

桂学
2023-03-14

Spring上下文是否在运行时成功加载?我发现xsd(3.0和3.2)中同时存在不同版本的名称空间定义存在一些不一致之处。是否可以尝试在这些名称空间中使用一致的相同版本,然后重试?

 类似资料:
  • 如何在spring boot应用程序中测试计划的作业任务?

  • 我想在一段时间内发送一个post请求。我创造了这样的方法; 我应该在控制器中使用@Scheduled吗?有更好的方法吗? 谢谢

  • 主要内容:测试计划的类型,如何编写测试计划,测试计划指南测试计划是描述软件测试领域和活动的详细文档。它概述了测试策略,目标,测试计划,所需资源(人力资源,软件和硬件),测试评估和测试可交付成果。 测试计划是每个软件测试的基础。这是最重要的活动,可确保以适当的顺序提供所有计划活动清单。 测试计划是用于将软件测试活动作为定义的过程进行的模板,该过程由测试经理完全监视和控制。 测试计划的类型 测试计划有三种类型 主测试计划 阶段测试计划 测试特定类型的测试计

  • 我正在开发一个 Spring-MVC 应用程序,其中我使用调度来删除不必要的额外内容。不幸的是,我计划的方法没有触发。谁能告诉我我做错了什么。 这是代码: 我知道参数名称为1周,但我将在3天后删除它。我刚刚复制了代码…:D任何帮助都很好。谢谢

  • 主要内容:测试计划注意事项 -可以将测试计划可视化为用于运行测试的JMeter脚本。 测试计划由测试元素组成,例如线程组,逻辑控制器,样本生成控制器,监听器,定时器,断言和配置元素。 测试计划包含执行脚本的所有步骤。 测试计划中包含的所有内容都按照从上到下的顺序执行,或者按照测试计划中定义的顺序执行。 下图给出了测试计划的目录级别。 测试计划注意事项 - 在运行整个测试计划之前,应保存测试计划。 JMeter文件或测试计划以扩

  • 如果Spring配置只包含Eagy(非惰性)singleton beans,即默认值,那么Spring是否可能在任何地方都没有注入这些bean的情况下抛出异常?我本质上是在寻找一种以Spring bean的形式检测死代码的方法。 我的问题有点类似于这些。 http://forum.spring.io/forum/spring-projects/container/116494-any-tools-