Skip to content
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

Examples: Updated HelloWorldServer to use Executor #11850

Merged
merged 12 commits into from
Feb 18, 2025
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@
import io.grpc.InsecureServerCredentials;
import io.grpc.Server;
import io.grpc.stub.StreamObserver;
import io.grpc.ServerBuilder;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see this getting used anywhere! Remove this import.

I wonder how it is making this far by clearing all the github tests. It should already be failing in checkstyle saying unused import or something!!! @ejona86

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, In local build as well as in the PR check, I don't see any issues related to the same

import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
* Server that manages startup/shutdown of a {@code Greeter} server.
Expand All @@ -31,11 +34,13 @@ public class HelloWorldServer {
private static final Logger logger = Logger.getLogger(HelloWorldServer.class.getName());

private Server server;

// Create a fixed thread pool
ExecutorService executor = Executors.newFixedThreadPool(2);
private void start() throws IOException {
/* The port on which the server should run */
int port = 50051;
server = Grpc.newServerBuilderForPort(port, InsecureServerCredentials.create())
.executor(executor)
.addService(new GreeterImpl())
.build()
.start();
Expand All @@ -48,6 +53,10 @@ public void run() {
try {
HelloWorldServer.this.stop();
} catch (InterruptedException e) {
if (server != null) {
server.shutdownNow();
}
executor.shutdown();
e.printStackTrace(System.err);
}
System.err.println("*** server shut down");
Expand Down