2017-02-17 28 views

回答

2

首先,create a Session照常。

然後,使用這個會話創建一個asynchronous session

int maxParallelRequests = 10; 
AsyncSessionFactory asyncFactory = AsyncSessionFactoryImpl.newInstance(); 
asyncSession = asyncFactory.createAsyncSession(session, maxParallelRequests); 

然後用這個asyncSession,你會使用一個正常的會話對象。

混合同步和異步

通常你需要執行一些同步操作了。例如,同步創建一個文件夾,然後在該文件夾內異步上傳文件。因爲如果你不等待文件夾被創建,文件上傳可能會失敗。下面是如何在這種情況下做的:

// Create the folder synchronously. 
Folder folder = session.getRootFolder().createFolder(properties); 

// Upload the file asynchronously. 
Future<ObjectId> futureDocumentId = asyncSession.createDocument(
    properties, 
    new ObjectIdImpl(remoteFolder.getId()), 
    contentStream, 
    VersioningState.MAJOR 
); 

通知的asyncSession.createDocument結構上面,那是因爲你不能寫folder.createDocument,因爲它會使用同步會話。

的futureDocumentId變量會讓你獲取文檔的標識,當你需要它,如果你需要它:

ObjectId documentId = futureDocumentId.get(); 

只有調用此方法,如果你真的需要它,並儘可能晚地稱它爲。