I have a project in which there is a use case where a user books a meeting lets suppose for 10am to 11 am with another user. Now on the backend i want to check at 11 am if the meeting went well or not.
For this i am using Scheduled Future and Runnable. Whenever i book a meeting i create a scheduled future with the relevant meeting end time cron expression..which works fine but the problem is that i am not able to access repository or any of the services inside my run function which is necessary. i am getting a Null Pointer Exception
Here is one of the meetingService Code which triggers using endpoint
public String createMeetingSchedulerJobs(){
BookingDetails booking = repBooking.findAll().get(0);
//create completion check job
String[] dateParts = booking.getDateOfMeeting().split("/");
TaskDefinition taskDefinition = new TaskDefinition();
taskDefinition.setData(booking.getMeetingId());
taskDefinition.setActionType(TaskDefinitonType.MEETING_COMPLETION);
taskDefinition.setCronExpression(miscUtils.generateCronExpression(parseInt(dateParts[0]),parseInt(dateParts[1]),booking.getEndTimeHour(),booking.getEndTimeMinute(),0));
taskDefinitionBean.setTaskDefinition(taskDefinition);
taskDefinitionBean.setMiscUtils(miscUtils);
return taskSchedulerService.scheduleATask(taskDefinition);
}
the relevant schedulerService code
public String scheduleATask(TaskDefinition taskDefinition){
String jobId = UUID.randomUUID().toString();
taskDefinition.setId(jobId);
TaskDefinitionBean taskDefinitionBean = new TaskDefinitionBean();
taskDefinitionBean.setTaskDefinition(taskDefinition);
Runnable tasklet = taskDefinitionBean;
ScheduledFuture<?> scheduledTask = taskScheduler.schedule(tasklet, new CronTrigger(taskDefinition.getCronExpression(), TimeZone.getTimeZone(TimeZone.getDefault().getID())));
jobsMap.put(jobId, scheduledTask);
return jobsMap.toString();
}
Example Service
@Service
public class MiscellaneousUtils {
public String returnHello(){
return "Hello there ~";
}
}
TaskDefinitionBean.class
@Configurable
@Service
public class TaskDefinitionBean implements Runnable{
private TaskDefinition taskDefinition;
private MiscellaneousUtils miscUtils;
private BeanFactory beanFactory;
@Override
public void run() {
miscUtils = beanFactory.getBean(MiscellaneousUtils.class);
}
public TaskDefinition getTaskDefinition(){
return taskDefinition;
}
public void setTaskDefinition(TaskDefinition taskDefinition){
this.taskDefinition=taskDefinition;
}
public void setMiscUtils(MiscellaneousUtils miscUtils){this.miscUtils=miscUtils;}
}
I have tried different types of dependency injection in TaskDefinitionBean.class but not successful. I know there are many relevant and insightful questions which were similar but still couldn't use the approach.
The Error which i am getting is :
java.lang.NullPointerException: null
at fG.Service.TaskDefinitionBean.run(TaskDefinitionBean.java:38) ~[classes/:na]
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) ~[spring-context-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:93) [spring-context-5.2.5.RELEASE.jar:5.2.5.RELEASE]
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:514) [na:na]
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) [na:na]
at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:299) [na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) [na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) [na:na]
at java.base/java.lang.Thread.run(Thread.java:844) [na:na]
CodePudding user response:
for dependency injection to work Your bean has to be created by spring, You create it manually
TaskDefinitionBean taskDefinitionBean = new TaskDefinitionBean();
so beanFactory
is null
You can Autowire beanFactory
in Service and pass it to TaskDefinitionBean