1

我正在尝试StringJob子类调用由 a 指定的任意控制器操作。

我已经尝试了这个问题的公认答案,但发现它对我不起作用......WS.url("http://www.yahoo.com/").get();有效,但WS.url("http://localhost/foo/bar").get()会在 60 秒后阻塞并最终超时。

我也尝试按照此答案中的建议增加 中的play.pool值,但没有任何区别。application.conf

这是我的代码:

应用程序.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

乔布斯.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,857注意和之间的时间间隔13:23:51,886——这是 60 秒的超时。

知道有什么问题吗?

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

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

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

4

1 回答 1

2

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

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

于 2013-09-04T03:18:06.803 回答