ManagedChannel is the gRPC Java class that represents a client-side connection to a server. It owns the underlying HTTP/2 connections, name resolution, load balancing and retries, and it is the object your generated client stubs use to send every remote call. You create one channel per target service, share it across threads, and shut it down when the application stops.
ManagedChannel extends the abstract io.grpc.Channel class. Channel only knows how to start a call; ManagedChannel adds the lifecycle methods (shutdown(), shutdownNow(), awaitTermination(), isShutdown(), isTerminated()) that make it your job to release the resources.
A channel hides the details of talking to a remote service. Behind a single ManagedChannel object gRPC handles:
dns:/// or xds:/// URI, or an in-process name) to one or more addresses.pick_first by default, round_robin when configured).Because the channel is expensive to create and cheap to use, the gRPC guidance is to create it once and reuse it for the lifetime of the client. Do not create a channel per request.
You normally build a channel with ManagedChannelBuilder. In production code, use TLS. usePlaintext() is meant for local development and tests.
GreeterGrpc, HelloRequest and HelloReply are classes generated from a .proto file by the protobuf compiler plugin. The stub receives the channel in its factory method, so every call made through that stub travels over this channel.
For TLS, use Grpc.newChannelBuilder(target, TlsChannelCredentials.create()), which is the credentials-based API. It replaces the older pattern of calling useTransportSecurity() on a transport-specific builder such as NettyChannelBuilder.
Every generated client stub wraps a Channel. gRPC Java generates three stub styles, and all of them can share the same channel:
| Stub | Created with | Behaviour |
|---|---|---|
| Blocking | newBlockingStub(channel) | Waits for the response on the calling thread |
| Async | newStub(channel) | Uses StreamObserver callbacks, supports streaming |
| Future | newFutureStub(channel) | Returns ListenableFuture, unary calls only |
Stubs are lightweight and immutable. Methods such as withDeadlineAfter(), withInterceptors() and withCompression() return a new stub that still uses the same channel, so you can customise a call without touching the connection.
A channel holds threads, sockets and, for Netty, native resources. If you drop the reference without shutting it down, gRPC logs an "ManagedChannel was not shutdown properly" warning and the resources leak until garbage collection.
shutdown() to stop accepting new calls while letting in-flight calls finish.awaitTermination(timeout, unit) to wait for them.shutdownNow() to cancel the remaining calls.The GreeterClient.close() method above follows exactly this sequence. In a Spring Boot application, expose the channel as a bean and set a destroy method so the container performs the shutdown for you.
A channel is lazy. After build() it does not connect until the first call (or until you call getState(true)). It then moves through the states IDLE, CONNECTING, READY, TRANSIENT_FAILURE and SHUTDOWN. By default a channel with no active calls goes back to idle after 30 minutes, which you can change with idleTimeout().
You can observe transitions with channel.notifyWhenStateChanged(state, runnable). Most applications do not need this and rely on per-call deadlines and the retry policy instead. If a server is unreachable, a call fails with Status.UNAVAILABLE unless you opt in to waitForReady(), which makes the call wait for the channel to become ready.
For unit tests you do not need a network. The grpc-inprocess artifact provides InProcessChannelBuilder and InProcessServerBuilder, which connect a client and server inside the same JVM through a named endpoint. grpc-testing adds GrpcCleanupRule and, in newer versions, GrpcCleanupExtension for JUnit 5, which shuts channels and servers down after each test.
No. A channel is a long-lived, thread-safe object that multiplexes many calls over a few connections. Create it once per target service and reuse it.
Yes. You can share one channel, and any stubs built on it, between many threads without extra synchronisation.
Channel is the abstract type that stubs and interceptors work with. ManagedChannel extends it with lifecycle control, so the code that creates the channel can shut it down.
The channel was garbage collected before shutdown() was called. Close it explicitly when the owning component stops.
ManagedChannel is the client's long-lived connection manager in gRPC Java. Build it once with ManagedChannelBuilder or Grpc.newChannelBuilder, pass it to blocking, async or future stubs, configure deadlines and keep-alive as needed, and always shut it down with shutdown() and awaitTermination().