我已经阅读了有关@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);
}
}
答案 0 :(得分:1)
要特别注意以下事实:@Async
注释如果被this
调用则不起作用。因此,尝试在RTCEntitiesProcessor
类中为Ready
自动布线。为此,您应将Ready
设为@Component