Google自然语言在LOCALHOST上失败。 JAVA:DEADLINE超出错误

时间:2017-05-19 11:24:47

标签: java localhost google-api-java-client deadlines

我只是在我的代码中设置了Google Natural Language客户端。在我的本地系统上一切正常,Google Natural Language API会在几秒钟内完成。

但是当我尝试在Tomcat(localhost)上运行我的代码时,请求在DEADLINE EXCEEDED ERROR 1分钟后终止,同时进行非常简单的测试调用。

以下是一些代码:

public static List<Entity> getLanguageEntities(String input, InputStream credentialInputStream) {
    try {
        // Instantiates a client
        LanguageServiceClient language = getClient(credentialInputStream);

        // build document
        Document doc = Document.newBuilder().setContent(input).setType(Type.PLAIN_TEXT).build();

        //HERE IT BREAKS..
        AnalyzeEntitiesResponse analyzeEntitiesResponse = language.analyzeEntities(doc, EncodingType.NONE);

        List<Entity> entityList = analyzeEntitiesResponse.getEntitiesList();

        return entityList;

    }catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

public static LanguageServiceClient getClient(InputStream credentialInputStream) throws Exception {

    final LanguageServiceSettings languageServiceSettings;

    final GoogleCredentials myCredentials = GoogleCredentials
                .fromStream(credentialInputStream)
                .createScoped(
                        Collections.singleton("https://www.googleapis.com/auth/cloud-platform")
                );
        final CredentialsProvider credentialsProvider = FixedCredentialsProvider.create(myCredentials);

        final InstantiatingChannelProvider channelProvider = LanguageServiceSettings
                .defaultChannelProviderBuilder()
                .setCredentialsProvider(credentialsProvider)
                .build();
        languageServiceSettings = LanguageServiceSettings
                .defaultBuilder()
                .setChannelProvider(channelProvider)
                .build();

    // Create Google API client
    LanguageServiceClient languageServiceClient = LanguageServiceClient.create(languageServiceSettings);

    return languageServiceClient;
}

0 个答案:

没有答案