There is an application for spring boot. I want to write a class that will do a reconnect to RethinkDB if the base has fallen. I launched a task that checks if the connection is alive, and if it is closed, then I need to create a new connection and add it to the application context. The class is as follows:
package service; import com.rethinkdb.RethinkDB; import com.rethinkdb.net.Connection; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; @Service public class RethinkDBReconnector { @Autowired private RethinkDB r; @Autowired private Connection conn; @Scheduled(fixedRate = 1000) public void task() { if (!conn.isOpen()) { System.out.println("closed"); } } } The connection is configured as:
@Configuration public class SpringConfig { @Bean public Connection connection(RethinkDB r) { return r.connection().hostname("127.0.0.1").port(28015).connect(); } @Bean public RethinkDB rethinkDB() { return RethinkDB.r; } } But I do not know how to get the application context and add a new connection to it so that it can be used instead of the old one. I do not know if it can be implemented as a dependency. Most likely no. How can I solve this problem?
The problem is not solved. Assigning a new value to a link in the reconnect does not update the connection globally.
I tried to do this:
@Bean @Scheduled(fixedRate = 1000) public Connection connection(RethinkDB r) { Connection conn = r.connection().connect(); if (conn == null || (!conn.isOpen())) { conn = r.connection().connect(); } return conn; } At startup, an exception occurs:
Caused by: java.lang.IllegalStateException: Encountered invalid @Scheduled method 'connection': Only no-arg methods may be annotated with @Scheduled at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.processScheduled(ScheduledAnnotationBeanPostProcessor.java:496) at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.lambda$null$1(ScheduledAnnotationBeanPostProcessor.java:359) at java.base/java.lang.Iterable.forEach(Iterable.java:75) at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.lambda$postProcessAfterInitialization$2(ScheduledAnnotationBeanPostProcessor.java:359) at java.base/java.util.LinkedHashMap.forEach(LinkedHashMap.java:684) at org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor.postProcessAfterInitialization(ScheduledAnnotationBeanPostProcessor.java:358) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:434) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1749) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:576) ... 63 common frames omitted
Can't write query because response pump is not running.How to fix? - typemoon@Scheduled(fixedRate = 1000)on the connection creation method (with checking fornulland for!conn.isOpen()) - Chubatiy