Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to retrieve current session. Please verify thread has a current session available. #745

Closed
jianke221 opened this issue Jul 11, 2016 · 9 comments

Comments

@jianke221
Copy link

when i use openstack4j to upload a file to container by Multi thread,it failed my program as follows:
UploadTaskThread.java:

public class UploadTaskThread implements Runnable, Serializable {
private static final long serialVersionUID = 5833214272850373920L;

private OSClientV3 os;
private String filename;

public UploadTaskThread(OSClientV3 os,String filename){
    this.os = os;
    this.filename = filename;
}


public void run() {
    File f=new File("f:"+"/"+"9124e1bf-a4fe-4583-bff7-63530fbaff6d"+"/"+filename);
    String etag = os.objectStorage().objects().put("9124e1bf-a4fe-4583-bff7-63530fbaff6d", filename, Payloads.create(f));
    System.out.println(filename+"上传成功!!!+++++++"+etag);   

}

}

in main method :
FixThreadPool.getInstance();

    for(int i = 0;i<254;i++){

        String filename = String.valueOf(i)+".ts";
        UploadTaskThread task = new UploadTaskThread(os,filename);
        FixThreadPool.getInstance().execute(task);
    }
    FixThreadPool.getInstance().closeFixThreadPool();

the Error information:
Exception in thread "pool-1-thread-242" org.openstack4j.api.exceptions.OS4JException: Unable to retrieve current session. Please verify thread has a current session available.
at org.openstack4j.openstack.internal.BaseOpenStackService.builder(BaseOpenStackService.java:95)
at org.openstack4j.openstack.internal.BaseOpenStackService.builder(BaseOpenStackService.java:88)
at org.openstack4j.openstack.internal.BaseOpenStackService.put(BaseOpenStackService.java:58)
at org.openstack4j.openstack.storage.object.internal.ObjectStorageObjectServiceImpl.put(ObjectStorageObjectServiceImpl.java:125)
at org.openstack4j.openstack.storage.object.internal.ObjectStorageObjectServiceImpl.put(ObjectStorageObjectServiceImpl.java:105)
at com.inspur.openstack4j.service.UploadTaskThread.run(UploadTaskThread.java:23)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Exception in thread "pool-1-thread-236" org.openstack4j.api.exceptions.OS4JException: Unable to retrieve current session. Please verify thread has a current session available.
at org.openstack4j.openstack.internal.BaseOpenStackService.builder(BaseOpenStackService.java:95)
at org.openstack4j.openstack.internal.BaseOpenStackService.builder(BaseOpenStackService.java:88)
at org.openstack4j.openstack.internal.BaseOpenStackService.put(BaseOpenStackService.java:58)
at org.openstack4j.openstack.storage.object.internal.ObjectStorageObjectServiceImpl.put(ObjectStorageObjectServiceImpl.java:125)
at org.openstack4j.openstack.storage.object.internal.ObjectStorageObjectServiceImpl.put(ObjectStorageObjectServiceImpl.java:105)
at com.inspur.openstack4j.service.UploadTaskThread.run(UploadTaskThread.java:23)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

@gondor
Copy link
Member

gondor commented Jul 11, 2016

@auhlig With V3, does this guide still apply or should we add an alternate for V3? http://www.openstack4j.com/learn/threads/

@jianke221
Copy link
Author

i use V3,but when i use "Access access = os.getAccess();" it show "- The method getAccess() is undefined for the type
OSClient.OSClientV3
- Access cannot be resolved to a type" @gondor

@vinodborole
Copy link
Contributor

@jianke221 refer #676

@vinodborole
Copy link
Contributor

vinodborole commented Jul 11, 2016

@jianke221 your new code will be like

public class UploadClient {
    public static void main(String[] args) {
        ExecutorService executor = Executors.newFixedThreadPool(254);
        OSClientV3 os = OspSession.getClientV3(Client.MITAKA, Facing.PUBLIC);
       Token token= os.getToken();
        for(int i = 0;i<254;i++){
            String filename = String.valueOf(i)+".ts";
            UploadTask task = new UploadTask(os,filename,token);
            executor.execute(task);
        }  
        executor.shutdown();

        try{
                if (executor.awaitTermination(60, TimeUnit.SECONDS )) {
                    System.out.println( "Completed units of work successfully.");
                }else{
                    System.err.println( "Failed to execute in allowed time period." );
                }
            }catch ( InterruptedException e ) {
                e.printStackTrace( System.err );
            }
        System.exit( 0 );
    }
}
public class UploadTask implements Runnable{
    private OSClientV3 os;
    private String filename;
    public UploadTask(OSClientV3 os,String filename, Token token){
        this.filename = filename;
        os = OSFactory.clientFromToken(token);
    }
    public void run() {
        File f=new File("f:"+"/"+"9124e1bf-a4fe-4583-bff7-63530fbaff6d"+"/"+filename);
        String etag = os.objectStorage().objects().put("9124e1bf-a4fe-4583-bff7-63530fbaff6d", filename, Payloads.create(f));
        System.out.println("filename : "+etag);   
    }
}

@jianke221
Copy link
Author

i have some question in follow program:
public UploadTask(OSClientV3 os,String filename, Token token){
this.filename = filename;
os = OSFactory.clientFromToken(token);
}

but you hava OSClientV3 os as Parameter the token is needed? @vinodborole

@auhlig
Copy link
Member

auhlig commented Jul 11, 2016

@gondor You're right. Need to refer to the token instead of access. I'll fix the guide during the afternoon.

@jianke221 As @vinodborole already pointed out: The Access object exists only within the Identity v2 domain. In v3 please use Token token = is.getToken()

@auhlig
Copy link
Member

auhlig commented Jul 13, 2016

@jianke221: After updating the docs does this work for you?

@auhlig
Copy link
Member

auhlig commented Sep 18, 2016

Closing this as documentation has been updated.

@gaogaoSpark
Copy link

I have the same problem , if according to the official network Direct creation,no problem, but create by spring ,will happened,
OK:
public static OSClient.OSClientV3 getOSClient(){
return OSFactory.builderV3()
.endpoint("http://controller:5000/v3")
.credentials("0a1328426fa24ed9a6b2b4e79c7a6bbb", "abc")
.scopeToProject(Identifier.byId("c5ba590cab874f55b1668bad5cd2a6a6"))
.authenticate();
}
ERROR:Unable to retrieve current session. Please verify thread has a current session
@bean
public OSClient osClient(){
return OSFactory.builderV3()
.endpoint("http://controller:5000/v3")
.credentials("0a1328426fa24ed9a6b2b4e79c7a6bbb", "abc")
.scopeToProject(Identifier.byId("c5ba590cab874f55b1668bad5cd2a6a6"))
.authenticate();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants