繁体   English   中英

Spring @Async注释不会创建新线程

[英]Spring @Async annotation doesn't create a new thread

我已经阅读了有关@Async问题的所有文章,但是即使我以正确的方式正确找到了代码,它似乎也无法正常工作。

我将其作为春季实施的指南: https : //spring.io/guides/gs/async-method/

这是我的RestController类,带有@Autowired类声明

@RestController
public class RTCEntitiesRESTController {

    @Autowired
    private RTCEntitiesProcessor rtcEntitiesProcessor;

    @RequestMapping(value = "/refresh", method = RequestMethod.POST)
    public void refreshEntities() {
        rtcEntitiesProcessor.getStatus().start();
    }

    @RequestMapping(value = "/status", method = RequestMethod.GET)
    public @ResponseBody
    AbstractState getStatus() {
        return rtcEntitiesProcessor.getStatus();
    }

}

public class Ready extends AbstractState {

    private static final Logger LOGGER = LogManager.getLogger(Ready.class);

    @Override
    @Scheduled(fixedDelay = 600000)
    public void start() {
        context.setStatus(new InProgress());
        LOGGER.info("Starting process");
        LOGGER.info(Thread.currentThread().getName());
        context.processData();
    }

    @Override
    public String getStatus() {
        return "ready";
    }

}

这是@Component

@Component
public class RTCEntitiesProcessor {

    private static final Logger LOGGER = LogManager.getLogger(RTCEntitiesProcessor.class);

    private RTCEntitiesController entitiesController;
    private List<RTCRoleDefinition> roleDefinitions;
    private List<RTCUser> users;
    private AbstractState status;
    @Value("${rtcServer.timeBetweenExecutionsInMinutes}")
    private long timeBetweenExecutions;

    public RTCEntitiesProcessor(RTCEntitiesController entitiesController) {
        this.entitiesController = entitiesController;
        this.roleDefinitions = new ArrayList<RTCRoleDefinition>();
        this.users = new ArrayList<RTCUser>();
        status = new Ready();
    }

    @PostConstruct
    public void passContext() {
        status.setContext(this);
    }

    @Async
    public void processData() {
        try {
            getNewEntities();
            LOGGER.info(Thread.currentThread().getName());
            Thread.sleep(10000);
            status.block();
        } catch (Exception e) {
            LOGGER.info("Error while processing entities", e);
            status.fail();
        }
    }

    private void getNewEntities() throws InterruptedException {
        List<RTCRoleDefinition> newRoleDefinitions = new ArrayList<RTCRoleDefinition>();
        List<RTCUser> newUsers = new ArrayList<RTCUser>();
        for (RTCProjectArea projectArea : entitiesController.getProjectAreas()) {
            projectArea.addAreas(entitiesController.getTeamAreas(projectArea));
            processArea(projectArea, null);
            newRoleDefinitions.addAll(projectArea.getRoles());
            newUsers = mergeUsers(newUsers, projectArea.getMembers());
            for (RTCTeamArea rtcTeamArea : projectArea.getAreas()) {
                newRoleDefinitions.addAll(rtcTeamArea.getRoles());
                newUsers = mergeUsers(newUsers, rtcTeamArea.getMembers());
            }
        }
        this.roleDefinitions = newRoleDefinitions;
        this.users = newUsers;
    }

这是@SpringBootAplication

@SpringBootApplication
@EnableAsync
@EnableScheduling
public class MainController {

    private static final Logger LOGGER = LogManager.getLogger(MainController.class);

    @Value("${rtcServer.username}")
    private String username;
    @Value("${rtcServer.password}")
    private String password;
    @Value("${rtcServer.uri}")
    private String serverURI;

    public static void main(String[] args) {
        LOGGER.info("Running Spring Boot Application");
        SpringApplication.run(MainController.class, args);
    }

    @Bean
    public RTCHttpClient getClient() {
        return new RTCHttpClient(username, password, serverURI);
    }

}

要特别注意以下事实: @Async注释如果被this调用则不起作用。 因此,尝试在Ready类中自动连接RTCEntitiesProcessor 您应该为此Ready@Component

要了解您可以在https://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/aop.html#aop-understanding-aop-proxies中阅读更多内容

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM