use of com.google.cloud.translate.Translate in project getting-started-java by GoogleCloudPlatform.
the class BackgroundContextListener method contextInitialized.
@Override
public void contextInitialized(ServletContextEvent event) {
String firestoreProjectId = System.getenv("FIRESTORE_CLOUD_PROJECT");
Firestore firestore = (Firestore) event.getServletContext().getAttribute("firestore");
if (firestore == null) {
firestore = FirestoreOptions.getDefaultInstance().toBuilder().setProjectId(firestoreProjectId).build().getService();
event.getServletContext().setAttribute("firestore", firestore);
}
Translate translate = (Translate) event.getServletContext().getAttribute("translate");
if (translate == null) {
translate = TranslateOptions.getDefaultInstance().getService();
event.getServletContext().setAttribute("translate", translate);
}
String topicId = System.getenv("PUBSUB_TOPIC");
TopicName topicName = TopicName.of(firestoreProjectId, topicId);
Publisher publisher = (Publisher) event.getServletContext().getAttribute("publisher");
if (publisher == null) {
try {
publisher = Publisher.newBuilder(topicName).build();
event.getServletContext().setAttribute("publisher", publisher);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Aggregations