mirror of
https://gitee.com/jmix/cuba.git
synced 2024-12-01 10:47:37 +08:00
PL-7331 TaskLifeCycle.publish should throw InterruptedException
This commit is contained in:
parent
cacbab369a
commit
e7e0a88060
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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.
|
||||
*
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user