如何关闭自托管的Apache CXF Web服务?

时间:2012-08-13 18:56:22

标签: java cxf

我有一个控制台应用程序,可以使用以下代码简要描述:

public static void main(String[] args) throws InterruptedException {
    JAXRSServerFactoryBean sfb = new JAXRSServerFactoryBean();
    sfb.setResourceClasses(DummyService.class);
    sfb.setResourceProvider(DummyService.class, new SingletonResourceProvider(new DummyServiceImpl()));
    sfb.setProvider(new JacksonJaxbJsonProvider());
    sfb.setAddress("http://0.0.0.0:9000/");
    BindingFactoryManager manager = sfb.getBus().getExtension(BindingFactoryManager.class);
    JAXRSBindingFactory bindingFactory = new JAXRSBindingFactory();
    bindingFactory.setBus(sfb.getBus());
    manager.registerBindingFactory(JAXRSBindingFactory.JAXRS_BINDING_ID, bindingFactory);
    Server server = sfb.create();       
    server.stop(); // here
}

我从Eclipse运行这个程序,我希望它立即完成,因为我最终有server.stop()。这行代码由应用程序保持活动状态执行,因此我必须手动关闭它。这是日志CXF打印到控制台:

Aug 13, 2012 10:51:10 PM org.apache.cxf.endpoint.ServerImpl initDestination
INFO: Setting the server's publish address to be http://0.0.0.0:9000/
2012-08-13 22:51:10.598:INFO:oejs.Server:jetty-7.5.3.v20111011
2012-08-13 22:51:10.646:INFO:oejs.AbstractConnector:Started SelectChannelConnector@0.0.0.0:9000 STARTING
2012-08-13 22:51:10.662:INFO:oejsh.ContextHandler:started o.e.j.s.h.ContextHandler{,null}
2012-08-13 22:51:10.663:INFO:oejsh.ContextHandler:stopped o.e.j.s.h.ContextHandler{,null}

在调用server.stop()应用程序后仍在运行,但服务器没有响应任何请求,所以感觉我需要关闭其他内容。

什么是阻止它的正确方法?

更新

server.stop();
server.destroy();

也行不通。

更新

<dependency>
  <groupId>org.apache.cxf</groupId>
  <artifactId>cxf-bundle-jaxrs</artifactId>
  <version>2.6.1</version>
</dependency>

2 个答案:

答案 0 :(得分:7)

已修复

server.stop();
server.destroy();
sfb.getBus().shutdown(true)

感觉像是肮脏的黑客。

答案 1 :(得分:0)

来自javadoc(http://cxf.apache.org/javadoc/latest/org/apache/cxf/jaxrs/JAXRSServerFactoryBean.html):

  

[..]这将为您启动服务器并将其注册到   ServerManager的。请注意,您应该显式关闭完成后创建的服务器:

myServer.close();
myServer.destroy();