CXF WS-Discovery Example

CXF WS-Discovery Example explain about configuring WS-Discovery service with Apache CXF.

Web Services Dynamic Discovery (WS-Discovery) is a protocol, which enable dynamic discovery of services available on the local network. By default, WS-Discovery uses a UDP based multicast transport to announce new services

Apache CXF  is a free and open source project, and a fully featured Webservice framework. It helps you building webservices using different front-end API's, like as JAX-RS and JAX-WS.

WS-Discovery is available from Apache CXF 2.7.x onwards.

Reference-> http://cxf.apache.org/docs/ws-discovery.htm

Required Libraries

You need to download

  1. JDK 6
  2. Eclipse 3.7
  3. CXF-2.7.12
  4. Tomcat 7

Following jar must be in ClassPath

  1. asm-3.3.1.jar
  2. commons-logging-1.1.1.jar
  3. cxf-2.7.12.jar
  4. cxf-services-ws-discovery-api-2.7.12.jar
  5. cxf-services-ws-discovery-service-2.7.12.jar
  6. httpasyncclient-4.0-beta3.jar
  7. httpclient-4.2.5.jar
  8. httpcore-4.2.4.jar
  9. httpcore-nio-4.2.4.jar
  10. jetty-continuation-8.1.15.v20140411.jar
  11. jetty-http-8.1.15.v20140411.jar
  12. jetty-io-8.1.15.v20140411.jar
  13. jetty-server-8.1.15.v20140411.jar
  14. jetty-util-8.1.15.v20140411.jar
  15. mina-core-2.0.7.jar
  16. neethi-3.0.3.jar
  17. slf4j-api-1.7.7.jar
  18. slf4j-jdk14-1.7.7.jar
  19. stax2-api-3.1.4.jar
  20. woodstox-core-asl-4.4.0.jar
  21. wsdl4j-1.6.3.jar
  22. xml-resolver-1.2.jar
  23. xmlschema-core-2.1.0.jar

CXF WS-Discovery Example

I am creating a sample web service project that pass Student object and return with some changes on that object. The service is using simple POJO (Plain Old Java Object) bean.

Firstly create a Dynamic Web Project (File->New->Dynamic Web Project) named "CXFTutorial" according to following screenshot

CXF WS-Discovery Example CXF WS-Discovery Example

Create a Student Object

package com.student;

public class Student {
 
private String name;
 
public String getName() {
   
return name;
 
}
 
public void setName(String name) {
   
this.name = name;
 
}
}

Create a Service Interface

This service interface will defines which methods of web service, to be invoked by the client

package com.student;

import javax.jws.WebService;

@WebService
public interface ChangeStudentDetails {
 
Student changeName(Student student);
}

Implement the Service Interface

Here we implement the service interface created on the previous step

package com.student;

import javax.jws.WebService;

@WebService(endpointInterface = "com.student.ChangeStudentDetails")
public class ChangeStudentDetailsImpl implements ChangeStudentDetails {
   
public Student changeName(Student student) {
     
student.setName("Hello "+student.getName());
     
return student;
   
}
}

Create a SimpleCXFNonSpringServlet

Here we are using SimpleCXFNonSpringServlet class in order to add service implementation class(ChangeStudentDetailsImpl)

We are not hard coding the port for deploying the service, but using random port. Deployment is done dynamically using CXF WS-Discovery service, so port may change on each deployment.

package com.student;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;

import javax.servlet.ServletConfig;
import javax.xml.ws.Endpoint;
import org.apache.cxf.Bus;
import org.apache.cxf.BusFactory;
import org.apache.cxf.transport.servlet.CXFNonSpringServlet;

public class SimpleCXFNonSpringServlet extends CXFNonSpringServlet {

 
private static final long serialVersionUID = 1L;

 
@Override
 
public void loadBus(ServletConfig servletConfig) {
   
super.loadBus(servletConfig);
    Bus bus = getBus
();
    BusFactory.setDefaultBus
(bus);
   
   
try {
       
ServerSocket sock = new ServerSocket();
        InetSocketAddress s =
new InetSocketAddress(InetAddress.getLocalHost(), 0);
        sock.bind
(s);
       
int port = sock.getLocalPort();
        sock.close
();
        String address =
"http://localhost:" + port + "/ChangeStudent";
        System.out.println
("Publishing on " + address);
       
        Endpoint.publish
(address, new ChangeStudentDetailsImpl());
   
} catch (Exception e) {
       
// TODO Auto-generated catch block
       
e.printStackTrace();
   
}
  }
}

web.xml

<web-app id="WebApp_ID" version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
	<servlet>
		<display-name>SimpleCXFNonSpringServlet</display-name>
		<servlet-name>SimpleCXFNonSpringServlet</servlet-name>
		<servlet-class>com.student.SimpleCXFNonSpringServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>SimpleCXFNonSpringServlet</servlet-name>
		<url-pattern>/*</url-pattern>
	</servlet-mapping>
</web-app>

Publishing CXF Web Service

Run CXF WS-Discovery Example

Note

WS-Discovery operates over TCP and UDP port 3702 and uses IP multicast address 239.255.255.250 (see below console screenshot). Currently service is deployed on the port as "48656", but this will change on each deployment.

http://localhost:48656/ChangeStudent?wsdl

Below screenshot you can see service is automatically deployed using CXF WS-Discovery Service

CXF WS-Discovery Console

Note

you can also see CXF WS-Discovery Client in order to run this service











5 Responses to "CXF WS-Discovery Example"
  1. Anton 2014-06-03 21:02:57.0
  1. admin 2014-06-04 21:02:57.0
  1. Carson 2014-06-05 21:02:57.0
  1. Carson 2014-06-06 21:02:57.0
  1. Carson 2014-06-07 21:02:57.0

Your email address will not be published. Required fields are marked *