简体   繁体   中英

Spring @Async annotation doesn't create a new thread

i had read all the post about problems with @Async , but even when i have the code exactly the way that is supposedly correct it appears to refuse to work.

I use as a guide for implement this the spring: https://spring.io/guides/gs/async-method/

This is my RestController class, with the @Autowired class declaration

@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";
    }

}

This is the @Component class

@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;
    }

This is the @SpringBootAplication Class

@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);
    }

}

Pay extra attention to the fact that @Async annotations do not work if they are invoked by this . So, try to autowire RTCEntitiesProcessor in your Ready class. You should make Ready as @Component for that

To understand yor can read more in https://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/aop.html#aop-understanding-aop-proxies

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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