Skip to content

Examples: Updated HelloWorldServer to use Executor #11850

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

Merged
merged 12 commits into from
Feb 18, 2025
Merged
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,20 @@ public class HelloWorldServer {
private static final Logger logger = Logger.getLogger(HelloWorldServer.class.getName());

private Server server;

private void start() throws IOException {
/* The port on which the server should run */
int port = 50051;
/*
* By default gRPC uses a global, shared Executor.newCachedThreadPool() for gRPC callbacks into
* your application. This is convenient, but can cause an excessive number of threads to be
* created if there are many RPCs. It is often better to limit the number of threads your
* application uses for processing and let RPCs queue when the CPU is saturated.
* The appropriate number of threads varies heavily between applications.
* Async application code generally does not need more threads than CPU cores.
*/
ExecutorService executor = Executors.newFixedThreadPool(2);
server = Grpc.newServerBuilderForPort(port, InsecureServerCredentials.create())
.executor(executor)
.addService(new GreeterImpl())
.build()
.start();
Expand All @@ -48,7 +60,12 @@ public void run() {
try {
HelloWorldServer.this.stop();
} catch (InterruptedException e) {
if (server != null) {
server.shutdownNow();
}
e.printStackTrace(System.err);
} finally {
executor.shutdown();
}
System.err.println("*** server shut down");
}
Expand Down