openstack4j
openstack4j copied to clipboard
getting exception Already connected while trying to reuse the token in object store v3
Below is the snippet of my code. The authentication is successful and token is generated. But the same token cannot be resued for multiple service . I m getting error already connected exception. Pease help me to resolve this issue at the earliest as it has become blocker for me.
void initializeOSAccess() {
log.info "initializing Object Store Access"
osClient = OSFactory.builderV3()
.withConfig(Config.newConfig().withSSLVerificationDisabled())
.endpoint(objectStoreProperties.endpoint)
.credentials(objectStoreProperties.username, objectStoreProperties.password, org.openstack4j.model.common.Identifier.byName("user"))
.scopeToProject(org.openstack4j.model.common.Identifier.byName(objectStoreProperties.tenant), org.openstack4j.model.common.Identifier.byName("user"))
.authenticate()
log.info "Token Id : ${osClient.getToken().getId()}"
log.info "Token Expires: ${osClient.getToken().getExpires()}"
}
OSClient connectToOS() {
if (!token || isTokenExpired()) {
synchronized (this) {
if (!token || tokenExpired) {
initializeOSAccess()
token = osClient.token
}
}
}
log.info "## My Token Id : ${token}"
OSFactory.clientFromToken(token)
}
boolean isTokenExpired() {
long now = System.currentTimeMillis()
long expires = token.expires.time
now >= expires
}
String uploadFileToRegion(String region, String containerName, String fileName, InputStream is ,int remainingAttempts, int retryWaitSeconds ){ String etag try{ OSClient.OSClientV3 osClientV3 = connectToOS()
log.info("using region: ${region}")
osClientV3.useRegion(region)
etag = osClientV3.objectStorage().objects().put( containerName, fileName, Payloads.create(is))
}catch(OS4JException ex){
if (--remainingAttempts == 0)
{
log.warn('Retries exhausted.')
throw ex
}
else
{
try {
log.warn("Exception occurred.. will wait for ${retryWaitSeconds} seconds before retry.")
Thread.sleep((1000*retryWaitSeconds))
return uploadFileToRegion(region,containerName,fileName, is, remainingAttempts, retryWaitSeconds)
} catch (InterruptedException ie) {
log.warn('We should not come here, anyways do nothing'+ie.message)
}
}
}
etag
}
Add withSSLVerificationDisabled and have a try.
Config config = Config.newConfig().withSSLVerificationDisabled();
OSClientV3 clientV3 = OSFactory.clientFromToken(token, config);