本文整理汇总了Java中io.grpc.Metadata.Key方法的典型用法代码示例。如果您正苦于以下问题:Java Metadata.Key方法的具体用法?Java Metadata.Key怎么用?Java Metadata.Key使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.grpc.Metadata
的用法示例。
在下文中一共展示了Metadata.Key方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: metadataInterceptor
import io.grpc.Metadata; //导入方法依赖的package包/类
private ClientInterceptor metadataInterceptor() {
ClientInterceptor interceptor = new ClientInterceptor() {
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(
final io.grpc.MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, final Channel next) {
return new ClientInterceptors.CheckedForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {
@Override
protected void checkedStart(Listener<RespT> responseListener, Metadata headers)
throws StatusException {
for (ConfigProto.CallMetadataEntry entry : callConfiguration.getMetadataList()) {
Metadata.Key<String> key = Metadata.Key.of(entry.getName(), Metadata.ASCII_STRING_MARSHALLER);
headers.put(key, entry.getValue());
}
delegate().start(responseListener, headers);
}
};
}
};
return interceptor;
}
示例2: echoRequestHeadersInterceptor
import io.grpc.Metadata; //导入方法依赖的package包/类
/**
* Echo the request headers from a client into response headers and trailers. Useful for
* testing end-to-end metadata propagation.
*/
private static ServerInterceptor echoRequestHeadersInterceptor(final Metadata.Key<?>... keys) {
final Set<Metadata.Key<?>> keySet = new HashSet<Metadata.Key<?>>(Arrays.asList(keys));
return new ServerInterceptor() {
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
ServerCall<ReqT, RespT> call,
final Metadata requestHeaders,
ServerCallHandler<ReqT, RespT> next) {
return next.startCall(new SimpleForwardingServerCall<ReqT, RespT>(call) {
@Override
public void sendHeaders(Metadata responseHeaders) {
responseHeaders.merge(requestHeaders, keySet);
super.sendHeaders(responseHeaders);
}
@Override
public void close(Status status, Metadata trailers) {
trailers.merge(requestHeaders, keySet);
super.close(status, trailers);
}
}, requestHeaders);
}
};
}
示例3: interceptorShouldFreezeContext
import io.grpc.Metadata; //导入方法依赖的package包/类
@Test
public void interceptorShouldFreezeContext() {
TestService svc = new TestService();
// Plumbing
serverRule.getServiceRegistry().addService(ServerInterceptors.interceptForward(svc,
new AmbientContextServerInterceptor("ctx-"),
new AmbientContextFreezeServerInterceptor()));
GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc
.newBlockingStub(serverRule.getChannel())
.withInterceptors(new AmbientContextClientInterceptor("ctx-"));
// Test
Metadata.Key<String> key = Metadata.Key.of("ctx-k", Metadata.ASCII_STRING_MARSHALLER);
AmbientContext.initialize(Context.current()).run(() -> {
AmbientContext.current().put(key, "value");
stub.sayHello(HelloRequest.newBuilder().setName("World").build());
});
assertThat(svc.frozen).isTrue();
}
示例4: beforeMethod
import io.grpc.Metadata; //导入方法依赖的package包/类
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
MethodInterceptResult result) throws Throwable {
final Metadata headers = (Metadata)allArguments[1];
final ContextCarrier contextCarrier = new ContextCarrier();
ContextManager.inject(contextCarrier);
CarrierItem contextItem = contextCarrier.items();
while (contextItem.hasNext()) {
contextItem = contextItem.next();
Metadata.Key<String> headerKey = Metadata.Key.of(contextItem.getHeadKey(), Metadata.ASCII_STRING_MARSHALLER);
headers.put(headerKey, contextItem.getHeadValue());
}
GRPCDynamicFields cachedObjects = (GRPCDynamicFields)objInst.getSkyWalkingDynamicField();
GRPCDynamicFields listenerCachedObject = new GRPCDynamicFields();
listenerCachedObject.setSnapshot(ContextManager.capture());
listenerCachedObject.setDescriptor(cachedObjects.getDescriptor());
((EnhancedInstance)allArguments[0]).setSkyWalkingDynamicField(listenerCachedObject);
}
示例5: echoRequestMetadataInHeaders
import io.grpc.Metadata; //导入方法依赖的package包/类
/**
* Echoes request headers with the specified key(s) from a client into response headers only.
*/
private static ServerInterceptor echoRequestMetadataInHeaders(final Metadata.Key<?>... keys) {
final Set<Metadata.Key<?>> keySet = new HashSet<Metadata.Key<?>>(Arrays.asList(keys));
return new ServerInterceptor() {
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
ServerCall<ReqT, RespT> call,
final Metadata requestHeaders,
ServerCallHandler<ReqT, RespT> next) {
return next.startCall(new SimpleForwardingServerCall<ReqT, RespT>(call) {
@Override
public void sendHeaders(Metadata responseHeaders) {
responseHeaders.merge(requestHeaders, keySet);
super.sendHeaders(responseHeaders);
}
@Override
public void close(Status status, Metadata trailers) {
super.close(status, trailers);
}
}, requestHeaders);
}
};
}
示例6: echoRequestMetadataInTrailers
import io.grpc.Metadata; //导入方法依赖的package包/类
/**
* Echoes request headers with the specified key(s) from a client into response trailers only.
*/
private static ServerInterceptor echoRequestMetadataInTrailers(final Metadata.Key<?>... keys) {
final Set<Metadata.Key<?>> keySet = new HashSet<Metadata.Key<?>>(Arrays.asList(keys));
return new ServerInterceptor() {
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
ServerCall<ReqT, RespT> call,
final Metadata requestHeaders,
ServerCallHandler<ReqT, RespT> next) {
return next.startCall(new SimpleForwardingServerCall<ReqT, RespT>(call) {
@Override
public void sendHeaders(Metadata responseHeaders) {
super.sendHeaders(responseHeaders);
}
@Override
public void close(Status status, Metadata trailers) {
trailers.merge(requestHeaders, keySet);
super.close(status, trailers);
}
}, requestHeaders);
}
};
}
示例7: statsHeaderMalformed
import io.grpc.Metadata; //导入方法依赖的package包/类
@Test
public void statsHeaderMalformed() {
// Construct a malformed header and make sure parsing it will throw
byte[] statsHeaderValue = new byte[]{1};
Metadata.Key<byte[]> arbitraryStatsHeader =
Metadata.Key.of("grpc-tags-bin", Metadata.BINARY_BYTE_MARSHALLER);
try {
tagCtxSerializer.fromByteArray(statsHeaderValue);
fail("Should have thrown");
} catch (Exception e) {
// Expected
}
// But the header key will return a default context for it
Metadata headers = new Metadata();
assertNull(headers.get(censusStats.statsHeader));
headers.put(arbitraryStatsHeader, statsHeaderValue);
assertSame(tagger.empty(), headers.get(censusStats.statsHeader));
}
示例8: toHeaders
import io.grpc.Metadata; //导入方法依赖的package包/类
private static Metadata toHeaders(Map<String, List<String>> metadata) {
Metadata headers = new Metadata();
if (metadata != null) {
for (String key : metadata.keySet()) {
Metadata.Key<String> headerKey = Metadata.Key.of(
key, Metadata.ASCII_STRING_MARSHALLER);
for (String value : metadata.get(key)) {
headers.put(headerKey, value);
}
}
}
return headers;
}
示例9: contextTransfersOneHopSync
import io.grpc.Metadata; //导入方法依赖的package包/类
@Test
public void contextTransfersOneHopSync() throws Exception {
Metadata.Key<String> ctxKey = Metadata.Key.of("ctx-context-key", Metadata.ASCII_STRING_MARSHALLER);
String expectedCtxValue = "context-value";
AtomicReference<String> ctxValue = new AtomicReference<>();
// Service
GreeterGrpc.GreeterImplBase svc = new GreeterGrpc.GreeterImplBase() {
@Override
public void sayHello(HelloRequest request, StreamObserver<HelloResponse> responseObserver) {
ctxValue.set(AmbientContext.current().get(ctxKey));
responseObserver.onNext(HelloResponse.newBuilder().setMessage("Hello " + request.getName()).build());
responseObserver.onCompleted();
}
};
// Plumbing
serverRule1.getServiceRegistry().addService(ServerInterceptors
.intercept(svc, new AmbientContextServerInterceptor("ctx-")));
GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc
.newBlockingStub(serverRule1.getChannel())
.withInterceptors(new AmbientContextClientInterceptor("ctx-"));
// Test
AmbientContext.initialize(Context.current()).run(() -> {
AmbientContext.current().put(ctxKey, expectedCtxValue);
stub.sayHello(HelloRequest.newBuilder().setName("world").build());
});
assertThat(ctxValue.get()).isEqualTo(expectedCtxValue);
}
示例10: multiValueContextTransfers
import io.grpc.Metadata; //导入方法依赖的package包/类
@Test
public void multiValueContextTransfers() throws Exception {
Metadata.Key<String> ctxKey = Metadata.Key.of("ctx-context-key", Metadata.ASCII_STRING_MARSHALLER);
String expectedCtxValue1 = "context-value1";
String expectedCtxValue2 = "context-value2";
String expectedCtxValue3 = "context-value3";
AtomicReference<Iterable<String>> ctxValue = new AtomicReference<>();
// Service
GreeterGrpc.GreeterImplBase svc = new GreeterGrpc.GreeterImplBase() {
@Override
public void sayHello(HelloRequest request, StreamObserver<HelloResponse> responseObserver) {
ctxValue.set(AmbientContext.current().getAll(ctxKey));
responseObserver.onNext(HelloResponse.newBuilder().setMessage("Hello " + request.getName()).build());
responseObserver.onCompleted();
}
};
// Plumbing
serverRule1.getServiceRegistry().addService(ServerInterceptors
.intercept(svc, new AmbientContextServerInterceptor("ctx-")));
GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc
.newBlockingStub(serverRule1.getChannel())
.withInterceptors(new AmbientContextClientInterceptor("ctx-"));
// Test
AmbientContext.initialize(Context.current()).run(() -> {
AmbientContext.current().put(ctxKey, expectedCtxValue1);
AmbientContext.current().put(ctxKey, expectedCtxValue2);
AmbientContext.current().put(ctxKey, expectedCtxValue3);
stub.sayHello(HelloRequest.newBuilder().setName("world").build());
});
assertThat(ctxValue.get()).containsExactlyInAnyOrder(expectedCtxValue1, expectedCtxValue2, expectedCtxValue3);
}
示例11: contextTransfersOneHopAsync
import io.grpc.Metadata; //导入方法依赖的package包/类
@Test
public void contextTransfersOneHopAsync() throws Exception {
Metadata.Key<String> ctxKey = Metadata.Key.of("ctx-context-key", Metadata.ASCII_STRING_MARSHALLER);
String expectedCtxValue = "context-value";
AtomicReference<String> ctxValue = new AtomicReference<>();
// Service
GreeterGrpc.GreeterImplBase svc = new GreeterGrpc.GreeterImplBase() {
@Override
public void sayHello(HelloRequest request, StreamObserver<HelloResponse> responseObserver) {
ctxValue.set(AmbientContext.current().get(ctxKey));
responseObserver.onNext(HelloResponse.newBuilder().setMessage("Hello " + request.getName()).build());
responseObserver.onCompleted();
}
};
// Plumbing
serverRule1.getServiceRegistry().addService(ServerInterceptors
.intercept(svc, new AmbientContextServerInterceptor("ctx-")));
GreeterGrpc.GreeterFutureStub stub = GreeterGrpc
.newFutureStub(serverRule1.getChannel())
.withInterceptors(new AmbientContextClientInterceptor("ctx-"));
// Test
AmbientContext.initialize(Context.current()).run(() -> {
AmbientContext.current().put(ctxKey, expectedCtxValue);
ListenableFuture<HelloResponse> futureResponse = stub.sayHello(HelloRequest.newBuilder().setName("world").build());
// Verify response callbacks still have context
MoreFutures.onSuccess(
futureResponse,
response -> assertThat(AmbientContext.current().get(ctxKey)).isEqualTo(expectedCtxValue),
Context.currentContextExecutor(Executors.newSingleThreadExecutor()));
await().atMost(Duration.ONE_SECOND).until(futureResponse::isDone);
});
assertThat(ctxValue.get()).isEqualTo(expectedCtxValue);
}
示例12: multipleContextTransfersOneHopSync
import io.grpc.Metadata; //导入方法依赖的package包/类
@Test
public void multipleContextTransfersOneHopSync() throws Exception {
Metadata.Key<String> ctxKey = Metadata.Key.of("ctx-context-key", Metadata.ASCII_STRING_MARSHALLER);
Metadata.Key<String> l5dKey = Metadata.Key.of("l5d-context-key", Metadata.ASCII_STRING_MARSHALLER);
String expectedCtxValue = "context-value";
AtomicReference<String> ctxValue = new AtomicReference<>();
AtomicReference<String> l5dValue = new AtomicReference<>();
// Service
GreeterGrpc.GreeterImplBase svc = new GreeterGrpc.GreeterImplBase() {
@Override
public void sayHello(HelloRequest request, StreamObserver<HelloResponse> responseObserver) {
ctxValue.set(AmbientContext.current().get(ctxKey));
l5dValue.set(AmbientContext.current().get(l5dKey));
responseObserver.onNext(HelloResponse.newBuilder().setMessage("Hello " + request.getName()).build());
responseObserver.onCompleted();
}
};
// Plumbing
serverRule1.getServiceRegistry().addService(ServerInterceptors.intercept(svc,
new AmbientContextServerInterceptor("ctx-"),
new AmbientContextServerInterceptor("l5d-")));
GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc
.newBlockingStub(serverRule1.getChannel())
.withInterceptors(
new AmbientContextClientInterceptor("ctx-"),
new AmbientContextClientInterceptor("l5d-"));
// Test
AmbientContext.initialize(Context.current()).run(() -> {
AmbientContext.current().put(ctxKey, expectedCtxValue);
AmbientContext.current().put(l5dKey, expectedCtxValue);
stub.sayHello(HelloRequest.newBuilder().setName("world").build());
});
assertThat(ctxValue.get()).isEqualTo(expectedCtxValue);
assertThat(l5dValue.get()).isEqualTo(expectedCtxValue);
}
示例13: binaryContextValueTransfers
import io.grpc.Metadata; //导入方法依赖的package包/类
@Test
public void binaryContextValueTransfers() throws Exception {
Metadata.Key<byte[]> ctxKey = Metadata.Key.of(
"ctx-context-key" + Metadata.BINARY_HEADER_SUFFIX,
Metadata.BINARY_BYTE_MARSHALLER);
byte[] expectedCtxValue = BaseEncoding.base16().decode("DEADBEEF");
AtomicReference<byte[]> ctxValue = new AtomicReference<>();
// Service
GreeterGrpc.GreeterImplBase svc = new GreeterGrpc.GreeterImplBase() {
@Override
public void sayHello(HelloRequest request, StreamObserver<HelloResponse> responseObserver) {
ctxValue.set(AmbientContext.current().get(ctxKey));
responseObserver.onNext(HelloResponse.newBuilder().setMessage("Hello " + request.getName()).build());
responseObserver.onCompleted();
}
};
// Plumbing
serverRule.getServiceRegistry().addService(ServerInterceptors
.intercept(svc, new AmbientContextServerInterceptor("ctx-")));
GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc
.newBlockingStub(serverRule.getChannel())
.withInterceptors(new AmbientContextClientInterceptor("ctx-"));
// Test
AmbientContext.initialize(Context.current()).run(() -> {
AmbientContext.current().put(ctxKey, expectedCtxValue);
stub.sayHello(HelloRequest.newBuilder().setName("world").build());
});
assertThat(ctxValue.get()).containsExactly(expectedCtxValue);
}
示例14: contextFreezingWorks
import io.grpc.Metadata; //导入方法依赖的package包/类
@Test
public void contextFreezingWorks() {
Metadata.Key<String> key = Metadata.Key.of("key", Metadata.ASCII_STRING_MARSHALLER);
AmbientContext context = new AmbientContext();
assertThat(context.isFrozen()).isFalse();
Object freezeKey = context.freeze();
assertThat(context.isFrozen()).isTrue();
assertThatThrownBy(() -> context.put(key, "foo")).isInstanceOf(IllegalStateException.class);
}
示例15: contextScopeStackingWorks
import io.grpc.Metadata; //导入方法依赖的package包/类
@Test
public void contextScopeStackingWorks() {
Metadata.Key<String> key = Metadata.Key.of("k", Metadata.ASCII_STRING_MARSHALLER);
AmbientContext.initialize(Context.current()).run(() -> {
AmbientContext.current().put(key, "outer");
assertThat(AmbientContext.current().get(key)).isEqualTo("outer");
AmbientContext.current().fork(Context.current()).run(() -> {
AmbientContext.current().put(key, "inner");
assertThat(AmbientContext.current().get(key)).isEqualTo("inner");
});
assertThat(AmbientContext.current().get(key)).isEqualTo("outer");
});
}