Skip to content

Commit 3c9133e

Browse files
fix(functions): add code to shutdown publisher to free up resources (GoogleCloudPlatform#10027)
1 parent a585cff commit 3c9133e

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

functions/pubsub/publish-message/src/main/java/functions/PublishMessage.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.nio.charset.StandardCharsets;
3131
import java.util.Optional;
3232
import java.util.concurrent.ExecutionException;
33+
import java.util.concurrent.TimeUnit;
3334
import java.util.logging.Level;
3435
import java.util.logging.Logger;
3536

@@ -40,7 +41,8 @@ public class PublishMessage implements HttpFunction {
4041
private static final Logger logger = Logger.getLogger(PublishMessage.class.getName());
4142

4243
@Override
43-
public void service(HttpRequest request, HttpResponse response) throws IOException {
44+
public void service(HttpRequest request, HttpResponse response)
45+
throws IOException, InterruptedException {
4446
Optional<String> maybeTopicName = request.getFirstQueryParameter("topic");
4547
Optional<String> maybeMessage = request.getFirstQueryParameter("message");
4648

@@ -72,6 +74,12 @@ public void service(HttpRequest request, HttpResponse response) throws IOExcepti
7274
} catch (InterruptedException | ExecutionException e) {
7375
logger.log(Level.SEVERE, "Error publishing Pub/Sub message: " + e.getMessage(), e);
7476
responseMessage = "Error publishing Pub/Sub message; see logs for more info.";
77+
} finally {
78+
if (publisher != null) {
79+
// When finished with the publisher, shutdown to free up resources.
80+
publisher.shutdown();
81+
publisher.awaitTermination(1, TimeUnit.MINUTES);
82+
}
7583
}
7684

7785
responseWriter.write(responseMessage);

functions/pubsub/publish-message/src/test/java/functions/PublishMessageTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ public void beforeTest() throws IOException {
7070
}
7171

7272
@Test
73-
public void functionsPubsubPublish_shouldFailWithoutParameters() throws IOException {
73+
public void functionsPubsubPublish_shouldFailWithoutParameters()
74+
throws IOException, InterruptedException {
7475
new PublishMessage().service(request, response);
7576

7677
writerOut.flush();
77-
assertThat(responseOut.toString()).isEqualTo(
78-
"Missing 'topic' and/or 'message' parameter(s).");
78+
assertThat(responseOut.toString()).isEqualTo("Missing 'topic' and/or 'message' parameter(s).");
7979
}
8080

8181
@Test
@@ -86,8 +86,8 @@ public void functionsPubsubPublish_shouldPublishMessage() throws Exception {
8686
new PublishMessage().service(request, response);
8787

8888
writerOut.flush();
89-
assertThat(logHandler.getStoredLogRecords().get(0).getMessage()).isEqualTo(
90-
"Publishing message to topic: " + FUNCTIONS_TOPIC);
89+
assertThat(logHandler.getStoredLogRecords().get(0).getMessage())
90+
.isEqualTo("Publishing message to topic: " + FUNCTIONS_TOPIC);
9191
assertThat(responseOut.toString()).isEqualTo("Message published.");
9292
}
9393
}

0 commit comments

Comments
 (0)