31.3. 在 Plain Java Application 中发布服务


概述

当您要将应用程序部署为普通 java 应用时,您需要实现在应用的 main() 方法中发布端点的逻辑。Apache CXF 为您提供了编写应用程序的 main() 方法的两个选项。

  • 使用 wsdl2java 工具生成的 main() 方法
  • 编写发布端点的自定义 main() 方法

生成服务器主线

代码生成器 -server 标志使工具生成简单的服务器主线。生成的服务器主要线(如 例 31.2 “生成的服务器主线” 所示)为指定 WSDL 合同中的每个 端口 元素发布一个服务提供商。

更多信息请参阅 第 44.2 节 “cxf-codegen-plugin”

例 31.2 “生成的服务器主线” 显示生成的服务器主线。

例 31.2. 生成的服务器主线

package org.apache.hello_world_soap_http;

import javax.xml.ws.Endpoint;

public class GreeterServer {

    protected GreeterServer() throws Exception {
        System.out.println("Starting Server");
        Object implementor = new GreeterImpl();
        String address = "http://localhost:9000/SoapContext/SoapPort";
        Endpoint.publish(address, implementor);
    }

    public static void main(String args[]) throws Exception {
        new GreeterServer();
        System.out.println("Server ready...");

        Thread.sleep(5 * 60 * 1000);
        System.out.println("Server exiting");
        System.exit(0);
    }
}

例 31.2 “生成的服务器主线” 中的代码执行以下操作:

实例化服务实施对象的副本。

根据端点的合同中 wsdl:port 元素的内容,创建端点的地址。

发布端点。

编写服务器主线

如果您使用了 Java 第一开发模型,或者您不想使用生成的服务器主线,您可以自行编写。要编写服务器主线,您必须执行以下操作:

  1. “实例化服务提供商”一节 服务提供商的 javax.xml.ws.Endpoint 对象。
  2. 创建发布服务提供商时要使用的可选服务器上下文。
  3. “发布服务供应商”一节 服务提供商使用一个 publish() 方法。
  4. 当应用程序准备好退出时,停止服务提供商。

例 31.3 “自定义服务器主线” 显示发布服务提供商的代码。

例 31.3. 自定义服务器主线

package org.apache.hello_world_soap_http;

import javax.xml.ws.Endpoint;

public class GreeterServer
{
  protected GreeterServer() throws Exception
  {
  }

  public static void main(String args[]) throws Exception
  {
    GreeterImpl impl = new GreeterImpl();
    Endpoint endpt.create(impl);
    endpt.publish("http://localhost:9000/SoapContext/SoapPort");

    boolean done = false;
   while(!done)
    {
      ...
    }

   endpt.stop();
    System.exit(0);
  }
}

例 31.3 “自定义服务器主线” 中的代码执行以下操作:

实例化服务实施对象的副本。

为服务实施 创建 未发布的端点。

将服务提供商发布至 http://localhost:9000/SoapContext/SoapPort

循环,直到服务器应关闭为止。

停止公布的端点。

Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.