This article is a part of WCF & Java Interop

WCF’s fundamental communication mechanism is SOAP-based Web services. Because WCF implements Web services technologies defined by the WS-* specifications, other software which is based on SOAP and supports WS-* specifications can communicate with the WCF-based applications.

 

To build up a cross-platform WCF server, you can use Metro. Metro is a Web Services framework that provides tools and infrastructure to develop Web Services solutions for the end users and middleware developers. It depends on Java programming language. The latest version of Metro is 1.4.

 

In the development process of SCM Anywhere (a SCM tool, with fully integrated version control, Issue Tracking and Build Automation, developed with WCF and METRO/WSIT), we found that METRO is NOT as mature as WCF. There are lots of small issues in METRO/WSIT. Luckily, METRO is an open source project and keeps evolving all the time. Our experience is that if you find some features are not working properly in METRO, keep downloading the latest version from Java.net. Several weeks later, you may discover that the features are working properly.

 

To implement a Java client to communicate with the WCF server, you can follow the steps below:

1. Download METRO/WSIT from the home page of Metro: http://metro.java.net/2.2/.

2. Download Eclipse. We use Eclipse + Metro to develop Dynamsoft SCM Anywhere.

3. Install Metro by executing the command: java –jar metro-1_4.jar. The installation folder of Metro contains some documents, tools and samples. You can find the documents in the “docs” folder.

4. Use the C# project “WcfService1″ (provided in my WCF client and WCF service article) as the WCF server. Go to Property of the WCF project, and set the server port to one that is not occupied by other services. Here we used 8888 for example. In the “web.config” file, change the string “wsHttpBinding” to “basicHttpBinding”.

5. This is the key step. We use the wsimport tool included in Metro to generate the Java client code. Create a file named “service1.xml” and copy the following code to the file:

<bindings
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
wsdlLocaption="http://localhost:8888/Service1.svc?wsdl"
xmlns="http://java.sun.com/xml/ns/jaxws">
<bindings node="wsdl:definitions">
<enableAsyncMapping>true</enableAsyncMapping>
</bindings>
</bindings>

The parameter “enableAsyncMapping” means generating the asynchronous method communicating to WCF server. Save this file, and execute the following command:

bin\wsimport -extension -keep -Xnocompile -b TestService.xml http://localhost:8888/Service1.svc?wsdl

Then you can find two new directories in Metro folder: “org” and “com”. They are the generated Java code.

6. Open Eclipse IDE, create a new Java project named “SimpleWCFClient”, and copy the two new directories “org” and “com” to the “src” folder of the project. Refresh the project, and you can find that some new code files are in the project.

7. Create a test class named “WCFTest” and write the following code to the file:

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;
import org.tempuri.IService1;
import org.tempuri.Service1;

public class WCFTest {
public static void main(String[] strArgs)
{
try
{
Service1 service1 = new Service1(new URL(“http://localhost:8888/Service1.svc?wsdl”), new QName(“http://tempuri.org/”, “Service1″));
IService1 port = service1.getBasicHttpBindingIService1();
((BindingProvider)port).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, “http://localhost:8888/Service1.svc”);
int input = 100;
String strOutput = port.getData(input);
System.out.println(strOutput);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

8. Four .jar files need to be added to the Java project. You can get these files in the “lib” folder of Metro: webservices-api.jar, webservices-extra.jar, webservices-extra-api.jar, webservices-rt.jar. Then go to Property of the project and add these jars to the project.

9. Compile and run the Java project. If the Eclipse console outputs “You entered: 100″, congratulations, you are successful.

 

You can download these code from here. When you are familiar with these, you will find it’s very convenient to write a Java application communicating with a WCF client.

 

Links:
Previous article >>>>: WCF client and WCF service
Next article >>>>: Data types between Java and WCF
WCF & Java Interop series home page: WCF & Java Interop