Let us use the following simple interface (with a single method for simplicity):
public interface Reverse {
public String reverseString( String str );
}
Compile this file and make sure it is
available in the Java Services classpath.
jsregintf Reverse
This command should output a message saying the registration
was successful.
public class MyProvider implements Reverse {
public String reverseString( String str )
{
StringBuffer rev = new StringBuffer();
for ( int i = str.length()-1; i > 0; i-- )
rev.append( str.charAt( i ));
return rev.toString();
}
}
jsregprovider MyProvider Reverse
Which is telling JavaServices that "MyProvider" class will provide the interface "Reverse".
If the classpath is setup correctly, the client simply looks up the "Reverse" interface using the hostname/IP-address, and the port number, of the server.
public class SomeClient {
public static void main( String[] args )
{
try {
// Get an instance of Java Services
JSVCS jsvc = new JSVCS();
// Lookup if there is a "Reverse" interface registered at a given host
Reverse r = (Reverse) jsvc.lookup( "JavaServiceHost.com", 4444, Reverse.class );
// That's it. Use the interface.
String reversed = r.reverseString( "A String" );
System.out.println( "Reversed = " + reversed );
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
It is as simple as that! As long as the interface "Reverse" is registered with JSVCS running
at JavaServiceHost.com at port 4444, the above client will be able to use the
interface to make remote calls over the web.
Note that the client does not need to know anything about "MyProvider". The provider class can change, or be replaced, and client will continue to work as expected.