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

Google App Engine does not support vision api Runtime.addShutdownHook error

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

@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 资源管理器中的这个抛出:

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.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)

根据 Google 的回复,这是 google 应用引擎和 grpc 之间与 google 视觉

通信的已知兼容性问题

我在 AppEngine 上遇到了同样的问题,当我转向 Compute Engine 时,我在 tomcat.

上遇到了类似的问题

对于旧库,我对 AppEngine 没有任何问题,我建议同时使用它:https://developers.google.com/api-client-library/java/apis/vision/v1

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>

完成。现在它应该工作了。