gpt4 book ai didi

java - Play Framework : Call Controller/URL from within a Job

转载 作者:行者123 更新时间:2023-12-02 00:33:08 26 4
gpt4 key购买 nike

我有一份按小时运行的工作。我想像这样调用 Controller 的操作方法:

public class MyJob extends Job {

@Override
public void doJob() throws Exception {
MyController.someActionMethod();
}
}

或直接调用 URL。

如果这可以在工作中实现,有什么想法吗?

如果我直接调用 Controller 的操作方法,我会得到:

NullPointerException occured : null

play.exceptions.JavaExecutionException
at play.jobs.Job.call(Job.java:155)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:98)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:207)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.NullPointerException
at play.classloading.enhancers.ControllersEnhancer$ControllerInstrumentation.isActionCallAllowed(ControllersEnhancer.java:187)
at controllers.MyController.someActionMethod(MyController.java)
at fun.job.MyJob.doJob(MyJob.java:10)
at play.jobs.Job.doJobWithResult(Job.java:50)
at play.jobs.Job.call(Job.java:146)
... 7 more

最佳答案

您可以使用 WS 帮助程序类。例如,

HttpResponse response = WS.url("http://www.yahoo.com/").get()
String html = response.getString(); // or...
Document xml = response.getXml();

您上面所做的操作会尝试将浏览器重定向到与 Controller 操作关联的 URL,这从异步作业的上下文中没有意义,其中除其他原因外,没有浏览器可以处理重定向。

关于java - Play Framework : Call Controller/URL from within a Job,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8469126/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com