-
Notifications
You must be signed in to change notification settings - Fork 216
Update java/Ice/greeter shutdown #332
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,17 +2,18 @@ | |
|
|
||
| package com.zeroc.demos.greeter; | ||
|
|
||
| import com.zeroc.demos.greeter.Chatbot; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't need import since Chatbot is in the same package. |
||
| import com.zeroc.Ice.Communicator; | ||
| import com.zeroc.Ice.ObjectAdapter; | ||
|
|
||
| class Server { | ||
| public static void main(String[] args) { | ||
| // Create an Ice communicator to initialize the Ice runtime. The communicator is disposed before the program exits. | ||
| // Create an Ice communicator to initialize the Ice runtime. The communicator is closed (destroyed) at the end | ||
| // of the try statement. | ||
| try (Communicator communicator = com.zeroc.Ice.Util.initialize(args)) { | ||
| Runtime.getRuntime().addShutdownHook(new Thread(() -> { | ||
| communicator.destroy(); | ||
| })); | ||
|
|
||
| // Register a shutdown hook that calls communicator.shutdown() when the user shuts down the server with | ||
| // Ctrl+C or similar. The shutdown hook thread also waits until the current thread completes its cleanup. | ||
| shutdownCommunicatorOnCtrlC(communicator, Thread.currentThread()); | ||
|
|
||
| // Create an object adapter that listens for incoming requests and dispatches them to servants. | ||
| ObjectAdapter adapter = communicator.createObjectAdapterWithEndpoints("GreeterAdapter", "tcp -p 4061"); | ||
|
|
@@ -24,9 +25,22 @@ public static void main(String[] args) { | |
| adapter.activate(); | ||
| System.out.println("Listening on port 4061..."); | ||
|
|
||
| // Wait until the user presses Ctrl+C. | ||
| // Wait until the communicator is shut down. Here, this occurs when the user presses Ctrl+C. | ||
| communicator.waitForShutdown(); | ||
| System.out.println("Caught Ctrl+C, exiting..."); | ||
| } | ||
| } | ||
|
|
||
| private static void shutdownCommunicatorOnCtrlC(Communicator communicator, Thread cleanupThread) { | ||
|
||
| Runtime.getRuntime().addShutdownHook(new Thread(() -> { | ||
| System.out.println("Caught Ctrl+C, shutting down..."); | ||
| communicator.shutdown(); | ||
|
|
||
| // Wait until the cleanup thread completes. | ||
| try { | ||
| cleanupThread.join(); | ||
| } catch (InterruptedException e) { | ||
| assert false : "Shutdown thread cannot be interrupted"; | ||
| } | ||
| })); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to answer your question, this is the default location where the
applicationplugin we use puts these executables. Maybe we could tweak it, but this is the default.If you're a normal Java developer, used to gradle, what you would expect to do is this:
And this does work, but it also outputs extra clutter which obscures what the demo is actually doing.
You can remove this clutter by running:
Which I think is slightly clearer than having to type out the paths directly.
So, I think we should switch to recommending these commands instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we change this because how Ctrl-C is handled by the scripts.