PL-7331 TaskLifeCycle.publish should throw InterruptedException

This commit is contained in:
Yuriy Artamonov 2016-07-11 18:30:53 +04:00
parent cacbab369a
commit e7e0a88060
6 changed files with 45 additions and 34 deletions

View File

@ -120,14 +120,19 @@ public class DesktopBackgroundWorker implements BackgroundWorker {
@Override
protected final V doInBackground() throws Exception {
Thread.currentThread().setName("BackgroundTaskThread");
Thread.currentThread().setName(String.format("BackgroundTaskThread-%s",
System.identityHashCode(Thread.currentThread())));
try {
if (!Thread.currentThread().isInterrupted()) {
// do not run any activity if canceled before start
result = runnableTask.run(new TaskLifeCycle<T>() {
@SafeVarargs
@Override
public final void publish(T... changes) {
public final void publish(T... changes) throws InterruptedException {
if (Thread.currentThread().isInterrupted()) {
throw new InterruptedException("Task is interrupted and is trying to publish changes");
}
handleProgress(changes);
}

View File

@ -25,17 +25,16 @@ import java.util.Map;
* interact with the execution environment.
*
* @param <T> task progress measurement unit
*
*/
public interface TaskLifeCycle<T> {
/**
* Publish changes to show progress.
*
* @param changes Changes
* @throws InterruptedException if task was interrupted by calling {@link BackgroundTaskHandler#cancel()}
*/
@SuppressWarnings({"unchecked"})
void publish(T... changes);
void publish(T... changes) throws InterruptedException;
/**
* @return true if the working thread has been interrupted

View File

@ -22,47 +22,51 @@ import org.apache.http.entity.FileEntity;
import java.io.*;
import java.util.UUID;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.haulmont.bali.util.Preconditions.checkNotNullArgument;
public class FileStorageProgressEntity extends FileEntity {
private final long size;
private final UUID fileId;
private final FileUploadingAPI.UploadToStorageProgressListener listener;
private final FileUploading.UploadToStorageProgressListener listener;
public FileStorageProgressEntity(File file, String contentType, UUID fileId,
FileUploadingAPI.UploadToStorageProgressListener listener) {
FileUploading.UploadToStorageProgressListener listener) {
super(file, contentType);
checkNotNullArgument(listener);
checkNotNullArgument(fileId);
this.listener = listener;
this.size = file.length();
this.fileId = fileId;
checkNotNull(listener);
checkNotNull(fileId);
}
@Override
public void writeTo(OutputStream outstream) throws IOException {
public void writeTo(OutputStream outStream) throws IOException {
long transferredBytes = 0L;
if (outstream == null) {
if (outStream == null) {
throw new IllegalArgumentException("Output stream may not be null");
}
try (InputStream instream = new FileInputStream(this.file)) {
try (InputStream inStream = new FileInputStream(this.file)) {
byte[] tmp = new byte[4096];
int readedBytes;
while ((readedBytes = instream.read(tmp)) != -1) {
while ((readedBytes = inStream.read(tmp)) != -1) {
if (Thread.currentThread().isInterrupted())
throw new InterruptedIOException();
outstream.write(tmp, 0, readedBytes);
outStream.write(tmp, 0, readedBytes);
transferredBytes += readedBytes;
try {
listener.progressChanged(fileId, transferredBytes, size);
} catch (InterruptedException e) {
throw new InterruptedIOException();
}
outstream.flush();
}
outStream.flush();
}
}
}

View File

@ -351,7 +351,7 @@ public class FileUploading implements FileUploadingAPI, FileUploadingMBean {
UploadToStorageProgressListener progressListener = new UploadToStorageProgressListener() {
@Override
public void progressChanged(UUID fileId, long uploadedBytes, long totalBytes) {
public void progressChanged(UUID fileId, long uploadedBytes, long totalBytes) throws InterruptedException {
taskLifeCycle.publish(uploadedBytes);
}
};
@ -401,4 +401,17 @@ public class FileUploading implements FileUploadingAPI, FileUploadingMBean {
}
return builder.toString();
}
/**
* Listener to be notified about the progress of uploading file from the temporary storage
* into middleware FileStorage.
*/
interface UploadToStorageProgressListener {
/**
* @param fileId temporary file ID
* @param uploadedBytes current uploaded bytes count
* @param totalBytes total contents size
*/
void progressChanged(UUID fileId, long uploadedBytes, long totalBytes) throws InterruptedException;
}
}

View File

@ -28,7 +28,6 @@ import java.util.UUID;
/**
* Client API for uploading files and transfer them to the middleware FileStorage.
*
*/
public interface FileUploadingAPI {
@ -45,19 +44,6 @@ public interface FileUploadingAPI {
void progressChanged(UUID fileId, int receivedBytes);
}
/**
* Listener to be notified about the progress of uploading file from the temporary storage
* into middleware FileStorage.
*/
interface UploadToStorageProgressListener {
/**
* @param fileId temporary file ID
* @param uploadedBytes current uploaded bytes count
* @param totalBytes total contents size
*/
void progressChanged(UUID fileId, long uploadedBytes, long totalBytes);
}
/**
* Store the byte array in a new temporary file.
*

View File

@ -228,7 +228,11 @@ public class WebBackgroundWorker implements BackgroundWorker {
return runnableTask.run(new TaskLifeCycle<T>() {
@SafeVarargs
@Override
public final void publish(T... changes) {
public final void publish(T... changes) throws InterruptedException {
if (Thread.currentThread().isInterrupted()) {
throw new InterruptedException("Task is interrupted and is trying to publish changes");
}
handleProgress(changes);
}