I have a runnable class
public class MissingPartitionsTask implements Runnable {
@Autowired
private PartitionsService partitionsService;
private Schedules schedule;
MissingPartitionsTask(Schedules schedule){
this.schedule = schedule;
}
@Override
public void run() {
partitionsService.findAll(schedule.getId());
}
}
When I run it, I get
java.lang.NullPointerException
at MissingPartitionsTask.run(MissingPartitionsTask.java:26)
This is row 26
partitionsService.findAll(schedule.getId());
I checked schedule.getId()
. It's not empty. There are 3 rows in the table with id 1, 2 and 3.
I also have a service
@GetMapping("/partitions/{id}")
public List<LocalDate> findAll(@PathVariable long id) {
return partitionsService.findAll(id);
}
It works when I call it using my browser and returns a list of dates.
http://localhost:8181/partitions/3
Why does MissingPartitionsTask
not work?
EDIT My application class
@SpringBootApplication
@EnableScheduling
@EnableAsync
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
EDIT2
@Service
public class SchedulerService {
@Autowired
private ScheduleRepository scheduleRepository;
@Autowired
private TaskExecutor taskExecutor;
@Scheduled(fixedRate=60000)
public void scheduleAll() {
scheduleRepository.findAll().forEach(
schedule -> {
MissingPartitionsTask missingPartitionsTask = new MissingPartitionsTask(schedule);
taskExecutor.execute(missingPartitionsTask);
});
}
}
EDIT3
Why is my Spring @Autowired field null?
Does not help. My service is autowired
@Autowired
private PartitionsService partitionsService;
and annotated
@Service
public class PartitionsService {
schedule IS NOT NULL. It returns values.
@Override
public void run() {
System.out.println(schedule.getId());
}
result 1 2 3
The service works when I do a GET, it does not work when I try calling it from a thread.
EDIT4
The problem was that Spring didn't control my runnable. Moving my run method to SchedulerService as
private Runnable newRunnable(Schedules schedule) {
return () -> {
List<String> missing = partitionsService.findMissing(schedule.getId());
};
}
And calling it like this
taskExecutor.execute(newRunnable(schedule));
instead of
taskExecutor.execute(missingPartitionsTask);
Solved it.