'Encountered invalid @Scheduled method 'methodName': Only no-arg methods may be annotated with @Scheduled
plz I don't know wath is the exact problem in the code if I add Scheduled annotatiçon in my code suddenly this error appears if you have any soulustion please. plz I don't know wath is the exact problem in the code if I add Scheduled annotatiçon in my code suddenly this error appears if you have any soulustion please.
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
12-09-2019 18:11:54.908 [restartedMain] ERROR o.s.boot.SpringApplication.reportFailure - Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'missionResource': Unsatisfied dependency expressed through field 'missionManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'missionImpl' defined in file [C:\Users\El Oussa\Desktop\vgas-api\vgas-manager\target\classes\ma\valueit\vgas\manager\business\impl\MissionImpl.class]: Initialization of bean failed; nested exception is java.lang.IllegalStateException: Encountered invalid @Scheduled method 'editMission': Only no-arg methods may be annotated with @Scheduled
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)
I have added @EnableScheduling in SpringBootApplication
--------------------------------------------------------
@SpringBootApplication
@EnableScheduling
@ComponentScan("com.qaiboub.vs")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@Service
public class MissionImpl extends CrudManagerImpl<MissionDto, Integer, MissionEntity, MissionService, MissionConverter> implements MissionManager {
@Autowired
private MissionService missionService;
@Autowired
private MissionConverter missionConverter;
enter code here
@Override
public MissionService getService() {
return missionService;
}
@Override
public MissionConverter getConverter() {
return missionConverter;
}
@Scheduled(cron = "0 15 18 * * *")
public void editMission(Integer id, MissionDto missionDto) {
if (StringUtils.isEmpty(id)) {
throw new MissingIdException();
}
if (missionDto == null) {
throw new InvalidPayloadException();
}
if (!id.equals(missionDto.getId())) {
throw new BusinessException(CommonErrorCode.TRYING_TO_EDIT_ANOTHER_ENTITY);
}
missionEntity = missionConverter.convertFrom(missionDto);
missionEntity = missionService.save(missionEntity);
}
}
Solution 1:[1]
remove the function arg. Scheduler don't take arg in their functions
Solution 2:[2]
You need to remove all the arguments from the function, the scheduler does not accept any argument
public void syncData(@RequestParam(name = "created_at_from") String createdAtFrom,
@RequestParam(name = "created_at_to") String createdAtTo) {}
Correct format is
public void syncData() throws Exception {}
Use this link for additional details: https://www.baeldung.com/shedlock-spring
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | user15185713 |
Solution 2 | CodingBee |