1
votes

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

  1. is this the right way of doing it?
  2. Is there any cleaner way to set the couchbaseTemplate,cluster and Bucket object without affecting live transaction.
  3. 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.

1
Can configureRepositoryOperationsMapping can change the template at runtime by calling set Default template after 2 days?user2758406

1 Answers

-1
votes

Duplicate the CouchbaseTemplate class in your own package. Allow the setting of the bucket in that class.

   /** Duplicate the CouchbaseTemplate from springData and allow setter method
 for Bucket in that class .the couchbaseTemplate is instantiated in spring from AbstractCouchbaseConfiguration class extension. **/
        @Autowired CustomCouchbaseTemplate customCouchbaseTemplate 

in your class that will be creating the new Bucket Object.From there

Bucket oldBucket= customCouchbaestemplate.getBucket();
customCouchbaestemplate.setBucket( newBucket);
Thread.sleep(100000); //Time in ms

This will allow existing bucket to close after certain interval so that it can finish its pending queries. The new queries will start execution on the new bucket.