Calling Java Services
Seamless platform interoperation
Ignite.NET can work with Java services the same way as with .NET services. On .NET side you only need an interface with methods that have compatible signatures.
By default, all the cluster nodes (Java and .NET ones) have to have Java services classes in their Java classpaths. There are several options on how to overcome this default requirement.
However, this is different from .NET services where a service implementation does not have to be located on all nodes.
Example
Java
Implement service, make sure service methods are public:
public class MyJavaService implements Service {
// Service method to be called from .NET
public String testToUpper(String x) {
return x.toUpperCase();
}
// Service interface implementation
@Override public void cancel(ServiceContext context) { // No-op. }
@Override public void init(ServiceContext context) throws Exception { // No-op. }
@Override public void execute(ServiceContext context) throws Exception { // No-op. }
}
Java service can run on any nodes (.NET, C++, Java-only), so there are no restrictions on deployment:
ignite.services().deployClusterSingleton("myJavaSvc", new MyJavaService());
.NET
Create service interface:
// Interface can have any name
interface IJavaService
{
// Method must have the same name (case-sensitive) and same signature:
// argument types and order.
// Argument names and return type do not matter.
string testToUpper(string str);
}
Get service proxy and invoke method:
var config = new IgniteConfiguration
{
// Make sure that Java service class is in classpath on all nodes, including .NET
JvmClasspath = @"c:\my-project\src\Java\target\classes\"
}
var ignite = Ignition.Start(config);
// Make sure to use the same service name as in deployment
var prx = ignite.GetServices().GetServiceProxy<IJavaService>("myJavaSvc");
string result = prx.testToUpper("invoking Java service...");
Console.WriteLine(result);
Interface Method Mapping
.NET service interface is mapped to Java service dynamically, at the moment of method invocation:
- It is not necessary to have all Java service methods in .NET interface.
- .NET interface can have members that are not present in Java service. You won't get an exception until you call these missing methods.
Java methods are resolved the following way:
- Look for a method with specified name and parameter count. If there is only one - use it.
- Among matched methods look for a method with compatible arguments (via
Class.isAssignableFrom). Invoke it when there is one matching method. Throw an exception when there is more than one. - Method return type is ignored, since .NET and Java do not allow identical methods with different return types.
See Platform Interoperability: Type Compatibility for details on method argument and result mapping.
params/varargs are also supported, since in .NET and Java these are syntactic sugar for object arrays.
Updated almost 5 years ago
