gpt4 book ai didi

java - 从作业中调用 Controller 操作方法

转载 作者:行者123 更新时间:2023-11-30 04:15:27 33 4
gpt4 key购买 nike

我试图从 Job 子类调用由 String 指定的任意 Controller 操作。

我已尝试接受的答案 this question ,但发现它对我不起作用... WS.url("http://www.yahoo.com/").get(); 有效,但是 WS .url("http://localhost/foo/bar").get() 阻塞并最终在 60 秒后超时。

我还尝试按照this answer中的建议增加application.conf中的play.pool值,但这没有什么区别。

这是我的代码:

application.conf:

# Execution pool
# ~~~~~
# Default to 1 thread in DEV mode or (nb processors + 1) threads in PROD mode.
# Try to keep a low as possible. 1 thread will serialize all requests (very useful for debugging purpose)
play.pool=3

路线:

PUT     /jobs/invokejob                         Jobs.invokeTestJob
GET /jobs/sampleAction Jobs.sampleControllerAction

Jobs.java:

public static void invokeTestJob()
{
Logger.warn("Jobs.invokeTestJob() called");
new SampleJob("Jobs.sampleControllerAction").in(1);
Logger.warn("Finished scheduling SampleJob");

listJobs();
}

public static void sampleControllerAction()
{
Logger.warn("Jobs.sampleControllerAction() called");
renderText("OK");
}

SampleJob.java:

public class SampleJob extends QJob
{
public final String action;

public SampleJob(String actionSpec)
{
this.action = actionSpec;
}

@Override
public void doJob()
{
Logger.warn("SampleJob.doJob() called");

final ActionDefinition actionDefinition = Router.reverse(action);
actionDefinition.absolute();
final WSRequest URL = WS.url(actionDefinition.url);
HttpResponse response = null;
switch(actionDefinition.method)
{
case "GET":
{
Logger.warn("GETting %s", URL.url);
response = URL.get();
break;
}
case "POST":
{
Logger.warn("POSTting %s", URL.url);
response = URL.post();
break;
}
case "PUT":
{
Logger.warn("PUTting %s", URL.url);
response = URL.put();
break;
}
case "DELETE":
{
Logger.warn("DELETEing %s", URL.url);
response = URL.delete();
break;
}
}
Logger.warn("response=%s", response.getString());
}
}

日志输出:

13:22:50,783 WARN  [play-thread-1] ~ Jobs.invokeTestJob() called
13:22:50,786 WARN [play-thread-1] ~ Finished scheduling SampleJob
13:22:51,804 WARN [jobs-thread-1] ~ SampleJob.doJob() called
13:22:51,857 WARN [jobs-thread-1] ~ GETting http://localhost/quattro/jobs/sampleAction
13:23:51,886 ERROR [jobs-thread-1] ~

@6fe61pf2p
Error during job execution (jobs.SampleJob)

Execution exception (In {module:quattro}/app/jobs/SampleJob.java around line 36)
RuntimeException occured : java.util.concurrent.ExecutionException: java.util.concurrent.TimeoutException: No response received after 60000

play.exceptions.JavaExecutionException: java.util.concurrent.ExecutionException: java.util.concurrent.TimeoutException: No response received after 60000
at play.jobs.Job.call(Job.java:155)
at play.jobs.Job$2.call(Job.java:94)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:178)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:292)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.RuntimeException: java.util.concurrent.ExecutionException: java.util.concurrent.TimeoutException: No response received after 60000
at play.libs.ws.WSAsync$WSAsyncRequest.get(WSAsync.java:223)
at jobs.SampleJob.doJob(SampleJob.java:36)
at play.jobs.Job.doJobWithResult(Job.java:50)
at play.jobs.Job.call(Job.java:146)
... 8 more
Caused by: java.util.concurrent.ExecutionException: java.util.concurrent.TimeoutException: No response received after 60000
at com.ning.http.client.providers.netty.NettyResponseFuture.get(NettyResponseFuture.java:223)
at com.ning.http.client.providers.netty.NettyResponseFuture.get(NettyResponseFuture.java:187)
at play.libs.ws.WSAsync$WSAsyncRequest.get(WSAsync.java:221)
... 11 more
Caused by: java.util.concurrent.TimeoutException: No response received after 60000
at com.ning.http.client.providers.netty.NettyResponseFuture.get(NettyResponseFuture.java:215)
... 13 more

请注意 13:22:51,85713:23:51,886 之间的时间间隔 - 即 60 秒超时。

知道出了什么问题吗?

现在,我只是想让这个简单的案例发挥作用。稍后,我需要在请求中提供 cookie 等。

此外,我对使用 WS 来实现此目的不太满意,因为我并没有真正尝试访问另一台主机上的 Web 服务;我正在尝试调用我自己的应用程序中的一些代码,因此必须生成返回给我自己的实际 HTTP 请求似乎有点愚蠢。有没有办法在内部构造请求的内容并直接调用操作方法?

如上所述,这样做的目的是允许将任意 Controller 操作安排为我的系统中的作业。我希望避免每次需要安排作业来执行某些 Controller 代码已经执行的操作时都必须编写代码。

最佳答案

我发现了问题。我的请求被重定向。实际上,它被重定向了两次:一次是因为我正在使用安全模块,该模块会重定向到登录页面,甚至在此之前,我的公司似乎安装了 localhost 请求的一些网络安全设备本地网络 IP 地址。

我确实将我的 WSRequest 对象的 followRedirects 属性设置为 true,但似乎不起作用,并导致它挂起。一旦我关闭它,我就看到了重定向的发生,然后我做了一些解决方法来避免重定向,所以现在它可以工作了。

关于java - 从作业中调用 Controller 操作方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18524973/

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