[]
        
(Showing Draft Content)

CancellationTokenSource

Class CancellationTokenSource

java.lang.Object
com.grapecity.documents.excel.CancellationTokenSource
All Implemented Interfaces:
AutoCloseable

public class CancellationTokenSource extends Object implements AutoCloseable
Allows you to send cancellation requests to one or more operations.

To use a CancellationTokenSource:

  1. Create a token source.
  2. Attach the token to one or more operations by passing the return value of getToken() as a parameter.
  3. Later, cancel the associated operations by calling cancel() on this token source.

This class implements AutoCloseable and should be used with try-with-resources to ensure proper cleanup of internal resources.


 try (CancellationTokenSource cts = new CancellationTokenSource()) {
     cts.cancelAfter(Duration.ofSeconds(10));
     CancellationToken token = cts.getToken();
     workbook.processTemplate(token);
 } catch (Exception e) { }
 
  • Constructor Details

    • CancellationTokenSource

      public CancellationTokenSource()
  • Method Details

    • cancel

      public void cancel()
      Initiates cancel request. All operations that have been associated with this token will be cancelled.

      It is assumed that the consumers of getToken() will do 'best-effort' attempt to perform cancellation. This method returns immediately and if the cancellation is successful the cancelled operation will throw CancellationException.

      
       try (CancellationTokenSource cts = new CancellationTokenSource()) {
           CancellationToken token = cts.getToken();
           cts.cancel();
           boolean cancellationRequested = token.isCancellationRequested();
       }
       
    • cancelAfter

      public void cancelAfter(Duration delay)
      Schedules a cancel operation on this CancellationTokenSource after the specified time span.
      
       try (CancellationTokenSource cts = new CancellationTokenSource()) {
           cts.cancelAfter(Duration.ZERO);
           CancellationToken token = cts.getToken();
       }
       
      Parameters:
      delay - The time span to wait before canceling this CancellationTokenSource. This value can't be null.
    • getToken

      public CancellationToken getToken()
      Returns a token associated with this CancellationTokenSource.
      
       try (CancellationTokenSource cts = new CancellationTokenSource()) {
           CancellationToken token = cts.getToken();
       }
       
      Returns:
      The cancellation token. Always returns the same instance.
    • close

      public void close() throws Exception
      Closes the resource and performs cleanup operations.
      
       CancellationTokenSource cts = new CancellationTokenSource();
       try {
           cts.cancelAfter(Duration.ofSeconds(10));
           workbook.processTemplate(cts.getToken());
       } finally {
           cts.close();
       }
       
      Specified by:
      close in interface AutoCloseable
      Throws:
      Exception - if cleanup fails.