This code runs forever, testing this code in local
@GetMapping (value = "/callResourceByKey/{key}",produces={"application/json"})
public ResponseEntity<Response> getResponse(){
GcpPubSubConfig gcpPubSubConfig = new GcpPubSubConfig();
try {
gcpPubSubConfig.publishWithErrorHandler("hello", "project", "topic");
}
catch (Exception e){
System.out.println("error"+e.getMessage());
}
}
onSuccess and onFailure are not being called and it's waiting forever in publisher.shutdown()
import java.io.IOException;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import com.google.api.core.ApiFuture;
import com.google.api.core.ApiFutureCallback;
import com.google.api.core.ApiFutures;
import com.google.api.gax.rpc.ApiException;
import com.google.cloud.pubsub.v1.Publisher;
import com.google.protobuf.ByteString;
import com.google.pubsub.v1.PubsubMessage;
import com.google.pubsub.v1.TopicName;
@Component
public class GcpPubSubConfig {
@Async
public void publishWithErrorHandler(String message, String projectId, String topicId) throws IOException, InterruptedException {
TopicName topicName = TopicName.of(projectId, topicId);
Publisher publisher = null;
try {
publisher = Publisher.newBuilder(topicName).build();
System.out.println("hello1");
ByteString data = ByteString.copyFromUtf8(message);
System.out.println("hello2");
PubsubMessage pubsubMessage = PubsubMessage.newBuilder().setData(data).build();
System.out.println("hello3");
ApiFuture<String> future = publisher.publish(pubsubMessage);
System.out.println("hello4");
ApiFutures.addCallback(future, new ApiFutureCallback<String>() {
public void onSuccess(String messageId) {
// Ignore for now
System.out.println("hellosuc");
}
public void onFailure(Throwable t) {
if (t instanceof ApiException) {
System.out.println("helloerr");
ApiException apiException = ((ApiException) t);
}
}
}, Executors.newSingleThreadExecutor());
System.out.println("hello5");
} finally {
System.out.println("hello6");
if (publisher != null) {
// When finished with the publisher, shutdown to free up resources.
System.out.println("hello7");
publisher.shutdown();
System.out.println("hello8");
publisher.awaitTermination(1, TimeUnit.MINUTES);
}
}
System.out.println("hello9");
}
}
:: Spring Boot :: (v2.7.2)
3408 --- [ main] DemoApplicationResource : No active profile set, falling back to 1 default profile: "default"
3408 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data Datastore repositories in DEFAULT mode.
3408 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 18 ms. Found 0 Datastore repository interfaces.
3408 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
3408 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8086 (http)
3408 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
3408 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.58]
3408 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
3408 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2765 ms
3408 --- [ main] o.s.c.g.a.c.GcpContextAutoConfiguration : The default project ID is project
3408 --- [ main] c.g.a.oauth2.DefaultCredentialsProvider : Your application has authenticated using end user credentials from Google Cloud SDK. We recommend that most server applications use service accounts instead. If your application continues to use end user credentials from Cloud SDK, you might receive a "quota exceeded" or "API not enabled" error. For more information about service accounts, see https://cloud.google.com/docs/authentication/.
3408 --- [ main] o.s.c.g.core.DefaultCredentialsProvider : Default credentials provider for user .apps.googleusercontent.com
3408 --- [ main] o.s.c.g.core.DefaultCredentialsProvider : Scopes in use by default credentials: [https://www.googleapis.com/auth/pubsub, https://www.googleapis.com/auth/spanner.admin, https://www.googleapis.com/auth/spanner.data, https://www.googleapis.com/auth/datastore, https://www.googleapis.com/auth/sqlservice.admin, https://www.googleapis.com/auth/devstorage.read_only, https://www.googleapis.com/auth/devstorage.read_write, https://www.googleapis.com/auth/cloudruntimeconfig, https://www.googleapis.com/auth/trace.append, https://www.googleapis.com/auth/cloud-platform, https://www.googleapis.com/auth/cloud-vision, https://www.googleapis.com/auth/bigquery, https://www.googleapis.com/auth/monitoring.write]
3408 --- [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 1 endpoint(s) beneath base path '/actuator'
3408 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8086 (http) with context path ''
3408 --- [ main] DemoApplicationResource : Started DemoApplicationResource in 16.485 seconds (JVM running for 17.372)
3408 --- [nio-8086-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
3408 --- [nio-8086-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
3408 --- [nio-8086-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 0 ms