Skip to content

Commit 6e30c56

Browse files
authored
Add IceGrid/query Java demo (#627)
1 parent af7faca commit 6e30c56

19 files changed

Lines changed: 727 additions & 0 deletions

File tree

java/IceGrid/query/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Gradle generated build files
2+
bin
3+
build
4+
.gradle

java/IceGrid/query/README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# IceGrid Query
2+
3+
This demo shows how to use the Query object provided by the IceGrid registry to lookup a well-known object by type.
4+
5+
## Ice prerequisites
6+
7+
- Install IceGrid. See [Ice service installation].
8+
9+
## Building the demo
10+
11+
The demo has two Gradle projects, **client** and **server**, both using the [application plugin].
12+
13+
To build the demo, run:
14+
15+
```shell
16+
./gradlew installDist
17+
```
18+
19+
This creates a self-contained distribution under build/install/ for each application.
20+
Each distribution includes:
21+
22+
- a bin/ directory with start scripts, and
23+
- a lib/ directory containing the application JAR and all its runtime dependencies.
24+
25+
In our IceGrid deployment, we call java directly and set the class path to include everything in the distribution’s lib/
26+
directory. This ensures both the server JAR and its dependencies (such as Ice) are available at runtime.
27+
28+
## Running the demo
29+
30+
First, start the IceGrid registry in its own terminal:
31+
32+
```shell
33+
icegridregistry --Ice.Config=config.registry
34+
```
35+
36+
Then, start the IceGrid node in its own terminal:
37+
38+
```shell
39+
icegridnode --Ice.Config=config.node
40+
```
41+
42+
Next, deploy the "GreeterHall" application in this IceGrid deployment:
43+
44+
```shell
45+
icegridadmin --Ice.Config=config.admin -e "application add greeter-hall.xml"
46+
```
47+
48+
Finally, run the client application:
49+
50+
```shell
51+
./gradlew :client:run --quiet
52+
```
53+
54+
[Application plugin]: https://docs.gradle.org/current/userguide/application_plugin.html
55+
[Ice service installation]: https://github.com/zeroc-ice/ice/blob/main/NIGHTLY.md#ice-services
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) ZeroC, Inc.
2+
3+
plugins {
4+
// Apply the application plugin to tell gradle this is a runnable Java application.
5+
id("application")
6+
7+
// Apply the Slice-tools plugin to enable Slice compilation.
8+
id("com.zeroc.slice-tools") version "3.8.+"
9+
10+
// Pull in our local 'convention plugin' to enable linting.
11+
id("zeroc-linting")
12+
}
13+
14+
dependencies {
15+
// Add the Ice and IceGrid libraries as an implementation dependencies.
16+
implementation("com.zeroc:ice:3.8.+")
17+
implementation("com.zeroc:icegrid:3.8.+")
18+
}
19+
20+
sourceSets {
21+
main {
22+
// Add the Greeter.ice file from the parent slice directory to the main source set.
23+
slice {
24+
srcDirs("../slice")
25+
}
26+
}
27+
}
28+
29+
application {
30+
// Specify the main entry point for the application.
31+
mainClass.set("com.example.icegrid.query.client.Client")
32+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
}

java/IceGrid/query/config.admin

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Config file for icegridadmin
2+
3+
# A proxy to the Locator object hosted by the IceGrid registry.
4+
Ice.Default.Locator=IceGrid/Locator:tcp -h localhost -p 4061
5+
6+
# Dummy username and password. They work because IceGrid is configured with the NullPermissionsVerifier.
7+
IceGridAdmin.Username=foo
8+
IceGridAdmin.Password=bar

java/IceGrid/query/config.node

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Config file for icegridnode
2+
3+
# A proxy to the Locator object hosted by the IceGrid registry.
4+
Ice.Default.Locator=IceGrid/Locator:tcp -h localhost -p 4061
5+
6+
# The name of this IceGrid node.
7+
IceGrid.Node.Name=node1
8+
9+
# The endpoints of this node's object adapter. This object adapter receives requests from the IceGrid registry.
10+
# We configure this object adapter to listen on an OS-assigned tcp port on the loopback interface since the IceGrid
11+
# registry runs on the same host in this deployment.
12+
IceGrid.Node.Endpoints=tcp -h 127.0.0.1
13+
14+
# The directory where the node stores the config files for the Ice servers it starts.
15+
IceGrid.Node.Data=db/node
16+
17+
# Trace activation of Ice servers (3 = very verbose).
18+
IceGrid.Node.Trace.Activator=3

java/IceGrid/query/config.registry

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Config file for icegridregistry
2+
3+
# The endpoints of this registry's client object adapter. This object adapter receives requests from the clients.
4+
# We configure it to listen on tcp port 4061, on all interfaces.
5+
IceGrid.Registry.Client.Endpoints=tcp -p 4061
6+
7+
# The endpoints of this registry's server object adapter. This object adapter receives requests from the servers when
8+
# they register/unregister themselves or their endpoints with the registry.
9+
# We configure this object adapter to listen on an OS-assigned tcp port, on all interfaces.
10+
IceGrid.Registry.Server.Endpoints=tcp
11+
12+
# The endpoints of this registry's internal object adapter. This object adapter receives requests from the IceGrid
13+
# nodes.
14+
# We configure this object adapter to listen on an OS-assigned tcp port, on the loopback interface since the IceGrid
15+
# node runs on the same host in this deployment.
16+
IceGrid.Registry.Internal.Endpoints=tcp -h 127.0.0.1
17+
18+
# The directory when the registry stores its LMDB database. This directory must exist and be writable by the registry.
19+
IceGrid.Registry.LMDB.Path=db/registry
20+
21+
# The admin interface of the registry accepts any username/password combination.
22+
IceGrid.Registry.AdminPermissionsVerifier=IceGrid/NullPermissionsVerifier
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
servers/
2+
tmp/
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.mdb
2+
*.lock
42.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)