结合 Akka、Spray 和嵌入式 Jetty
我正在尝试创建一个包含 Akka、Spray 和 Jetty 的独立 JAR。理想情况下,我将整个应用程序分发到单个文件中,而不包含任何外部文件。
我了解如何创建嵌入式 Jetty 服务器实例
def main(args: Array[String]): Unit = {
val server = new Server(9012);
server.start();
server.join();
Thread.sleep(2000);
server.stop();
}
,并且我按照 Spray 示例代码创建了 HelloService 和 Boot 类,但我不知道如何连接这两个类,因此当在 Jetty 上请求 URL 时服务器提供一个 Spray 服务来响应它。任何帮助将不胜感激。
更新:由于 Alois Cochard 提出的一个询问线索,我离解决这个问题越来越近了(我来自 Web 脚本背景,并且正在了解 Java Web 服务)一直......具有挑战性!)。我修改了 main 方法 来启动服务器并阅读 Jetty 和 akka 配置 文件入门模板中的内容。它正在读取这两个文件,但现在当我导航到 Jetty 服务器上的 / 时,我得到了这个:
HTTP 错误:500
访问 / 时出现问题。原因:
断言失败:找到 0 个 ID 为“spray-root-service”的参与者,预计只有一个
我知道我错过了一些愚蠢的东西(可能我应该分解并使用 SBT,但能够编译并运行Eclipse,然后在浏览器中刷新,是如此简单和吸引人)。
更新#2:解决了问题。我没有创建 WebAppContext 对象,这意味着 web.xml 永远不会被读取,因此 Akka 永远不会被加载。 这是修订后的主要方法,现在正在运行。
I'm trying to create a standalone JAR containing Akka, Spray, and Jetty. Ideally I distribute the entire application in that single file, without any external files whatsoever.
I understand how to create an embedded Jetty server instance
def main(args: Array[String]): Unit = {
val server = new Server(9012);
server.start();
server.join();
Thread.sleep(2000);
server.stop();
}
and I've followed the Spray example code in creating a HelloService and Boot class, but I have no earthly idea of how to connect the two, so that when a URL is requested on the Jetty server a Spray service responds to it. Any help would be much appreciated.
Update: I'm getting a lot closer to solving this problem, thanks to a thread of inquiry prompted by Alois Cochard (I'm coming from a web scripting background, and getting my head around Java web services has been ... challenging!). I've modified my main method to start the server and read the Jetty and akka configuration files that are in the getting started template. It's reading both of those files, but now I'm getting this when I navigate to / on the Jetty server:
HTTP ERROR: 500
Problem accessing /. Reason:
assertion failed: 0 actors for id 'spray-root-service' found, expected exactly one
I know I'm missing something silly (and probably that I should break down and use SBT, but being able to just compile and run in Eclipse, and then refresh in the browser, is so simple and appealing).
Update #2: Figured out the problem. I wasn't creating a WebAppContext object, which meant that the web.xml was never getting read, and thus Akka was never being loaded. This is the revised main method which is now working.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据spray-template,您应该在web.xml配置文件中添加Spray servlet连接器:
http://github.com/spray/spray-template/blob/master/src/main/webapp/WEB-INF/web.xml
可以找到一些信息关于如何配置独立jetty 在这里使用此文件(直接在 netty 文档中肯定有更好的参考):
http://exist.sourceforge.net/ deployment.html#d47e594
顺便说一句,使用喷雾模板作为项目的基础看起来是个好主意;)
According to the spray-template, you should add the Spray servlet connector in the web.xml configuration file:
http://github.com/spray/spray-template/blob/master/src/main/webapp/WEB-INF/web.xml
You can find some informations about how to configure a standealone jetty to use this file here (there is surely better references in netty documentation directly):
http://exist.sourceforge.net/deployment.html#d47e594
BTW, using the spray template as a basis for your project looks like a good idea ;)