I want to replace a spring couchbaseTemplate object which is generated on startup by custom Bucket object( where username is <> bucketName) and Cluster object.I am able to create and make the application running.CouchbaseTemplate is also getting invoked. After 2 days,I will get new username,password .So i want to reload the couchbaseTemplate,Cluster and Bucket - 3 new objects in the spring context so that the new couchbaseTemplate starts getting operational.I have tried to replace the couchbaseTemplate from the applicationContext by calling.
applicationContext.destroyBean( "couchbaseTemplate",applicationContext.getBean("couchbaseTemplate"))
applicationContext.registerSingleton( "couchbaseTemplate",couchbaseTemplate);
But this code doesnt work for me and says bean is still present in context. My question is
- is this the right way of doing it?
- Is there any cleaner way to set the couchbaseTemplate,cluster and Bucket object without affecting live transaction.
- Will RefreshScope approach to reload bean help here?However that doesnt gaurantee all dependent bean reloads dependent on the bean which is annotated with @RefreshScope.
The volume of hits to couchbase DB is 100/second .
Further findings I tried further and found that the spring-data-couchbase repository can be modified to allow the setting up of the Bucket inside the CouchbaseTemplate.java using the AtomicReference for the client to use the Bucket
AtomicReference<Bucket> bucketRef ;
Bucket getClient(atomicReferenceBucket.get()) ;
void setAtomicReferenceBucket(Bucket bucket) {
AtomicReference<Bucket> bucketRef= new AtomicReference(bucket) ;
return bucketRef;
}
resetBucket(Bucket bucket){
bucketRef.set(bucket);
}
replace all client with the getClient() method
However i get requestCancelledInFlightException for first requet when i swap the bucket created using new username and password. Please suggest.