Skip to main content
Skip table of contents

Client-Side Java Mapping for Operations

Mapping for Operations

As we saw in the Client-Side Java Mapping for Interfaces, for each operation on an interface, the generated proxy interface contains 4 methods for this operation. To invoke an operation, you call one of these methods on the proxy. For example, let’s take the generated code from the greeter example:

SLICE
["java:identifier:com.example.visitorcenter"]
module VisitorCenter
{
    interface Greeter
    {
        string greet(string name);
    }
}

The proxy interface generated from the Greeter interface, after removing extra details, is as follows:

JAVA
package com.example.visitorcenter;

public interface GreeterPrx extends com.zeroc.Ice.ObjectPrx {
    default String greet(String name) { ... }
    default String greet(String name, Map<String, String> context) { ... }

    default CompletableFuture<String> greetAsync(String name) { ... }
    default CompletableFuture<String> greetAsync(
            String name, Map<String, String> context) { ... }

    // ...

    static GreeterPrx createProxy(com.zeroc.Ice.Communicator communicator,
            String proxyString) { ... }

    // ...
}

Given a proxy to an object of type Greeter, the client can invoke the greet operation as follows:

JAVA
GreeterPrx greeter = GreeterPrx.createProxy(
    communicator, "greeter:tcp -h localhost -p 4061");

String greeting = greeter.greet("Alice");  // Get name via RPC

Sync and Async Methods

For each operation, the Slice compiler generates 4 methods on the proxy interface:

  • two overloaded “sync” methods with the same name as the operation. When you call these methods, your thread waits synchronously until the invocation completes. A successful invocation completes with a return value (which can be void), while an unsuccessful invocation completes with an exception.

  • two overloaded “async” methods, named <operation-name>Async. When you call these methods, your thread marshals the arguments to the method synchronously, but the remainder of this invocation is asynchronous, and the method returns a CompletableFuture immediately. These async methods are described in more detail in Asynchronous Method Invocation (AMI) in Java.

Async invocations allow you to use threads more efficiently. Sync invocations are more convenient to call. You decide what’s more important for your application.

Exception Handling in Java

Any operation invocation may throw a runtime exception and, if the operation has an exception specification, may also throw user exceptions. Suppose we have the following simple interface:

SLICE
exception Tantrum
{
    string reason;
}

interface Child
{
    void askToCleanUp() throws Tantrum;
}

Slice exceptions are thrown as Java exceptions, so you can simply enclose one or more operation invocations in a try-catch block:

JAVA
ChildPrx child = ...;   // Get child proxy...

try {
    child.askToCleanUp();
} catch (Tantrum t) {
    System.out.write("The child says: ");
    System.out.writeln(t.reason);
}
See Also
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.