繁体   English   中英

在Play!Framework中批量处理HTTP请求

[英]Batch HTTP requests in Play!Framework

我已经实现了当前的一组路由(例如):

GET     /api/:version/:entity               my.controllers.~~~~~
GET     /api/:version/:entity/:id           my.controllers.~~~~~
POST    /api/:version/:entity               my.controllers.~~~~~
POST    /api/:version/:entity/:id           my.controllers.~~~~~
DELETE  /api/:version/:entity               my.controllers.~~~~~

POST    /api/:version/search/:entity        my.controllers.~~~~~

他们工作得很漂亮。 现在,假设我要为同一API实现“批处理终结点”。 它看起来应该像这样:

POST    /api/:version/batch                 my.controllers.~~~~~

身体应该像这样:

[
    {
        "method": "POST",
        "call": "/api/1/customer",
        "body": {
            "name": "antonio",
            "email": "tonysmallhands@gmail.com"
        }
    },
    {
        "method": "POST",
        "call": "/api/1/customer/2",
        "body": {
            "name": "mario"
        }
    },
    {
        "method": "GET",
        "call": "/api/1/company"
    },
    {
        "method": "DELETE",
        "call": "/api/1/company/22"
    }
]

为此,我想知道如何调用播放框架路由器来传递这些请求? 我打算使用与单元测试建议类似的方法:

@Test
public void badRoute() {
  Result result = play.test.Helpers.routeAndCall(fakeRequest(GET, "/xx/Kiki"));
  assertThat(result).isNull();
} 

通过routeAndCall()的源代码,您会发现如下所示:

 /**
 * Use the Router to determine the Action to call for this request and executes it.
 * @deprecated
 * @see #route instead
 */
@SuppressWarnings(value = "unchecked")
public static Result routeAndCall(FakeRequest fakeRequest) {
    try {
        return routeAndCall((Class<? extends play.core.Router.Routes>)FakeRequest.class.getClassLoader().loadClass("Routes"), fakeRequest);
    } catch(RuntimeException e) {
        throw e;
    } catch(Throwable t) {
        throw new RuntimeException(t);
    }
}

/**
 * Use the Router to determine the Action to call for this request and executes it.
 * @deprecated
 * @see #route instead
 */
public static Result routeAndCall(Class<? extends play.core.Router.Routes> router, FakeRequest fakeRequest) {
    try {
        play.core.Router.Routes routes = (play.core.Router.Routes)router.getClassLoader().loadClass(router.getName() + "$").getDeclaredField("MODULE$").get(null);
        if(routes.routes().isDefinedAt(fakeRequest.getWrappedRequest())) {
            return invokeHandler(routes.routes().apply(fakeRequest.getWrappedRequest()), fakeRequest);
        } else {
            return null;
        }
    } catch(RuntimeException e) {
        throw e;
    } catch(Throwable t) {
        throw new RuntimeException(t);
    }
}

所以我的问题是:与复制上面的代码相比,使用Play做到这一点的方式是否更简单(我不反对将Scala和Java混合使用)? 我还希望提供并行或顺序执行批处理调用的选项……我猜想使用类加载器仅实例化一个Routes会成问题吗?

您可以使用以下方法调用来路由假请求: Play.current.global.onRouteRequest 请参阅此帖子以获取完整示例:http: //yefremov.net/blog/play-batch-api/

您可能可以为此使用WS API ,但就我个人而言,我只是创建一个私有方法来收集数据并从“单个”动作和“批处理”动作中使用它们,这肯定会更快。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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