I have this scheduler which is used to check for new data into DB table:
@Service
public class ReloadCache {
@Scheduled(fixedDelay = 1000)
public void reload() {
...... do something
}
}
......
@Service
public class LogicalClient {
final Map<String, Map<String, String>> map;
@PostConstruct
public void initializeBalances() {
............ map = new HashMap.......
}
@KafkaListener(......")
public void handle(....) {
.......read map here
}
}
Note that these two services are located in separate Java classes and packages.
When schedule runs and change is detected how how I can call again Java method initializeBalances
annotated with @PostConstruct
in order to generate again the map structure?
CodePudding user response:
Inject your LogicalClient
in your ReloadCache
class and call that function like this:
@Service
public class ReloadCache {
private final LogicalClient logicalClient;
public ReloadCache(LogicalClient client) // Injection through constructor.
{
this.logicalClient = client;
}
@Scheduled(fixedDelay = 1000)
public void reload() {
...... do something
client.initializeBalances()
}
}
Both your classes are annotated with the @Service
. So they are both Spring beans that can be injected wherever you find it suitable (as long as the receiving class is a bean itself).