简体   繁体   中英

Injecting a class instance in java play scheduler task

I am trying to create a scheduler in Java play framework 2.5.10 where i want to execute a task for every 15 mins. I have gone through the schedule tasks using which I am able to invoke the task.

But i found two problems

I got a null pointer exception where a class am injecting is coming null as from the stack trace

I have used the below approach as shown in code

in myThread am trying to inject some classes that are coming null.

Also it triggered only once is this due to the null pointer exception? I was expecting the task schedules for every 15 minutes even though i see null pointer.

 this.actorSystem
     .scheduler()
     .schedule( FiniteDuration.create(0,TimeUnit.SECONDS),
                FiniteDuration.create(15,TimeUnit.MINUTES),
                new myThread(),
                executionContext);

Ok I got around this problem by initializing the schedule task with play.api.Application and initialize the required class instances using.

myObject = Play.application(app).injector().instanceOf(MyObjectClass.class);

I see that the scheduler now is getting triggered every 15 minutes to get the job done. I might need put some null checks to avoid these exceptions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM