我只是在我的代码中设置了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;
}