Server-Side Java Mapping for Operations
Default Mapping for Operations
As we saw in the Server-Side Java Mapping for Interfaces, for each operation on an interface, the generated skeleton interface contains an abstract method with the same name.
For example, let’s take the generated code from the greeter example:
["java:identifier:com.example.visitorcenter"]
module VisitorCenter
{
interface Greeter
{
string greet(string name);
}
}
The skeleton interface generated from the Greeter interface, after removing extra details, is as follows:
package com.example.visitorcenter;
public interface Greeter extends com.zeroc.Ice.Object {
String greet(String name, com.zeroc.Ice.Current current);
// ...
}
The greet method takes a String name and a Current, then returns a value of type String. This method should be implemented in your derived servant class with something like:
class Chatbot implements Greeter {
@Override
public String greet(String name, Current current) {
return "Hello, " + name + "!";
}
}
AMD Mapping for Operations
Each operation with the ["amd"] metadata is mapped to a method with an Async suffix in the skeleton interface. The AMD mapping replaces the default “sync” mapping for the operation. See Asynchronous Method Dispatch (AMD) in Java for details.
Throwing Exceptions
To throw an exception from an operation implementation, you simply construct the exception and throw it. For example:
@Override
public void write(String[] text, Current current) throws WriteException {
// Try to write the file contents here...
// Assume we are out of space...
if (error) {
throw new WriteException("file too large");
}
}
Like on the client-side, the Slice exception specification maps to an exception specification on the corresponding Java method.
If you throw an arbitrary Java exception (such as a IllegalArgumentException), the Ice runtime catches the exception and then returns an UnknownException to the client.
If you throw an Ice runtime exception, such as MarshalException, the client receives an UnknownLocalException.
The server-side Ice runtime does not validate user exceptions thrown by an operation implementation to ensure they are compatible with the operation's Slice definition. Rather, Ice returns the user exception to the client, where the client-side runtime will validate the exception as usual and throws UnknownUserException for an unexpected exception type.