I'm trying to use Lettuce reactive api for creating subscriber which will be listening forever for messages from some predefined Redis topics
I've created a spring bean which contains logic of creating subscription/handling messages:
@PostConstructpublic void init() { StatefulRedisPubSubConnection<String, String> statefulRedisPubSubConnection = redisClient.connectPubSub(); RedisPubSubReactiveCommands<String, String> redisCommands = statefulRedisPubSubConnection.reactive(); redisCommands.subscribe(topicNames).subscribe(); redisCommands.observeChannels() .doOnNext(channelMessage -> //do something) .doOnError(err -> //handle error) .subscribe();}
Unfortunately, the code above doesn't keep the thread up. So, Spring Boot just shut down application right away after startup.
If I add some trick to keep main thread alive(like adding forever 'for' loop), then subscriber works perfectly. It receives messages from Redis topic.
How can I keep the application up forever with more elegant solution?