简体   繁体   中英

how to avoid Google App Engine does not support Runtime.addShutdownHook

I run this java code on google app engine (java7)

and i get an error, but i'm not sure what does it mean and how to avoid it?

        final TopicName topicName = pubSubFactory.createOrGetTopic(SpreadSheetConfig.s.TOPIC_NAME_ADD_PARTNER_REQUEST);
        CustomPublisher customPublisher = pubSubFactory.createPublisher(topicName);
        PublisherCallbackWithLog publisherCallback = new PublisherCallbackWithLog<String>();

        for (WazeSdkPartner.WazeSdkRequest wazeSdkRequest : wazeSdkRequestsList.getRequestList()) {
            customPublisher.publish(wazeSdkRequest, publisherCallback);
        }

and

public <T extends MessageLite> ApiFuture<String> publish(final T message, final ApiFutureCallback<T> futureCallback) throws Exception {
    final PubsubMessage      pubsubMessage   = PubsubMessage.newBuilder().setData(message.toByteString()).build();
    final ApiFuture<String>  messageIdFuture = publisher.publish(pubsubMessage);

    if ( futureCallback != null ) {
        ApiFutures.addCallback(messageIdFuture, (ApiFutureCallback) futureCallback);
    }

    return messageIdFuture;
}

error: Google App Engine does not support Runtime.addShutdownHook

/
java.lang.RuntimeException: java.lang.SecurityException: Google App Engine does not support Runtime.addShutdownHook
    at com.waze.sdkService.servlets.SdkPollerServlet.publishAddPartnersRequests(SdkPollerServlet.java:96)
    at com.waze.sdkService.servlets.SdkPollerServlet.publishRequestsIfNewPartners(SdkPollerServlet.java:72)
    at com.waze.sdkService.servlets.SdkPollerServlet.doGet(SdkPollerServlet.java:63)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

You are seeing that error because Google Cloud PubSub uses gRPC as transport layer. In particular, it uses a ManagedChannel and a ScheduledExecutorService for making calls (both provided as a ChannelAndExecutor pair). The executor service exits when the application is complete, and it does so by using daemon threads and adding a shutdown hook to wait for their completion.

However, gRPC is not supported by App Engine Standard using the Java 7 runtime. Java 8 supports gRPC , but it looks like there are no plans to support it also on Java 7 (see this google-cloud-java GitHub issue ).

To specify the Java 8 runtime for your application, just add this line to your appengine-web.xml file:

<runtime>java8</runtime>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM