@Test( expected = CancellationException.class ) public void testExecuteCancelOk() throws Exception { final Task<Object> task = createMockTask( 500 ); InOrder inOrder = inOrder( this.mockTaskStatusListener ); Future<Object> future = this.service.execute( task ); sleep( 100 ); future.cancel( true ); sleep( 50 ); inOrder.verify( this.mockTaskStatusListener ).taskStarted( eq( task ) ); inOrder.verify( this.mockTaskStatusListener ).taskFailed( eq( task ), Matchers.<Exception> any() ); inOrder.verifyNoMoreInteractions(); assertTrue( future.isCancelled() ); assertTrue( future.isDone() ); assertNull( future.get() ); }
@Override public <RESULT_TYPE> Future<RESULT_TYPE> execute( final Task<RESULT_TYPE> aTask ) { if ( aTask == null ) { throw new IllegalArgumentException( "Parameter Task cannot be null!" ); } final Callable<RESULT_TYPE> task = new Callable<RESULT_TYPE>() { private final TaskStatusListener tsl = BackgroundTaskExecutionService.this.taskStatusListener; @Override public RESULT_TYPE call() throws Exception { this.tsl.taskStarted( aTask ); try { RESULT_TYPE result = aTask.call(); this.tsl.taskEnded( aTask, result ); return result; } catch ( Exception exception ) { LOG.log( Level.FINE, "Task execution failed!", exception ); this.tsl.taskFailed( aTask, exception ); throw exception; } } }; return this.executorService.submit( task ); }
BackgroundTaskExecutionService implements TaskExecutionService { @Override public <RESULT_TYPE> Future<RESULT_TYPE> execute( final Task<RESULT_TYPE> aTask ) { if ( aTask == null ) { throw new IllegalArgumentException( "Parameter Task cannot be null!" ); } final Callable<RESULT_TYPE> task = new Callable<RESULT_TYPE>() { private final TaskStatusListener tsl = BackgroundTaskExecutionService.this.taskStatusListener; @Override public RESULT_TYPE call() throws Exception { this.tsl.taskStarted( aTask ); try { RESULT_TYPE result = aTask.call(); this.tsl.taskEnded( aTask, result ); return result; } catch ( Exception exception ) { LOG.log( Level.FINE, "Task execution failed!", exception ); this.tsl.taskFailed( aTask, exception ); throw exception; } } }; return this.executorService.submit( task ); } }
BackgroundTaskExecutionService implements TaskExecutionService { @Override public <RESULT_TYPE> Future<RESULT_TYPE> execute( final Task<RESULT_TYPE> aTask ) { if ( aTask == null ) { throw new IllegalArgumentException( "Parameter Task cannot be null!" ); } final Callable<RESULT_TYPE> task = new Callable<RESULT_TYPE>() { private final TaskStatusListener tsl = BackgroundTaskExecutionService.this.taskStatusListener; @Override public RESULT_TYPE call() throws Exception { this.tsl.taskStarted( aTask ); try { RESULT_TYPE result = aTask.call(); this.tsl.taskEnded( aTask, result ); return result; } catch ( Exception exception ) { LOG.log( Level.FINE, "Task execution failed!", exception ); this.tsl.taskFailed( aTask, exception ); throw exception; } } }; return this.executorService.submit( task ); } BackgroundTaskExecutionService( final TaskStatusListener aTaskStatusListener ); }
BackgroundTaskExecutionService implements TaskExecutionService { @Override public <RESULT_TYPE> Future<RESULT_TYPE> execute( final Task<RESULT_TYPE> aTask ) { if ( aTask == null ) { throw new IllegalArgumentException( "Parameter Task cannot be null!" ); } final Callable<RESULT_TYPE> task = new Callable<RESULT_TYPE>() { private final TaskStatusListener tsl = BackgroundTaskExecutionService.this.taskStatusListener; @Override public RESULT_TYPE call() throws Exception { this.tsl.taskStarted( aTask ); try { RESULT_TYPE result = aTask.call(); this.tsl.taskEnded( aTask, result ); return result; } catch ( Exception exception ) { LOG.log( Level.FINE, "Task execution failed!", exception ); this.tsl.taskFailed( aTask, exception ); throw exception; } } }; return this.executorService.submit( task ); } BackgroundTaskExecutionService( final TaskStatusListener aTaskStatusListener ); void close(); @Override Future<RESULT_TYPE> execute( final Task<RESULT_TYPE> aTask ); }
BackgroundTaskExecutionService implements TaskExecutionService { @Override public <RESULT_TYPE> Future<RESULT_TYPE> execute( final Task<RESULT_TYPE> aTask ) { if ( aTask == null ) { throw new IllegalArgumentException( "Parameter Task cannot be null!" ); } final Callable<RESULT_TYPE> task = new Callable<RESULT_TYPE>() { private final TaskStatusListener tsl = BackgroundTaskExecutionService.this.taskStatusListener; @Override public RESULT_TYPE call() throws Exception { this.tsl.taskStarted( aTask ); try { RESULT_TYPE result = aTask.call(); this.tsl.taskEnded( aTask, result ); return result; } catch ( Exception exception ) { LOG.log( Level.FINE, "Task execution failed!", exception ); this.tsl.taskFailed( aTask, exception ); throw exception; } } }; return this.executorService.submit( task ); } BackgroundTaskExecutionService( final TaskStatusListener aTaskStatusListener ); void close(); @Override Future<RESULT_TYPE> execute( final Task<RESULT_TYPE> aTask );  }