use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ManagedChannel in project toolkit by googleapis.
the class Pubsub method grpc.
private static void grpc(final Settings settings) throws Exception {
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
ManagedChannel channel = NettyChannelBuilder.forTarget(settings.endpoint()).executor(executor).sslContext(GrpcSslContexts.forClient().trustManager(new File(settings.cert())).build()).build();
final Semaphore semaphore = new Semaphore(settings.numWorkers());
Metadata header = new Metadata();
header.put(X_GOOG_HEADER_KEY, X_GOOG_HEADER_VALUE);
final PublisherFutureStub stub = MetadataUtils.attachHeaders(PublisherGrpc.newFutureStub(channel), header);
final AtomicLong resetTime = new AtomicLong();
final AtomicLong numCalls = new AtomicLong();
final AtomicLong numErrs = new AtomicLong();
long endTime = System.nanoTime() + settings.warmDurNano() + settings.targetDurNano();
Thread resetter = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(settings.warmDurNano() / MILLION);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
numCalls.set(0);
numErrs.set(0);
resetTime.set(System.nanoTime());
}
});
resetter.start();
while (System.nanoTime() < endTime) {
semaphore.acquire(1);
Futures.addCallback(stub.getTopic(GetTopicRequest.newBuilder().setTopic(TOPIC_NAME_STRING).build()), new FutureCallback<Topic>() {
@Override
public void onSuccess(Topic topic) {
if (!topic.getName().equals(TOPIC_NAME_STRING)) {
numErrs.incrementAndGet();
}
both();
}
@Override
public void onFailure(Throwable t) {
numErrs.incrementAndGet();
both();
}
void both() {
numCalls.incrementAndGet();
semaphore.release(1);
}
});
}
long nCalls = numCalls.get();
long nErrs = numErrs.get();
long runDurNano = System.nanoTime() - resetTime.get();
System.out.println("errors: " + nErrs);
System.out.println("calls: " + nCalls);
System.out.println("time per call (ns): " + (runDurNano / nCalls));
System.out.println("QPS: " + (nCalls * BILLION / runDurNano));
channel.shutdown().awaitTermination(10, TimeUnit.SECONDS);
executor.shutdown();
executor.awaitTermination(10, TimeUnit.SECONDS);
}
use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ManagedChannel in project vertx-examples by vert-x3.
the class Client method start.
@Override
public void start() throws Exception {
// Create the channel
ManagedChannel channel = VertxChannelBuilder.forAddress(vertx, "localhost", 8080).usePlaintext(true).build();
// Get a stub to use for interacting with the remote service
PingPongServiceGrpc.PingPongServiceVertxStub stub = PingPongServiceGrpc.newVertxStub(channel);
// Make a request
Messages.SimpleRequest request = Messages.SimpleRequest.newBuilder().setFillUsername(true).build();
// Call the remote service
stub.unaryCall(request, ar -> {
if (ar.succeeded()) {
System.out.println("My username is: " + ar.result().getUsername());
} else {
System.out.println("Coult not reach server " + ar.cause().getMessage());
}
});
}
use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ManagedChannel in project vertx-examples by vert-x3.
the class Client method start.
@Override
public void start() throws Exception {
ManagedChannel channel = VertxChannelBuilder.forAddress(vertx, "localhost", 8080).usePlaintext(true).build();
stub = RouteGuideGrpc.newVertxStub(channel);
List<Feature> features = Util.parseFeatures(Util.getDefaultFeaturesFile());
// Looking for a valid feature
getFeature(409146138, -746188906);
// Feature missing.
getFeature(0, 0);
// Looking for features between 40, -75 and 42, -73.
listFeatures(400000000, -750000000, 420000000, -730000000);
// Record a few randomly selected points from the features file.
recordRoute(features, 10);
routeChat();
}
use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ManagedChannel in project vertx-examples by vert-x3.
the class Client method start.
@Override
public void start() throws Exception {
ManagedChannel channel = VertxChannelBuilder.forAddress(vertx, "localhost", 8080).useSsl(options -> options.setSsl(true).setUseAlpn(true).setTrustStoreOptions(new JksOptions().setPath("tls/client-truststore.jks").setPassword("wibble"))).build();
GreeterGrpc.GreeterVertxStub stub = GreeterGrpc.newVertxStub(channel);
HelloRequest request = HelloRequest.newBuilder().setName("Julien").build();
stub.sayHello(request, asyncResponse -> {
if (asyncResponse.succeeded()) {
System.out.println("Succeeded " + asyncResponse.result().getMessage());
} else {
asyncResponse.cause().printStackTrace();
}
});
}
use of org.apache.beam.vendor.grpc.v1p43p2.io.grpc.ManagedChannel in project vertx-examples by vert-x3.
the class Client method start.
@Override
public void start() throws Exception {
// Create the channel
ManagedChannel channel = VertxChannelBuilder.forAddress(vertx, "localhost", 8080).usePlaintext(true).build();
// Get a stub to use for interacting with the remote service
ConsumerServiceGrpc.ConsumerServiceVertxStub stub = ConsumerServiceGrpc.newVertxStub(channel);
// Make a request
Messages.StreamingOutputCallRequest request = Messages.StreamingOutputCallRequest.newBuilder().build();
// Call the remote service
stub.streamingOutputCall(request, stream -> {
stream.handler(response -> {
System.out.println(new String(response.getPayload().toByteArray(), Charset.forName("UTF-8")));
}).endHandler(v -> {
System.out.println("Response has ended.");
});
});
}
Aggregations