- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有基于 spray.io 的网络服务,它作为独立的 jar 运行(我使用 sbt assembly
然后只是 java -jar myws.jar
).它与喷雾示例中的 Bootstrap 非常相似,如下所示:
/** Bootstrap */
object Boot extends App {
// we need an ActorSystem to host our application in
implicit val system = ActorSystem("my-system")
// create and start our service actor
val service = system.actorOf(Props[MyServiceActor], "my-ws-service")
implicit val timeout = Timeout(10.seconds)
CLIOptionsParser.parse(args, CLIOptionsConfig()) map { config =>
// start a new HTTP server
IO(Http) ? Http.Bind(service, interface = config.interface, port = config.port)
}
}
现在我只是使用 java -jar my-service "$@"&
在后台运行该进程并使用 kill -9 pid
停止。
我想优雅地停止我的网络服务,这意味着它完成打开的连接并拒绝新连接。
github 上的喷雾 jar 页面建议向它发送 Akka PoisonPill 消息
。理想情况下,我想从命令行启动它,尽可能简单。我想也许再附加一个仅绑定(bind)到本地主机的 HTTP 服务器实例,并使用一些其他方法来停止,并可能诊断 Web 服务。可行吗?还有哪些选择?
更新:我根据答案添加了我可以想象必须工作的内容,但似乎没有,至少我从来没有看到我希望在标准输出或日志中看到的任何消息。实际上,我已经尝试过 HttpUnbind、PoisonPill 的变体,以及一个。任何有强硬 akka 眼睛的人都可以看看这个吗?附言。钩子(Hook)本身被成功调用,检查它。我发送给 jvm 的信号是 SIGTERM。
/* Simple reaper actor */
class Reaper(refs: ActorRef*) extends Actor {
private val log = Logging(context.system, this)
val watched = ArrayBuffer(refs: _*)
refs foreach context.watch
final def receive = {
case Terminated(ref) =>
watched -= ref
log.info(s"Terminated($ref)")
println(s"Terminated($ref)")
if (watched.isEmpty) {
log.info("Shutting dow the system")
println("Shutting dow the system")
system.shutdown()
}
}
}
// termination hook to gracefully shutdown the service
Runtime.getRuntime.addShutdownHook(new Thread() {
override def run() = {
val reaper = system.actorOf(Props(new Reaper(IO(Http), service)))
//IO(Http) ? Http.Unbind(5.minutes)
IO(Http) ! PoisonPill
}
})
UPDATE2:因此,它以某种方式起作用,即 - 当发送 PoisonPill 时,所有当前 HTTP 连接都关闭了。但我宁愿停止接收新连接,并等待打开返回响应并关闭。
VERDICT:似乎 akka 有它自己的钩子(Hook),因为尽管我的钩子(Hook)被执行了,但在我没有采取任何行动的情况下,actor 被杀死并且所有连接都关闭了。如果有人会提供带有 JVM 关闭 Hook 的解决方案,那就太好了。我建议这是一个重要的问题,很遗憾它在网上没有任何好的方法。同时,我将尝试使用 tcp/http 实现正常关闭。
最佳答案
虽然我尝试使用 SIGTERM 和 JVM Hook ,但我需要阻止 Hook 线程退出,直到我的关闭序列完成,而我只是不知道该怎么做(我对 akka 有点缺乏经验,所以也许我错过了一些明显的解决方案)。
我最后所做的是仅将另一个 HTTP 监听器附加到 localhost,它具有启动关闭的方法(而且它碰巧对其他任务很方便,例如获取服务器状态、触发应用程序中的事件等)。
这可能看起来像这样(我怀疑这可能包含不必要的操作,因此欢迎改进):
在 Bootstrap 中:
implicit val system = ActorSystem(...)
// create and start external service actor
val service = system.actorOf(Props[MyWebServiceActor], "my-web-service")
// create internal service to manage application
val controlService = system.actorOf(Props[ControlServiceActor], "control-service")
implicit val timeout = Timeout(10.seconds)
// start a new HTTP server for external service (notifying control of HTTP listener)
IO(Http).tell(Http.Bind(service, interface = config.interface, port = config.port), controlService)
// start internal server looking at localhost
IO(Http) ? Http.Bind(controlService, interface = "127.0.0.1", port = config.controlPort)
和控制服务本身:
class ControlServiceActor extends Actor with HttpService with ActorLogging {
def actorRefFactory = context
implicit val system = context.system
/* Listener of main service */
var listener: ActorRef = _
def receive = {
// this is reply from IO.Http when HTTP listener is bound
case Http.Bound(_) =>
listener = sender()
context.become(mainContext)
}
// http api for graceful stop
val mainContext = runRoute {
path("stop") {
get {
parameter('timeout.as[Int] ? 60) { timeout =>
complete {
// unbind makes listener to reject new connections
context.become(shuttingDownContext)
log.warning(s"Stopping application within $timeout seconds...")
context.watch(listener)
listener ! Http.Unbind(timeout.seconds)
"Stopping..."
}
}
}
}
}
// Shutdown sequence
val shuttingDownContext = ({
// when unbound HTTP listener not accepting connections
case Http.Unbound =>
log.info("Webservice unbound, waiting for active connections to complete")
// when HTTP listener terminated after unbound it has been processed all requests
case Terminated(ref) if ref == listener =>
log.info("Webservice finished, exiting")
system.shutdown()
}: Actor.Receive) orElse runRoute(complete("Shutdown in progress"))
}
关于web-services - spray-can webservice 优雅关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24731242/
WebService 依赖: org.springframework.boot spring-boot-starter-web-services org.apache.cxf cxf-rt-fr
我需要在我的项目中使用 Web 服务。我使用 NetBeans,因此右键单击我的项目并尝试添加新的“Web 服务客户端”。上次我检查过,这是创建 Web 服务客户端的方法。但它导致了一个断言错误,说:
我在 Netbeans 中创建了两个 Java SOAP Web 服务。基本上每个服务都是一个不同的 Web 应用程序,有自己的 war 文件。我从 Java 客户端调用第一个 WS,并从第一个服务调
有没有一种有效的工具可以将 .Net C# webservice 转换为 java webservice。是否有任何开源工具可以提供帮助? 最佳答案 不要浪费时间寻找过渡工具。如果您使用的是 Java
情况是这样的。我从某人那里收到了由 Apache/Tomcat 服务器 (Java) 生成的 WSDL(包括 XSD)。我为其做一个项目的公司更喜欢 .NET,因此我使用 wsdl.exe 生成部分类
我正在使用 java 开发 axis2 网络服务,用于将记录插入数据库。我正在测试 web 服务客户端,它返回空响应代码,实际上我在 web 服务中返回整数值但我成功地将记录插入数据库,我可以在执行客
您好,我正在尝试使用 json 在钛中调用 webService。该 webService 不接受任何参数,所以我只需要调用它。 这是我的代码: var xhr = Titanium.Network.
我正在尝试将基本的Web服务模板部署到tomee,我尝试了Windows 7 64位和Windows 8 64位以及java版本1.8.0_25(64位),1.8.0_91(64位)(此java版本用
我正在尝试使用包含 web 服务参数的 get 方法调用 web 服务。但我无法在互联网上找到答案,请任何人帮助我。下面给出我的网络服务 http://api.crmseries.com/user/V
调用 Web 服务时出现以下抛出错误。除了人们问同样的问题外,用谷歌搜索没有任何结果。 Server was unable to process request. ---> The surrogate
我正在尝试使用 Yahoo 查询语言找到一种通过 Yahoo Weather 获取一些天气信息的方法。 因为我住在法国的一个叫尼斯的城市,下面的查询返回一个错误: select * from weat
我需要知道是否可以从后台调用 json webservices,当用户按下主页按钮时,我从后台执行调用此方法 - (void) runTimer { [NSThread detachNewTh
我有一个 Web 服务,它位于这样的反向代理后面: 现在发生的情况是,当我尝试添加 Web 引用来测试 Web 服务时,它说无法下载 wsdl 文件。那是因为当请求被发送时它是 https://uat
我需要创建一个Web服务,该服务用于通过输入一个字符串ID从服务器下载音频(wav)文件。如果服务器上不存在音频文件,则需要以json格式发送错误回传。 现在的问题是-如何为下载文件提供扩展名。我不知
我编写了一个 C# WebService。问题是,在我将其发布到 IIS 后,除非调用其任何方法,否则它不会自动启动。这是非常令人沮丧的,因为这个 WebService 必须在启动(其构造函数执行)后
simple spring example demoServiceImpl org.apache.axis2.extensions.spring.
我使用 reSTLet 为我的应用程序构建了 Java Web 服务。它是纯 Java 且独立的。有没有免费的云服务可以托管我的网络服务? 它的要求确实很低。其中之一是静态 IP。 最佳答案 使用 j
我正在研究基于 SOAP 的 Web 服务。我需要测试一个场景,如果由于任何网络问题或登录问题而发生 Web 服务连接错误。 apache cxf 的问题是无论 web 服务抛出什么异常 "java
如何在没有datamapper的情况下在mule中调用soap webservice并且输入是xml。我正在使用社区添加。 & 我的输入是 xml 而不是肥皂信封。 我的 wsdl 位置是 - htt
我知道 php webservice SOAP、json、rest 等,但我是 java webservice 的新手。现在我想让 php 客户端连接到 java webservice。最好的方法是什
我是一名优秀的程序员,十分优秀!