从作业中调用一个控制器的操作方法(Call a Controller action method f

2019-10-18 13:33发布

我想调用任意控制操作,由指定的String ,从Job的子类。

我曾尝试接受答案这个问题 ,但发现,它并没有为我工作... WS.url("http://www.yahoo.com/").get(); 作品,但WS.url("http://localhost/foo/bar").get()块,并最终超时60秒后。

我也曾尝试增加play.pool价值application.conf ,在推荐这个答案 ,但它并没有区别。

这里是我的代码:

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秒的超时。

任何想法有什么不对?

现在,我只是试图让这个简单的情况下工作。 后来,我需要与请求提供饼干等。

另外,我不使用的念头超级舒适WS对于这一点,因为我不是真的想另一台主机上访问web服务; 我试图调用一些代码,这是我自己的应用程序,因此它似乎有点傻不得不产生一个实际的HTTP请求回我自己。 是不是有什么办法可以在内部构建一个请求的内容,只是直接调用的操作方法?

正如上面提到的,这种预期的目的,是让任意控制器动作被调度在我的系统工作。 我想避免的每一个我们需要安排工作,做一些事情,一些控制器代码已经这样做时写的代码。

Answer 1:

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

我也有我的WSRequest对象的followRedirects属性设置为true,但似乎不起作用,并且导致挂起。 有一次,我打开那个时候,我看到了重定向发生的事情,然后我做了一些解决办法,以避免重定向,所以现在它的工作原理。



文章来源: Call a Controller action method from within a Job