下面列出了怎么用io.grpc.ExperimentalApi的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Set ciphers and APN appropriate for gRPC. Precisely what is set is permitted to change, so if
* an application requires particular settings it should override the options set here.
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1784")
@CanIgnoreReturnValue
public static SslContextBuilder configure(SslContextBuilder builder, SslProvider provider) {
switch (provider) {
case JDK:
{
Provider jdkProvider = findJdkProvider();
if (jdkProvider == null) {
throw new IllegalArgumentException(
"Could not find Jetty NPN/ALPN or Conscrypt as installed JDK providers");
}
return configure(builder, jdkProvider);
}
case OPENSSL:
{
ApplicationProtocolConfig apc;
if (OpenSsl.isAlpnSupported()) {
apc = NPN_AND_ALPN;
} else {
apc = NPN;
}
return builder
.sslProvider(SslProvider.OPENSSL)
.ciphers(Http2SecurityUtil.CIPHERS, SupportedCipherSuiteFilter.INSTANCE)
.applicationProtocolConfig(apc);
}
default:
throw new IllegalArgumentException("Unsupported provider: " + provider);
}
}
/**
* Creates a new method descriptor that always creates zero length messages, and always parses to
* null objects.
*
* @since 1.1.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/2600")
public static MethodDescriptor<Void, Void> voidMethod() {
return MethodDescriptor.<Void, Void>newBuilder()
.setType(MethodType.UNARY)
.setFullMethodName(MethodDescriptor.generateFullMethodName("service_foo", "method_bar"))
.setRequestMarshaller(TestMethodDescriptors.voidMarshaller())
.setResponseMarshaller(TestMethodDescriptors.voidMarshaller())
.build();
}
/**
* Set static application-specific metadata that is associated with the node being instantiated.
* This may include tags like "role":"frontend".
*
* @param metadata A map specifying a set of key-value tags.
*/
@ExperimentalApi
public Builder setMetadata(final Map<String, ByteString> metadata) {
Objects.requireNonNull(metadata);
this.metadata = Metadata.newBuilder().putAllMetadata(metadata).build();
return this;
}
/**
* Set a link failure detector to use for observers to watch their subjects.
*
* @param edgeFailureDetector A link failure detector used as input for Rapid's failure detection.
*/
@ExperimentalApi
public Builder setEdgeFailureDetectorFactory(final IEdgeFailureDetectorFactory edgeFailureDetector) {
Objects.requireNonNull(edgeFailureDetector);
this.edgeFailureDetector = edgeFailureDetector;
return this;
}
/**
* This is used to register subscriptions for different events
*/
@ExperimentalApi
public Builder addSubscription(final ClusterEvents event,
final BiConsumer<Long, List<NodeStatusChange>> callback) {
this.subscriptions.computeIfAbsent(event, k -> new ArrayList<>());
this.subscriptions.get(event).add(callback);
return this;
}
/**
* Set ciphers and APN appropriate for gRPC. Precisely what is set is permitted to change, so if
* an application requires particular settings it should override the options set here.
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1784")
@CanIgnoreReturnValue
public static SslContextBuilder configure(SslContextBuilder builder, SslProvider provider) {
switch (provider) {
case JDK:
{
Provider jdkProvider = findJdkProvider();
if (jdkProvider == null) {
throw new IllegalArgumentException(
"Could not find Jetty NPN/ALPN or Conscrypt as installed JDK providers");
}
return configure(builder, jdkProvider);
}
case OPENSSL:
{
ApplicationProtocolConfig apc;
if (OpenSsl.isAlpnSupported()) {
apc = NPN_AND_ALPN;
} else {
apc = NPN;
}
return builder
.sslProvider(SslProvider.OPENSSL)
.ciphers(Http2SecurityUtil.CIPHERS, SupportedCipherSuiteFilter.INSTANCE)
.applicationProtocolConfig(apc);
}
default:
throw new IllegalArgumentException("Unsupported provider: " + provider);
}
}
/**
* Creates a new method descriptor that always creates zero length messages, and always parses to
* null objects.
*
* @since 1.1.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/2600")
public static MethodDescriptor<Void, Void> voidMethod() {
return MethodDescriptor.<Void, Void>newBuilder()
.setType(MethodType.UNARY)
.setFullMethodName(MethodDescriptor.generateFullMethodName("service_foo", "method_bar"))
.setRequestMarshaller(TestMethodDescriptors.voidMarshaller())
.setResponseMarshaller(TestMethodDescriptors.voidMarshaller())
.build();
}
/**
* Note: This does not necessarily return a consistent view of the map.
*/
@Override
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/2222")
public List<ServerServiceDefinition> getServices() {
return Collections.unmodifiableList(new ArrayList<>(services.values()));
}
/**
* Creates a new builder, which delegates to the given ManagedChannelBuilder.
*
* @deprecated Use {@link #usingBuilder(ManagedChannelBuilder)} instead.
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/6043")
@Deprecated
public static AndroidChannelBuilder fromBuilder(ManagedChannelBuilder<?> builder) {
return usingBuilder(builder);
}
/**
* Note: This does not necessarily return a consistent view of the map.
*/
@Override
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/2222")
public List<ServerServiceDefinition> getServices() {
return Collections.unmodifiableList(new ArrayList<>(services.values()));
}
/**
* Captures the last received metadata for a stub. Useful for testing
*
* @param stub to capture for
* @param headersCapture to record the last received headers
* @param trailersCapture to record the last received trailers
* @return an implementation of the stub that allows to access the last received call's
* headers and trailers via {@code headersCapture} and {@code trailersCapture}.
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1789")
public static <T extends AbstractStub<T>> T captureMetadata(
T stub,
AtomicReference<Metadata> headersCapture,
AtomicReference<Metadata> trailersCapture) {
return stub.withInterceptors(
newCaptureMetadataInterceptor(headersCapture, trailersCapture));
}
/**
* Captures the last received metadata for a stub. Useful for testing
*
* @param stub to capture for
* @param headersCapture to record the last received headers
* @param trailersCapture to record the last received trailers
* @return an implementation of the stub that allows to access the last received call's
* headers and trailers via {@code headersCapture} and {@code trailersCapture}.
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1789")
public static <T extends AbstractStub<T>> T captureMetadata(
T stub,
AtomicReference<Metadata> headersCapture,
AtomicReference<Metadata> trailersCapture) {
return stub.withInterceptors(
newCaptureMetadataInterceptor(headersCapture, trailersCapture));
}
/**
* Sets the global registry for proto marshalling shared across all servers and clients.
*
* <p>Warning: This API will likely change over time. It is not possible to have separate
* registries per Process, Server, Channel, Service, or Method. This is intentional until there
* is a more appropriate API to set them.
*
* <p>Warning: Do NOT modify the extension registry after setting it. It is thread safe to call
* {@link #setExtensionRegistry}, but not to modify the underlying object.
*
* <p>If you need custom parsing behavior for protos, you will need to make your own
* {@code MethodDescriptor.Marshaller} for the time being.
*
* @since 1.16.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1787")
public static void setExtensionRegistry(ExtensionRegistry registry) {
ProtoLiteUtils.setExtensionRegistry(registry);
}
/**
* Produce a metadata marshaller for a protobuf type.
*
* @since 1.13.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/4477")
public static <T extends Message> Metadata.BinaryMarshaller<T> metadataMarshaller(T instance) {
return ProtoLiteUtils.metadataMarshaller(instance);
}
/**
* Sets the global registry for proto marshalling shared across all servers and clients.
*
* <p>Warning: This API will likely change over time. It is not possible to have separate
* registries per Process, Server, Channel, Service, or Method. This is intentional until there
* is a more appropriate API to set them.
*
* <p>Warning: Do NOT modify the extension registry after setting it. It is thread safe to call
* {@link #setExtensionRegistry}, but not to modify the underlying object.
*
* <p>If you need custom parsing behavior for protos, you will need to make your own
* {@code MethodDescriptor.Marshaller} for the time being.
*
* @since 1.0.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1787")
public static void setExtensionRegistry(ExtensionRegistryLite newRegistry) {
globalRegistry = checkNotNull(newRegistry, "newRegistry");
}
/**
* Creates a new marshaller that does nothing.
*
* @since 1.1.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/2600")
public static MethodDescriptor.Marshaller<Void> voidMarshaller() {
return new NoopMarshaller();
}
/**
* Attaches a set of request headers to a stub.
*
* @param stub to bind the headers to.
* @param extraHeaders the headers to be passed by each call on the returned stub.
* @return an implementation of the stub with {@code extraHeaders} bound to each call.
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1789")
public static <T extends AbstractStub<T>> T attachHeaders(T stub, Metadata extraHeaders) {
return stub.withInterceptors(newAttachHeadersInterceptor(extraHeaders));
}
/**
* Set's the compressor name to use for the call. It is the responsibility of the application
* to make sure the server supports decoding the compressor picked by the client. To be clear,
* this is the compressor used by the stub to compress messages to the server. To get
* compressed responses from the server, set the appropriate {@link io.grpc.DecompressorRegistry}
* on the {@link io.grpc.ManagedChannelBuilder}.
*
* @since 1.0.0
* @param compressorName the name (e.g. "gzip") of the compressor to use.
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1704")
public final S withCompression(String compressorName) {
return build(channel, callOptions.withCompression(compressorName));
}
/**
* Sets a custom option to be passed to client interceptors on the channel
* {@link io.grpc.ClientInterceptor} via the CallOptions parameter.
*
* @since 1.0.0
* @param key the option being set
* @param value the value for the key
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1869")
public final <T> S withOption(CallOptions.Key<T> key, T value) {
return build(channel, callOptions.withOption(key, value));
}
/**
* Returns a new stub that limits the maximum acceptable message size from a remote peer.
*
* <p>If unset, the {@link ManagedChannelBuilder#maxInboundMessageSize(int)} limit is used.
*
* @since 1.1.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/2563")
public final S withMaxInboundMessageSize(int maxSize) {
return build(channel, callOptions.withMaxInboundMessageSize(maxSize));
}
/**
* Returns a new stub that limits the maximum acceptable message size to send a remote peer.
*
* @since 1.1.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/2563")
public final S withMaxOutboundMessageSize(int maxSize) {
return build(channel, callOptions.withMaxOutboundMessageSize(maxSize));
}
/**
* Sets the global registry for proto marshalling shared across all servers and clients.
*
* <p>Warning: This API will likely change over time. It is not possible to have separate
* registries per Process, Server, Channel, Service, or Method. This is intentional until there
* is a more appropriate API to set them.
*
* <p>Warning: Do NOT modify the extension registry after setting it. It is thread safe to call
* {@link #setExtensionRegistry}, but not to modify the underlying object.
*
* <p>If you need custom parsing behavior for protos, you will need to make your own
* {@code MethodDescriptor.Marshaller} for the time being.
*
* @since 1.16.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1787")
public static void setExtensionRegistry(ExtensionRegistry registry) {
ProtoLiteUtils.setExtensionRegistry(registry);
}
/**
* Produce a metadata marshaller for a protobuf type.
*
* @since 1.13.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/4477")
public static <T extends Message> Metadata.BinaryMarshaller<T> metadataMarshaller(T instance) {
return ProtoLiteUtils.metadataMarshaller(instance);
}
/**
* Sets the global registry for proto marshalling shared across all servers and clients.
*
* <p>Warning: This API will likely change over time. It is not possible to have separate
* registries per Process, Server, Channel, Service, or Method. This is intentional until there
* is a more appropriate API to set them.
*
* <p>Warning: Do NOT modify the extension registry after setting it. It is thread safe to call
* {@link #setExtensionRegistry}, but not to modify the underlying object.
*
* <p>If you need custom parsing behavior for protos, you will need to make your own
* {@code MethodDescriptor.Marshaller} for the time being.
*
* @since 1.0.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1787")
public static void setExtensionRegistry(ExtensionRegistryLite newRegistry) {
globalRegistry = checkNotNull(newRegistry, "newRegistry");
}
/**
* Creates a new marshaller that does nothing.
*
* @since 1.1.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/2600")
public static MethodDescriptor.Marshaller<Void> voidMarshaller() {
return new NoopMarshaller();
}
/**
* Attaches a set of request headers to a stub.
*
* @param stub to bind the headers to.
* @param extraHeaders the headers to be passed by each call on the returned stub.
* @return an implementation of the stub with {@code extraHeaders} bound to each call.
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1789")
public static <T extends AbstractStub<T>> T attachHeaders(T stub, Metadata extraHeaders) {
return stub.withInterceptors(newAttachHeadersInterceptor(extraHeaders));
}
/**
* Set's the compressor name to use for the call. It is the responsibility of the application
* to make sure the server supports decoding the compressor picked by the client. To be clear,
* this is the compressor used by the stub to compress messages to the server. To get
* compressed responses from the server, set the appropriate {@link io.grpc.DecompressorRegistry}
* on the {@link io.grpc.ManagedChannelBuilder}.
*
* @since 1.0.0
* @param compressorName the name (e.g. "gzip") of the compressor to use.
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1704")
public final S withCompression(String compressorName) {
return build(channel, callOptions.withCompression(compressorName));
}
/**
* Sets a custom option to be passed to client interceptors on the channel
* {@link io.grpc.ClientInterceptor} via the CallOptions parameter.
*
* @since 1.0.0
* @param key the option being set
* @param value the value for the key
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1869")
public final <T> S withOption(CallOptions.Key<T> key, T value) {
return build(channel, callOptions.withOption(key, value));
}
/**
* Returns a new stub that limits the maximum acceptable message size from a remote peer.
*
* <p>If unset, the {@link ManagedChannelBuilder#maxInboundMessageSize(int)} limit is used.
*
* @since 1.1.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/2563")
public final S withMaxInboundMessageSize(int maxSize) {
return build(channel, callOptions.withMaxInboundMessageSize(maxSize));
}
/**
* Returns a new stub that limits the maximum acceptable message size to send a remote peer.
*
* @since 1.1.0
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/2563")
public final S withMaxOutboundMessageSize(int maxSize) {
return build(channel, callOptions.withMaxOutboundMessageSize(maxSize));
}