High-performance in-process transport implementation for gRPC Java that enables direct communication between client and server within the same JVM process
—
The gRPC In-Process Transport provides two types of socket addresses for identifying servers: named addresses using InProcessSocketAddress and anonymous addresses using AnonymousInProcessSocketAddress. These addresses enable different patterns for server identification and client connection.
A socket address that identifies an in-process server by name. Multiple clients can connect to the same named server.
/**
* Custom SocketAddress class for InProcessTransport.
*/
public final class InProcessSocketAddress extends SocketAddress {
/**
* Construct an address for a server identified by name.
* @param name The name of the inprocess server
*/
public InProcessSocketAddress(String name);
/**
* Gets the name of the inprocess server.
* @return the server name
*/
public String getName();
/**
* Returns the server name.
* @return the server name
*/
public String toString();
/**
* Hash code based on server name.
* @return hash code
*/
public int hashCode();
/**
* Returns true if the object is of the same type and server names match.
* @param obj the object to compare
* @return true if equal
*/
public boolean equals(Object obj);
}Usage Examples:
// Create named address
InProcessSocketAddress address = new InProcessSocketAddress("user-service");
// Get server name
String serverName = address.getName(); // Returns "user-service"
// Use with builders
InProcessServerBuilder serverBuilder =
InProcessServerBuilder.forAddress(address);
InProcessChannelBuilder channelBuilder =
InProcessChannelBuilder.forAddress(address);
// Addresses with same name are equal
InProcessSocketAddress addr1 = new InProcessSocketAddress("service");
InProcessSocketAddress addr2 = new InProcessSocketAddress("service");
boolean isEqual = addr1.equals(addr2); // Returns trueA socket address for anonymous in-process servers that can only be referenced via the specific address instance. This provides better isolation than named servers.
/**
* Custom SocketAddress class for InProcessTransport, for a server which
* can only be referenced via this address instance.
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/8626")
public final class AnonymousInProcessSocketAddress extends SocketAddress {
/**
* Creates a new AnonymousInProcessSocketAddress.
*/
public AnonymousInProcessSocketAddress();
}Usage Examples:
// Create anonymous address
AnonymousInProcessSocketAddress address = new AnonymousInProcessSocketAddress();
// Create server with anonymous address
Server server = InProcessServerBuilder.forAddress(address)
.addService(new MyServiceImpl())
.build()
.start();
// Create channel using the same address instance
Channel channel = InProcessChannelBuilder.forAddress(address)
.build();
// Only this specific address instance can connect to the server
// Creating a new AnonymousInProcessSocketAddress() will NOT connect to the same server| Feature | InProcessSocketAddress | AnonymousInProcessSocketAddress |
|---|---|---|
| Identification | Named (string-based) | Instance-based |
| Multiple Clients | ✅ Yes | ✅ Yes |
| Server Discovery | By name lookup | By address instance reference |
| Isolation | Shared namespace | Complete isolation |
| Use Case | General purpose, testing with known names | Maximum isolation, advanced testing scenarios |
| Status | Stable | Experimental |
Use InProcessSocketAddress when you need multiple clients to connect to the same server or when you want to use human-readable server names.
String serviceName = "order-service";
InProcessSocketAddress address = new InProcessSocketAddress(serviceName);
// Server setup
Server server = InProcessServerBuilder.forAddress(address)
.addService(new OrderServiceImpl())
.build()
.start();
// Multiple clients can connect using the same name
Channel channel1 = InProcessChannelBuilder.forAddress(address).build();
Channel channel2 = InProcessChannelBuilder.forName(serviceName).build(); // EquivalentUse AnonymousInProcessSocketAddress when you need complete isolation and want to ensure only specific code can access the server.
// Create anonymous address
AnonymousInProcessSocketAddress address = new AnonymousInProcessSocketAddress();
// Server setup - only accessible via this address instance
Server server = InProcessServerBuilder.forAddress(address)
.addService(new SecureServiceImpl())
.build()
.start();
// Only this specific address can connect
Channel channel = InProcessChannelBuilder.forAddress(address).build();
// This would create a DIFFERENT anonymous server (not connected)
// AnonymousInProcessSocketAddress differentAddress = new AnonymousInProcessSocketAddress();Anonymous addresses provide better test isolation:
@Test
void testServiceA() {
AnonymousInProcessSocketAddress address = new AnonymousInProcessSocketAddress();
// This test's server is completely isolated
Server server = InProcessServerBuilder.forAddress(address)
.addService(new ServiceAImpl())
.build()
.start();
Channel channel = InProcessChannelBuilder.forAddress(address).build();
// Test implementation...
}
@Test
void testServiceB() {
AnonymousInProcessSocketAddress address = new AnonymousInProcessSocketAddress();
// This test's server is also completely isolated
Server server = InProcessServerBuilder.forAddress(address)
.addService(new ServiceBImpl())
.build()
.start();
Channel channel = InProcessChannelBuilder.forAddress(address).build();
// Test implementation...
}NullPointerException - Thrown if name parameter is null in constructorIOException - Can be thrown during server registration if address is already in useBoth address types are thread-safe:
InProcessSocketAddress - All methods are thread-safe. Multiple threads can safely access the same address instance concurrently.AnonymousInProcessSocketAddress - Internally synchronized for thread-safe server registration and access. Safe for concurrent use across multiple threads.InProcessSocketAddress uses string-based lookups in a concurrent mapAnonymousInProcessSocketAddress uses direct object references for faster accessInstall with Tessl CLI
npx tessl i tessl/maven-io-grpc--grpc-inprocess