|
| 1 | +// Copyright (c) ZeroC, Inc. |
| 2 | + |
| 3 | +package com.example.icegrid.query.client; |
| 4 | + |
| 5 | +import com.example.visitorcenter.GreeterPrx; |
| 6 | +import com.zeroc.Ice.Communicator; |
| 7 | +import com.zeroc.Ice.LocatorPrx; |
| 8 | +import com.zeroc.Ice.ObjectPrx; |
| 9 | +import com.zeroc.Ice.Util; |
| 10 | +import com.zeroc.IceGrid.QueryPrx; |
| 11 | + |
| 12 | +class Client { |
| 13 | + public static void main(String[] args) { |
| 14 | + // Create an Ice communicator. We'll use this communicator to create proxies and manage outgoing connections. |
| 15 | + try (Communicator communicator = Util.initialize(args)) { |
| 16 | + |
| 17 | + // Set the default locator of the new communicator. It's the address of the Locator hosted by our IceGrid |
| 18 | + // registry. You can also set this proxy with the Ice.Default.Locator property. |
| 19 | + communicator.setDefaultLocator( |
| 20 | + LocatorPrx.createProxy(communicator, "IceGrid/Locator:tcp -h localhost -p 4061")); |
| 21 | + |
| 22 | + // Create a proxy to the Query object hosted by the IceGrid registry. "IceGrid/Query" a well-known proxy, |
| 23 | + // without addressing information. |
| 24 | + QueryPrx query = QueryPrx.createProxy(communicator, "IceGrid/Query"); |
| 25 | + |
| 26 | + // Look up an object with type ::VisitorCenter::Greeter. |
| 27 | + String greeterTypeId = GreeterPrx.ice_staticId(); // ::VisitorCenter::Greeter |
| 28 | + ObjectPrx proxy = query.findObjectByType(greeterTypeId); |
| 29 | + |
| 30 | + if (proxy == null) { |
| 31 | + System.out.println( |
| 32 | + String.format("The IceGrid registry doesn't know any object with type '%s'.", greeterTypeId)); |
| 33 | + } else { |
| 34 | + // Cast the object proxy to a Greeter proxy. |
| 35 | + GreeterPrx greeter = GreeterPrx.uncheckedCast(proxy); |
| 36 | + |
| 37 | + // Send a request to the remote object and get the response. |
| 38 | + String greeting = greeter.greet(System.getProperty("user.name")); |
| 39 | + System.out.println(greeting); |
| 40 | + |
| 41 | + // Send another request to the remote object. With the default configuration we use for this client, |
| 42 | + // this request reuses the connection and reaches the same server, even when we have multiple |
| 43 | + // replicated servers. |
| 44 | + greeting = greeter.greet("alice"); |
| 45 | + System.out.println(greeting); |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | +} |
0 commit comments