I would like to populate Redis from a Postgres table, and have a method that works. I would like this method to be run on application startup. However, when I try to use @PostConstruct
, it seems to run the method but not successfully. I'm using R2DBC to connect with Postgres.
I've tested this by creating an endpoint that calls this method. Upon startup, I will see the "test" message printed, meaning that the method is executed. However, Redis is not populated. But then when I hit the endpoint, I see the "test" message AND Redis is populated. What the heck?
@PostConstruct
public Mono<List<Boolean>> writeRedisFromPostgres() {
locationRepository.flushAll();
System.out.println("test");
return locationPostgresRepository.findAll().flatMap(
location -> locationRepository.writeLocation(location)
).collectList();
}
It looks like the locationPostgresRepository.findAll()
is returning empty on startup only. I put breakpoints in the method above as well as in my PostgresConfiguration class, and the configuration class is being executed first...
@Configuration
class PostgresConfig extends AbstractR2dbcConfiguration {
@Value("${postgres.host}")
private String host;
@Value("${postgres.port}")
private Integer port;
@Value("${postgres.username}")
private String username;
@Value("${postgres.password}")
private String password;
@Value("${postgres.database}")
private String database;
@Override
@Bean
public ConnectionFactory connectionFactory() {
return new PostgresqlConnectionFactory(
PostgresqlConnectionConfiguration.builder()
.host(host)
.port(port)
.username(username)
.password(password)
.database(database)
.build()
);
}
}