English 中文(简体)
In-process SOAP service server for Java
原标题:

OK, I am developing a program which will be deployed to lots of machines (Windows, Linux, AIX, z/Linux, openVMS, etc.). I want that application to contain a SOAP web service, but I don t want to bundle tomcat or run a separate service for the services (I want them in the same process as the rest of the application).

Basically what I m looking for is something where I can define a class (say WebServices). I m OK with writing WSDL or any other kind of service description as well. The I want something like this:

SOAPServer server = makeMeASoapServer();
//do config on the server
server.add(new WebService(...));
server.listen(port);

Obviously the names and parameters will be different.

I ve been looking at Axis, and it seems like it provides this, but I don t know what classes I need to use. Am I crazy in wanting this kind of behavior? I can t believe more people aren t looking for this, I do this all the time with embedded web services within .NET clients.

最佳回答

Seems jdk 6.0 already comes with a jax-ws implementation, and a little server you can embed. I havn t figured out all the pieces but here s a start:

mkdir -p helloservice/endpoint/

helloservice/endpoint/Hello.java :

package helloservice.endpoint;

import javax.jws.WebService;

@WebService()
public class Hello {
  private String message = new String("Hello, ");

  public void Hello() {}

  public String sayHello(String name) {
    return message + name + ".";
  }
}

helloservice/endpoint/Server.java:

package helloservice.endpoint;
import javax.xml.ws.Endpoint;

public class Server {

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

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

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

Build the thing:

mkdir build
javac -d build helloservice/endpoint/*java
$JAVA_HOME/wsgen -d build -s build -classpath .  helloservice.endpoint.Hello

Run the thing:

java -cp  build helloservice.endpoint.Server

Somethings running on http://localhost:9000/SoapContext/SoapPort now. You can get the wsdl on http://localhost:9000/SoapContext/SoapPort?WSDL

Havn t gotten around to making a client yet..

问题回答

In addition to nos s great answer, I found a class in Apache axis called SimpleHTTPServer which I m pretty sure does the same thing but only requires Java 1.5 for those of you stuck with 1.5

I m not going to explore it since I m going to use the other solution, so I haven t actually verified it does what I think it does, but I m pretty sure it does.

Most(/all?) Java SOAP server implementations provide a Servlet (the javax.xml.ws.Endpoint approach in another answer does look a bit simpler though...). Some SOAP implementations you could consider are: Apache CXF: cxf.apache.org, Apache Axis2: ws.apache.org/axis2/ or Spring Web Servies: static.springsource.org/spring-ws/site/ .

The most popular embedded Java web server seems to be Jetty, you can configure it either programatically (using plain Java or Spring beans) or using a custom XML format.

To address the main question directly, another approach would be to go with Jetty s embedded server. See this link for details. The links from the aforelinked page help you understand both the simple web server (i.e., one that serves up static pages; though I am fully aware "simple" is a horribly vague term wrt web servers) and the web server that helps you deploy web services.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签