搜索

此内容没有您所选择的语言版本。

29.3. Publishing a Service in a Plain Java Application

download PDF

Overview

When you want to deploy your application as a plain java application you need to implement the logic for publishing your endpoints in the application's main() method. Apache CXF provides you two options for writing your application's main() method.
  • use the main() method generated by the wsdl2java tool
  • write a custom main() method that publishes the endpoints

Generating a Server Mainline

The code generators -server flag makes the tool generate a simple server mainline. The generated server mainline, as shown in Example 29.2, “Generated Server Mainline”, publishes one service provider for each port element in the specified WSDL contract.
For more information see Section E.2, “cxf-codegen-plugin”.
Example 29.2, “Generated Server Mainline” shows a generated server mainline.

Example 29.2. Generated Server Mainline

package org.apache.hello_world_soap_http;

import javax.xml.ws.Endpoint;

public class GreeterServer {

    protected GreeterServer() throws Exception {
        System.out.println("Starting Server");
1        Object implementor = new GreeterImpl();
2        String address = "http://localhost:9000/SoapContext/SoapPort";
3        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);
    }
}
The code in Example 29.2, “Generated Server Mainline” does the following:
1
Instantiates a copy of the service implementation object.
2
Creates the address for the endpoint based on the contents of the address child of the wsdl:port element in the endpoint's contract.
3
Publishes the endpoint.

Writing a Server Mainline

If you used the Java first development model or you do not want to use the generated server mainline you can write your own. To write your server mainline you must do the following:
  1. Instantiate an javax.xml.ws.Endpoint object for the service provider.
  2. Create an optional server context to use when publishing the service provider.
  3. Publish the service provider using one of the publish() methods.
  4. Stop the service provider when the application is ready to exit.
Example 29.3, “Custom Server Mainline” shows the code for publishing a service provider.

Example 29.3. Custom Server Mainline

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
  { 
1    GreeterImpl impl = new GreeterImpl();
2    Endpoint endpt.create(impl);
3    endpt.publish("http://localhost:9000/SoapContext/SoapPort");

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

5   endpt.stop();
    System.exit(0);
  }
}
The code in Example 29.3, “Custom Server Mainline” does the following:
1
Instantiates a copy of the service's implementation object.
2
Creates an unpublished Endpoint for the service implementation.
3
Publishes the service provider at http://localhost:9000/SoapContext/SoapPort.
4
Loops until the server should be shutdown.
5
Stops the published endpoint.
Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

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

让开源更具包容性

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

關於紅帽

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

© 2024 Red Hat, Inc.