我是Playframework的新手。我正在尝试创建一个调度程序。我在创建它时遇到了问题。在这里,我分享了我的一部分代码。请帮我解决这个问题。我正在得到nullpointerexception。
Service.Java
public class AdminServices extends Service {
public ActorSystem actorSystem;
public ExecutionContext executionContext;
public class adminTaskScheduler {
}
public static AdminServices instance;
// private ActorSystem actorSystem;
// private ExecutionContext executionContext;
AdminServices() {
}
public static AdminServices getInstance() {
if (instance == null) {
instance = new AdminServices();
}
return instance;
}
// @Inject
// public AdminServices(ActorSystem actorSystem, ExecutionContext
// executionContext) {
// this.actorSystem = actorSystem;
// this.executionContext = executionContext;
//
// this.initialize();
// }
//
// private void initialize() {
// this.actorSystem.scheduler().schedule(Duration.create(10,
// TimeUnit.SECONDS),
// Duration.create(1, TimeUnit.DAYS), // delay
// () -> this.adminTaskScheduler(null, null), this.executionContext);
//
// }
public ServiceResult adminTaskScheduler( Integer userId) {
System.out.println("This is imp");
List<CourseCompletedDetailsDTO> pendingCourseDetailsList = new ArrayList<>();
//List<AppUserModel> appUserModelList = new ArrayList<>();
CourseCompletedDetailsDTO courseCompletedDetailsDTO = new CourseCompletedDetailsDTO();
// List<CourseCompletedDetailsDTO> courseCompletedDetailsList = new
// ArrayList<>();
// for (AppUserModel appUserModel : appUserModelList)
// userId = appUserModel.getId();
try {
AppUserModel employeeModel = AppUserServicesDAO.getInstance().getUserForUserId(userId);
System.out.println("user Id" + employeeModel);
if (employeeModel != null) {
// EmployeeProfileReturnValue profile = new
// EmployeeProfileReturnValue();
CourseModel courseModel = new CourseModel();
EmployeeProfileReturnValue.EmployeeProfileDTO pDTO = new EmployeeProfileReturnValue.EmployeeProfileDTO();
pDTO.setPendingCourses(pendingCourseDetailsList);
pDTO.setName(employeeModel.getUserName());
courseCompletedDetailsDTO.setCourseCode(courseModel.getCourseCode());
courseCompletedDetailsDTO.setCourseId(courseModel.getId());
courseCompletedDetailsDTO.setCourseName(courseModel.getCourseName());
pDTO.setDesignation(employeeModel.getDesignation());
}
} catch (Exception e) {
e.printStackTrace();
}
//return appUserModelList;
return createServiceResult(SERVICE_SUCCESS);
//return null;
}
@Inject
public AdminServices(ActorSystem actorSystem, ExecutionContext executionContext, Integer Id) {
// String sessionToken ;
// Integer userId;
this.actorSystem = actorSystem;
this.executionContext = executionContext;
// instance = new AdminServices();
this.initialize(Id);
}
public ServiceResult initialize(Integer Id) {
System.out.println("Service initialize method called");
try {
this.actorSystem.scheduler().schedule(Duration.create(5, TimeUnit.SECONDS),
Duration.create(1, TimeUnit.MINUTES), // delay
() -> adminTaskScheduler(3), this.executionContext);
System.out.println("This is scheduler class");
} catch (Exception e) {
e.printStackTrace();
return createServiceResult(FAILURE_STATUS);
}
return createServiceResult(SERVICE_SUCCESS);
}
}
我的DAO.Java
public AppUserModel getUserForUserId(Integer inUserId) {
return Ebean.find(AppUserModel.class).where().eq("id", inUserId).and().eq("isDeleted", 0).findUnique();
}
和MyCommand.java,我试图从其中发送ID并启动调度程序。
public int execute() {
try {
promiseResult = CompletableFuture.supplyAsync(() -> {
System.out.println("This is Command class");
ServiceResult serviceResult = AdminServices.getInstance().initialize(3);
//System.out.println("User Id" + appUserModel.getId());
ResponseData responseData = prepareJsonReponse(serviceResult);
result = responseData.getResult();
statusCode = responseData.getStatus();
return result;
});
/*
* promiseResult.handle((result, error) -> { if (error != null) {
* return prepareJsonReponse(SERVER_ERROR_STCODE); } else { return
* CompletableFuture.completedFuture(result); } });
*/
/*
* .thenApply(result -> { return result; }).;
*/
if (promiseResult != null)
return STATUS_OK;
} catch (Exception e) {
statusCode = SERVER_ERROR_STCODE;
e.printStackTrace();
return STATUS_INTERNAL_SERVER_ERROR;
}
return STATUS_BAD_REQUEST;
}
上面是我用来创建基于调度程序的员工ID的代码。