12

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.

tster
  • 17,883
  • 5
  • 53
  • 72
  • Why would you want to do this? The *only* good reason for SOAP is for communicating between applications - why on earth would you want your application to talk to itself via SOAP? – skaffman Nov 24 '09 at 20:36
  • 1
    I read this as programmatically loading different services on the server for external clients to be able to call. I'd still do that in a config file, though, instead of quite like that. – Dean J Nov 24 '09 at 20:41
  • It obviously isn't talking to itself. The listen command should make it listen on a port, the server will accept the HTTP, parse out the XML and forward the call onto the correct method with parameters and whatnot. The reason I want this is because the web service is a small part of the application, but it needs to interact with the rest of the application. Furthermore, I don't want to add 2 running programs when I can just add 1 (especially when they need to communicate to each other). – tster Nov 24 '09 at 20:43
  • @Dean, yes, that's what I want. The important part though is that the service interacts with other things the program is doing, so I want it to share an address space. – tster Nov 24 '09 at 20:44

4 Answers4

24

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..

nos
  • 223,662
  • 58
  • 417
  • 506
  • As an added bonus, this comes in the jax-ws project (part of Glassfish) and only requires Java 5. You cane download the releases here: https://jax-ws.dev.java.net/ – tster Nov 24 '09 at 22:19
  • Seems you don't even need that if you're on suns jdk 6 – nos Nov 24 '09 at 22:53
  • I think the wsgen command should be: wsgen -d build -s build/ -classpath build/ helloservice.endpoint.Hello – sixtyfootersdude Jan 26 '10 at 16:30
  • When I go to the first URL I just get this text: "Web Services No JAX-WS context information available." Anyone getting the same thing? Is that what is supposed to be displayed? I am not sure... The second URL displays xml.. as expected. – sixtyfootersdude Jan 26 '10 at 16:33
  • If I understand correctly that corresponds to a bottom-up web service when you implement the method in Java and get the WSDL out of it. Can you do the same in a top-down method (i.e. start with an WSDL) that runs in-process, using standard JDK ? – Marcus Junius Brutus Sep 17 '13 at 20:59
  • @MarcusJuniusBrutus Yes, the `wsimport` tool (as opposed to the wsgen tool used here) goes the other way. – nos Sep 17 '13 at 21:11
  • @nos Building upon your answer, I experimented on the client side (again, from a standalone Java application perspective): http://stackoverflow.com/questions/18925888/jax-ws-ways-to-call-a-web-service-from-a-standalone-java-7-se-client – Marcus Junius Brutus Sep 20 '13 at 21:08
1

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.

tster
  • 17,883
  • 5
  • 53
  • 72
1

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.

Luke Girvin
  • 13,221
  • 9
  • 64
  • 84
Stefan L
  • 1,529
  • 13
  • 20
0

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.

Sonny
  • 2,103
  • 1
  • 26
  • 34