9

We have a message processing server, which

  • start a few threads
  • processing the message
  • interact with the database etc.....

now the client want to have a web service server on the server, they will be able to querying the message processing server, with a web service client. e.g. give me all the messages for today, or delete the message with id....

the problem are:

  • The server just a standard j2se application, doesn't run inside application server, like tomcat or glassfish.
  • To handle a Http request, do I need to implement a http server?
  • I would like to use the nice j2ee annotation such as @webservice, @webmothod etc... is there any library or framework I can use
kolossus
  • 20,559
  • 3
  • 52
  • 104
Junchen Liu
  • 5,435
  • 10
  • 51
  • 62
  • Why cant you deploy the web service application on any container like tomcat or glassfish? and you can write its client to any j2se application. Is there any restriction from client? – zaffargachal Oct 12 '12 at 13:56
  • @zaffargachal It was not design like that, I suppose after 10 yes running we tell them the server need go into a thing called glassfish. they will go mad. – Junchen Liu Oct 12 '12 at 13:59
  • Cant you have other solution than web service, like RMI if consumer client is written in JAVA? – zaffargachal Oct 12 '12 at 14:02

2 Answers2

9

You don't need a third party library to use annotations. J2SE ships with , so all the annotations are still available to you. You can achieve lightweight results with the following solution, but for anything optimized/multi-threaded, it's on your own head to implement:

  1. Design a SEI, service endpoint interface, which is basically a java interface with web-service annotations. This is not mandatory, it's just a point of good design from basic OOP.

    import javax.jws.WebService;
    import javax.jws.WebMethod;
    import javax.jws.WebParam;
    import javax.jws.soap.SOAPBinding;
    import javax.jws.soap.SOAPBinding.Style;
    
    @WebService
    @SOAPBinding(style = Style.RPC) //this annotation stipulates the style of your ws, document or rpc based. rpc is more straightforward and simpler. And old.
    public interface MyService{
    @WebMethod String getString();
    
    }
    
  2. Implement the SEI in a java class called a SIB service implementation bean.

    @WebService(endpointInterface = "com.yours.wsinterface") //this binds the SEI to the SIB
    public class MyServiceImpl implements MyService {
    public String getResult() { return "result"; }
     }
    
  3. Expose the service using an Endpoint import javax.xml.ws.Endpoint;

    public class MyServiceEndpoint{
    
    public static void main(String[] params){
      Endpoint endPoint =  EndPoint.create(new MyServiceImpl());
      endPoint.publish("http://localhost:9001/myService"); //supply your desired url to the publish method to actually expose the service.
       }
    }
    

The snippets above, like I said, are pretty basic, and will perform poorly in production. You'll need to work out a threading model for requests. The endpoint API accepts an instance of Executor to support concurrent requests. Threading's not really my thing, so I'm unable to give you pointers.

kolossus
  • 20,559
  • 3
  • 52
  • 104
  • Note the answer above is a pseudo code, the concept is right, but the code might not work. here is a very simple but effective tutorial, and the code definitely works. http://www.mkyong.com/webservices/jax-ws/jax-ws-hello-world-example/ – Junchen Liu Oct 16 '12 at 09:02
  • also when your application exit you must stop the webservice gracefully. otherwise you would have problem to start again. as the port already bind?ect.. – Junchen Liu Oct 16 '12 at 09:14
  • 1
    How does one determine the correct value to use for `"com.yours.wsinterface"`? – Duncan Jones May 12 '14 at 10:29
  • 1
    @Duncan - the correct value would be the FQN to the java interface that's annotated with `@WebService` (in this example, `MyService`). in this case, the correct value would be `com.foo.MyService`, because that's what `MyServiceImpl ` implements. – kolossus May 12 '14 at 11:32
0

For using nice j2ee annotations see Apache CXF http://cxf.apache.org/.

Aleksandr M
  • 24,264
  • 12
  • 69
  • 143