Spring boot GCP PUBSUB Consumer application - spring

Can anyone share spring boot GCP PUBSUB subscriber application example, which keep running util externally killed (not rest api)?

Normally, our documentation instructs users to provide their own ThreadPoolTaskScheduler bean named pubsubSubscriberThreadPool with non-daemon threads to keep the application running. However, this seems to have stopped working and is currently an open issue.
In the meantime, you can provide a scheduled no-op process as a workaround:
#Scheduled (fixedRate = 1, timeUnit = TimeUnit.MINUTES)
public void ensureApplicationRuns() {
// do nothing, or log message
}

Related

Spring Kafka Listener shutdown Application

I have an application that creates 3 KafkaListener per configuration in the ConcurrentKafkaListenerContainerFactory if any of these Listeners throws a specific Exception. I would like to shutdown the entire spring application.
Since these errors are non recoverable and need manual intervention.
How would i go about doin this?
Do i just inject the ApplicationContext into the Listener and close the application from there?
Or is there a more appropriate way of handling this?
That's one way, or you could just call System.exit(1).

Spring Cloud with Liberty

I am currently running a Spring Boot application inside of Websphere Liberty, and use Consul for Service Discovery. To register services with Consul, I created a Liberty feature that hooks in to the Application Lifecycle events and performs the registration /deregistration. This works great, but by doing so I am coupling myself to Liberty. Spring-Cloud-Consul looks like it might solve that issue, but I can't get it to register a service with Liberty (it does connect to Consul) - only with an Embedded Tomcat Server. After looking at the Spring-Cloud-Consul code, the issue is that a EmbeddedServletContainerInitializedEvent isn't being fired, so no port is being set.
My question is, does Spring Cloud Consul only work with embedded servlet containers?
It is a known issue. Feel free to submit a PR. https://github.com/spring-cloud/spring-cloud-consul/issues/173
My solution was to bring spring-cloud-consul's ConsulLifecycle class local and add an ApplicationReadyEvent, like so :
#Autowired(required = false)
private ServerProperties serverProperties;
#EventListener
public void onApplicationReady(ApplicationReadyEvent event) {
this.setConfiguredPort(serverProperties.getPort());
log.info("Application ready on port " + serverProperties.getPort());
this.start();
}
Now my services register and deregister as expected.

Consuming Messages from Amazon SQS using Spring

i am using the Amazon SQS as Message Queue. I am investigating how it is possible to set up a Spring consumer within Tomcat that would consume messages. However i looked around and it seems to say that to deploy a Spring Message Driven Bean to consume messages from Queue in Tomcat, i would need TomcatEE / Tomcat + ActiveMQ.
At the same time i have also reviewed the following SQS-Spring driver and wonder if it is of much use. http://nevado.skyscreamer.org/quickstart.html
Could someone advise what is required to accomplish the above?
SimpleMessageListenerContainer can be used to start/stop listeners programatically.
simpleMessageListenerContainer.start("logical queue name")
Other than that you have two options. Using spring's QueueMessagingTemplate
Message<?> msg = ((QueueMessagingTemplate) template).receive("logical queue name");
this will requiere that no listeners are defined in the application for this queue.
Or use the spring cloud messaging annotation SQSListener
#SqsListener(value = "logical queue name")

Spring server start-up event

i am facing a problem. I am using tomcat server for my spring maven project and i want to subscribe my application to facebook once when server starts up. I have tried ApplicationListener ContextRefreshedEvent. It gets invoked on application context initialization, but the issue is at that time my server has not completely started and hence my application is not publically accessible which is required in my case as facebook subscription requires verification using application public URL. So i want to do subscription when my server gets started completey and not on application context initialization. Any idea how can i do it? Do i have nay application level event listener that can tell me that server has started completely?
Regards jia
You can annotate a bean method like so :
#PostConstruct
public void yrSubscribeLogic() {}
the app context can only be loaded after the server has fully started.

Spring JMS injection causing application not to startup

We have a spring application that publishes and listens to queues on a remote application server. My publisher and listener which are spring based listen within our own application server.
One of the problems we have for our test environments is the other app. server is not up so when our test application goes to start and it tries to inject JmsTemplate with its connectionFactory it blows up because it is not a valid connection and our entire application fails to load. This is causing grief with the other developers in our group that have nothing to do with JMS. All they want to do is run and test their code but the JmsTemplate connectionFactory is down.
Does anyone have any suggestion for enabling spring ignore some bad injections which will allow our application to start properly?
Thanks
I believe this could be achieved by defining separate spring profiles and then passing it as a parameter in your test environments while starting your application. You could mock or ignore any beans then.
Example
import org.springframework.context.annotation.Profile;
#Profile("test")
public class AppConfigTest {
....
....
}
JVM param / System property
-Dspring.profiles.active=test

Resources