Add google java format to gradle.
This commit is contained in:
parent
928b6cc1e5
commit
d663a76f2b
3 changed files with 83 additions and 59 deletions
|
@ -10,12 +10,14 @@ buildscript {
|
|||
}
|
||||
dependencies {
|
||||
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.3'
|
||||
classpath "gradle.plugin.com.github.sherter.google-java-format:google-java-format-gradle-plugin:0.7.1"
|
||||
}
|
||||
}
|
||||
|
||||
apply plugin: 'idea'
|
||||
apply plugin: 'java'
|
||||
apply plugin: 'com.google.protobuf'
|
||||
apply plugin: 'com.github.sherter.google-java-format'
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
|
@ -81,6 +83,10 @@ protobuf {
|
|||
}
|
||||
}
|
||||
|
||||
googleJavaFormat {
|
||||
toolVersion '1.7'
|
||||
}
|
||||
|
||||
// Inform IDEs like IntelliJ IDEA, Eclipse or NetBeans about the generated code.
|
||||
sourceSets {
|
||||
main {
|
||||
|
|
|
@ -16,8 +16,8 @@
|
|||
|
||||
package hipstershop;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.ImmutableListMultimap;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.Iterables;
|
||||
import hipstershop.Demo.Ad;
|
||||
import hipstershop.Demo.AdRequest;
|
||||
|
@ -26,13 +26,13 @@ import io.grpc.Server;
|
|||
import io.grpc.ServerBuilder;
|
||||
import io.grpc.StatusRuntimeException;
|
||||
import io.grpc.health.v1.HealthCheckResponse.ServingStatus;
|
||||
import io.grpc.stub.StreamObserver;
|
||||
import io.grpc.services.*;
|
||||
import io.grpc.stub.StreamObserver;
|
||||
import io.opencensus.common.Duration;
|
||||
import io.opencensus.contrib.grpc.metrics.RpcViews;
|
||||
import io.opencensus.exporter.trace.jaeger.JaegerTraceExporter;
|
||||
import io.opencensus.exporter.stats.stackdriver.StackdriverStatsConfiguration;
|
||||
import io.opencensus.exporter.stats.stackdriver.StackdriverStatsExporter;
|
||||
import io.opencensus.exporter.trace.jaeger.JaegerTraceExporter;
|
||||
import io.opencensus.exporter.trace.stackdriver.StackdriverTraceConfiguration;
|
||||
import io.opencensus.exporter.trace.stackdriver.StackdriverTraceExporter;
|
||||
import io.opencensus.trace.AttributeValue;
|
||||
|
@ -40,14 +40,14 @@ import io.opencensus.trace.Span;
|
|||
import io.opencensus.trace.Tracer;
|
||||
import io.opencensus.trace.Tracing;
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import org.apache.logging.log4j.Level;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
public final class AdService {
|
||||
|
||||
|
@ -64,8 +64,12 @@ public final class AdService {
|
|||
int port = Integer.parseInt(System.getenv("PORT"));
|
||||
healthMgr = new HealthStatusManager();
|
||||
|
||||
server = ServerBuilder.forPort(port).addService(new AdServiceImpl())
|
||||
.addService(healthMgr.getHealthService()).build().start();
|
||||
server =
|
||||
ServerBuilder.forPort(port)
|
||||
.addService(new AdServiceImpl())
|
||||
.addService(healthMgr.getHealthService())
|
||||
.build()
|
||||
.start();
|
||||
logger.info("Ad Service started, listening on " + port);
|
||||
Runtime.getRuntime()
|
||||
.addShutdownHook(
|
||||
|
@ -95,7 +99,7 @@ public final class AdService {
|
|||
*
|
||||
* @param req the request containing context.
|
||||
* @param responseObserver the stream observer which gets notified with the value of {@code
|
||||
* AdResponse}
|
||||
* AdResponse}
|
||||
*/
|
||||
@Override
|
||||
public void getAds(AdRequest req, StreamObserver<AdResponse> responseObserver) {
|
||||
|
@ -157,9 +161,7 @@ public final class AdService {
|
|||
return service;
|
||||
}
|
||||
|
||||
/**
|
||||
* Await termination on the main thread since the grpc library uses daemon threads.
|
||||
*/
|
||||
/** Await termination on the main thread since the grpc library uses daemon threads. */
|
||||
private void blockUntilShutdown() throws InterruptedException {
|
||||
if (server != null) {
|
||||
server.awaitTermination();
|
||||
|
@ -167,20 +169,41 @@ public final class AdService {
|
|||
}
|
||||
|
||||
private static ImmutableListMultimap<String, Ad> createAdsMap() {
|
||||
Ad camera = Ad.newBuilder().setRedirectUrl("/product/2ZYFJ3GM2N")
|
||||
.setText("Film camera for sale. 50% off.").build();
|
||||
Ad lens = Ad.newBuilder().setRedirectUrl("/product/66VCHSJNUP")
|
||||
.setText("Vintage camera lens for sale. 20% off.").build();
|
||||
Ad recordPlayer = Ad.newBuilder().setRedirectUrl("/product/0PUK6V6EV0")
|
||||
.setText("Vintage record player for sale. 30% off.").build();
|
||||
Ad bike = Ad.newBuilder().setRedirectUrl("/product/9SIQT8TOJO")
|
||||
.setText("City Bike for sale. 10% off.").build();
|
||||
Ad baristaKit = Ad.newBuilder().setRedirectUrl("/product/1YMWWN1N4O")
|
||||
.setText("Home Barista kitchen kit for sale. Buy one, get second kit for free").build();
|
||||
Ad airPlant = Ad.newBuilder().setRedirectUrl("/product/6E92ZMYYFZ")
|
||||
.setText("Air plants for sale. Buy two, get third one for free").build();
|
||||
Ad terrarium = Ad.newBuilder().setRedirectUrl("/product/L9ECAV7KIM")
|
||||
.setText("Terrarium for sale. Buy one, get second one for free").build();
|
||||
Ad camera =
|
||||
Ad.newBuilder()
|
||||
.setRedirectUrl("/product/2ZYFJ3GM2N")
|
||||
.setText("Film camera for sale. 50% off.")
|
||||
.build();
|
||||
Ad lens =
|
||||
Ad.newBuilder()
|
||||
.setRedirectUrl("/product/66VCHSJNUP")
|
||||
.setText("Vintage camera lens for sale. 20% off.")
|
||||
.build();
|
||||
Ad recordPlayer =
|
||||
Ad.newBuilder()
|
||||
.setRedirectUrl("/product/0PUK6V6EV0")
|
||||
.setText("Vintage record player for sale. 30% off.")
|
||||
.build();
|
||||
Ad bike =
|
||||
Ad.newBuilder()
|
||||
.setRedirectUrl("/product/9SIQT8TOJO")
|
||||
.setText("City Bike for sale. 10% off.")
|
||||
.build();
|
||||
Ad baristaKit =
|
||||
Ad.newBuilder()
|
||||
.setRedirectUrl("/product/1YMWWN1N4O")
|
||||
.setText("Home Barista kitchen kit for sale. Buy one, get second kit for free")
|
||||
.build();
|
||||
Ad airPlant =
|
||||
Ad.newBuilder()
|
||||
.setRedirectUrl("/product/6E92ZMYYFZ")
|
||||
.setText("Air plants for sale. Buy two, get third one for free")
|
||||
.build();
|
||||
Ad terrarium =
|
||||
Ad.newBuilder()
|
||||
.setRedirectUrl("/product/L9ECAV7KIM")
|
||||
.setText("Terrarium for sale. Buy one, get second one for free")
|
||||
.build();
|
||||
return ImmutableListMultimap.<String, Ad>builder()
|
||||
.putAll("photography", camera, lens)
|
||||
.putAll("vintage", camera, lens, recordPlayer)
|
||||
|
@ -214,9 +237,11 @@ public final class AdService {
|
|||
}
|
||||
} catch (Exception e) {
|
||||
if (i == (maxAttempts - 1)) {
|
||||
logger.log(Level.WARN, "Failed to register Stackdriver Exporter." +
|
||||
" Tracing and Stats data will not reported to Stackdriver. Error message: " + e
|
||||
.toString());
|
||||
logger.log(
|
||||
Level.WARN,
|
||||
"Failed to register Stackdriver Exporter."
|
||||
+ " Tracing and Stats data will not reported to Stackdriver. Error message: "
|
||||
+ e.toString());
|
||||
} else {
|
||||
logger.info("Attempt to register Stackdriver Exporter in " + sleepTime + " seconds ");
|
||||
try {
|
||||
|
@ -242,18 +267,18 @@ public final class AdService {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Main launches the server from the command line.
|
||||
*/
|
||||
/** Main launches the server from the command line. */
|
||||
public static void main(String[] args) throws IOException, InterruptedException {
|
||||
// Registers all RPC views.
|
||||
RpcViews.registerAllGrpcViews();
|
||||
|
||||
new Thread(new Runnable() {
|
||||
public void run() {
|
||||
initStackdriver();
|
||||
}
|
||||
}).start();
|
||||
new Thread(
|
||||
new Runnable() {
|
||||
public void run() {
|
||||
initStackdriver();
|
||||
}
|
||||
})
|
||||
.start();
|
||||
|
||||
// Register Jaeger
|
||||
initJaeger();
|
||||
|
|
|
@ -16,7 +16,6 @@
|
|||
|
||||
package hipstershop;
|
||||
|
||||
|
||||
import hipstershop.Demo.Ad;
|
||||
import hipstershop.Demo.AdRequest;
|
||||
import hipstershop.Demo.AdResponse;
|
||||
|
@ -38,12 +37,10 @@ import io.opencensus.trace.samplers.Samplers;
|
|||
import java.util.concurrent.TimeUnit;
|
||||
import javax.annotation.Nullable;
|
||||
import org.apache.logging.log4j.Level;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
/**
|
||||
* A simple client that requests ads from the Ads Service.
|
||||
*/
|
||||
/** A simple client that requests ads from the Ads Service. */
|
||||
public class AdServiceClient {
|
||||
|
||||
private static final Logger logger = LogManager.getLogger(AdServiceClient.class);
|
||||
|
@ -52,9 +49,7 @@ public class AdServiceClient {
|
|||
private final ManagedChannel channel;
|
||||
private final hipstershop.AdServiceGrpc.AdServiceBlockingStub blockingStub;
|
||||
|
||||
/**
|
||||
* Construct client connecting to Ad Service at {@code host:port}.
|
||||
*/
|
||||
/** Construct client connecting to Ad Service at {@code host:port}. */
|
||||
private AdServiceClient(String host, int port) {
|
||||
this(
|
||||
ManagedChannelBuilder.forAddress(host, port)
|
||||
|
@ -64,9 +59,7 @@ public class AdServiceClient {
|
|||
.build());
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct client for accessing RouteGuide server using the existing channel.
|
||||
*/
|
||||
/** Construct client for accessing RouteGuide server using the existing channel. */
|
||||
private AdServiceClient(ManagedChannel channel) {
|
||||
this.channel = channel;
|
||||
blockingStub = hipstershop.AdServiceGrpc.newBlockingStub(channel);
|
||||
|
@ -76,25 +69,24 @@ public class AdServiceClient {
|
|||
channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Ads from Server.
|
||||
*/
|
||||
/** Get Ads from Server. */
|
||||
public void getAds(String contextKey) {
|
||||
logger.info("Get Ads with context " + contextKey + " ...");
|
||||
AdRequest request = AdRequest.newBuilder().addContextKeys(contextKey).build();
|
||||
AdResponse response;
|
||||
|
||||
Span span =
|
||||
tracer.spanBuilder("AdsClient").setRecordEvents(true).setSampler(Samplers.alwaysSample())
|
||||
tracer
|
||||
.spanBuilder("AdsClient")
|
||||
.setRecordEvents(true)
|
||||
.setSampler(Samplers.alwaysSample())
|
||||
.startSpan();
|
||||
try (Scope scope = tracer.withSpan(span)) {
|
||||
tracer.getCurrentSpan().addAnnotation("Getting Ads");
|
||||
response = blockingStub.getAds(request);
|
||||
tracer.getCurrentSpan().addAnnotation("Received response from Ads Service.");
|
||||
} catch (StatusRuntimeException e) {
|
||||
tracer
|
||||
.getCurrentSpan()
|
||||
.setStatus(StatusConverter.fromGrpcStatus(e.getStatus()));
|
||||
tracer.getCurrentSpan().setStatus(StatusConverter.fromGrpcStatus(e.getStatus()));
|
||||
logger.log(Level.WARN, "RPC failed: " + e.getStatus());
|
||||
return;
|
||||
} finally {
|
||||
|
@ -118,7 +110,6 @@ public class AdServiceClient {
|
|||
return portNumber;
|
||||
}
|
||||
|
||||
|
||||
private static String getStringOrDefaultFromArgs(
|
||||
String[] args, int index, @Nullable String defaultString) {
|
||||
String s = defaultString;
|
||||
|
@ -154,9 +145,11 @@ public class AdServiceClient {
|
|||
.build());
|
||||
} catch (Exception e) {
|
||||
if (i == (maxAttempts - 1)) {
|
||||
logger.log(Level.WARN, "Failed to register Stackdriver Exporter." +
|
||||
" Tracing and Stats data will not reported to Stackdriver. Error message: " + e
|
||||
.toString());
|
||||
logger.log(
|
||||
Level.WARN,
|
||||
"Failed to register Stackdriver Exporter."
|
||||
+ " Tracing and Stats data will not reported to Stackdriver. Error message: "
|
||||
+ e.toString());
|
||||
} else {
|
||||
logger.info("Attempt to register Stackdriver Exporter in " + sleepTime + " seconds");
|
||||
try {
|
||||
|
@ -169,7 +162,7 @@ public class AdServiceClient {
|
|||
}
|
||||
|
||||
// Register Prometheus exporters and export metrics to a Prometheus HTTPServer.
|
||||
//PrometheusStatsCollector.createAndRegister();
|
||||
// PrometheusStatsCollector.createAndRegister();
|
||||
|
||||
AdServiceClient client = new AdServiceClient(host, serverPort);
|
||||
try {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue