Asynchronous CMIS client: Download or upload several files in parallel with OpenCMIS

天涯浪子 提交于 2019-12-12 00:53:15

问题


The ChangeLog of OpenCMIS 0.14 says:

Support for asynchronous operations has been added to the client library.

As a CMIS client, how to perform several downloads in parallel, or several uploads in parallel, with OpenCMIS 0.14 or above?

My goal is to finish all operations faster, thanks to multithreading. I could share a Session object between several thread manually, but if OpenCMIS has a built-in feature I would rather use it.


回答1:


First, create a Session as usual.

Then, use this session to create an asynchronous session:

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

Then use this asyncSession as you would use a normal session object.

Mixing synchronous and asynchronous

Often you will want to perform some synchronous operations too. For instance, create a folder synchronously and then asynchronously upload files inside this folder. Because if you don't wait for the folder to be created, document upload will probably fail. Here is how to do in such cases:

// 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
);

Notice the asyncSession.createDocument construct above, it is because you can not write folder.createDocument as it would use the synchronous session.

The futureDocumentId variable will let you get the identifier of the document when you need it, if you need it:

ObjectId documentId = futureDocumentId.get();

Only call this method if you really need it, and call it as late as possible.



来源:https://stackoverflow.com/questions/42291411/asynchronous-cmis-client-download-or-upload-several-files-in-parallel-with-open

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!