Google App Engine不支持vision api Runtime.addShutdownHook错误

时间:2017-03-23 05:51:03

标签: java google-app-engine google-cloud-endpoints google-api-java-client google-cloud-vision

我的代码看起来像是抛出了我不知道的异常:

@ApiMethod(name = "analyzeImage")
      public Message analyzeImage(@Named("imgURL") String imgURL) {
        try {
            GoogleCredential credential = GoogleCredential.getApplicationDefault();

             ImageAnnotatorClient vision = ImageAnnotatorClient.create();

            // The path to the image file to annotate
            String fileName = imgURL;

             // Reads the image file into memory
            Path path = Paths.get(fileName);
            byte[] data = Files.readAllBytes(path);
            ByteString imgBytes = ByteString.copyFrom(data);

         // Builds the image annotation request
            List<AnnotateImageRequest> requests = new ArrayList<>();
            Image img = Image.newBuilder().setContent(imgBytes).build();
            Feature feat = Feature.newBuilder().setType(Type.LABEL_DETECTION).build();
            AnnotateImageRequest request = AnnotateImageRequest.newBuilder()
                .addFeatures(feat)
                .setImage(img)
                .build();
            requests.add(request);

         // Performs label detection on the image file
            BatchAnnotateImagesResponse response = vision.batchAnnotateImages(requests);
            List<AnnotateImageResponse> responses = response.getResponsesList();

            for (AnnotateImageResponse res : responses) {
              if (res.hasError()) {
                System.out.printf("Error: %s\n", res.getError().getMessage());
                return new Message("Something broke");
              }

              for (EntityAnnotation annotation : res.getLabelAnnotationsList()) {
                Map<FieldDescriptor, Object> m = annotation.getAllFields();
                Set<FieldDescriptor> s = m.keySet();
                for(FieldDescriptor key : s){
                    System.out.printf("%s : %s\n", key, m.get(key).toString());
                }
              }
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return new Message("Hello:" + imgURL);
      } 

在api explorer中运行此命令:

java.lang.SecurityException: Google App Engine does not support Runtime.addShutdownHook
    at com.google.appengine.runtime.Request.process-75407a6647252b61(Request.java)
    at java.lang.Runtime.addShutdownHook(Runtime.java:45)
    at com.google.common.util.concurrent.MoreExecutors$Application.addShutdownHook(MoreExecutors.java:232)
    at com.google.common.util.concurrent.MoreExecutors$Application.addDelayedShutdownHook(MoreExecutors.java:204)
    at com.google.common.util.concurrent.MoreExecutors$Application.getExitingScheduledExecutorService(MoreExecutors.java:196)
    at com.google.common.util.concurrent.MoreExecutors$Application.getExitingScheduledExecutorService(MoreExecutors.java:228)
    at com.google.common.util.concurrent.MoreExecutors.getExitingScheduledExecutorService(MoreExecutors.java:176)
    at com.google.api.gax.grpc.InstantiatingExecutorProvider.getExecutor(InstantiatingExecutorProvider.java:51)
    at com.google.api.gax.grpc.ChannelAndExecutor.create(ChannelAndExecutor.java:62)
    at com.google.api.gax.grpc.ClientSettings.getChannelAndExecutor(ClientSettings.java:81)
    at com.google.cloud.vision.spi.v1.ImageAnnotatorClient.<init>(ImageAnnotatorClient.java:120)
    at com.google.cloud.vision.spi.v1.ImageAnnotatorClient.create(ImageAnnotatorClient.java:111)
    at com.google.cloud.vision.spi.v1.ImageAnnotatorClient.create(ImageAnnotatorClient.java:102)
    at photography.viks.ig.api.InstaSmartSvc.analyzeImage(InstaSmartSvc.java:93)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:42)
    at com.google.api.server.spi.SystemService.invokeServiceMethod(SystemService.java:351)
    at com.google.api.server.spi.handlers.EndpointsMethodHandler$RestHandler.handle(EndpointsMethodHandler.java:114)
    at com.google.api.server.spi.handlers.EndpointsMethodHandler$RestHandler.handle(EndpointsMethodHandler.java:102)
    at com.google.api.server.spi.dispatcher.PathDispatcher.dispatch(PathDispatcher.java:49)
    at com.google.api.server.spi.EndpointsServlet.service(EndpointsServlet.java:71)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
    at com.google.api.control.ControlFilter.doFilter(ControlFilter.java:220)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.api.control.ConfigFilter.doFilter(ConfigFilter.java:120)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:326)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
    at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:923)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
    at com.google.tracing.TraceContext$TraceContextRunnable.runInContext(TraceContext.java:453)
    at com.google.tracing.TraceContext$TraceContextRunnable$1.run(TraceContext.java:460)
    at com.google.tracing.CurrentContext.runInContext(CurrentContext.java:293)
    at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContextNoUnref(TraceContext.java:319)
    at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContext(TraceContext.java:311)
    at com.google.tracing.TraceContext$TraceContextRunnable.run(TraceContext.java:457)
    at java.lang.Thread.run(Thread.java:745)

2 个答案:

答案 0 :(得分:1)

根据Google的回复,谷歌应用引擎和grpc之间的已知兼容性问题与谷歌愿景进行沟通

答案 1 :(得分:0)

Google App Engine不支持Java 7中的Runtime.addShutdownHook,但支持 Java 8 。请在github上查看。

如何迁移到适用于Google App Engine标准环境的Java 8?
1.在你的appengine-web.xml中添加

<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
    ...
    <runtime>java8</runtime>
</appengine-web-app>

2。在你的pom.xml中添加

<project>
    ...
    <properties>
        ...
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
</project>

完成。现在它应该工作。