Asynchronous CMIS client: Download or upload sever

2019-09-07 08:06发布

问题:

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.