Skip to content

Commit

Permalink
WW-5414 Always call afterInvocation even in case of exception
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszlenart committed May 11, 2024
1 parent 649760d commit 0141068
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.Serializable;

Expand All @@ -30,6 +32,8 @@ public class StrutsBackgroundProcess implements BackgroundProcess, Serializable

private static final long serialVersionUID = 3884464776311686443L;

private static final Logger LOG = LogManager.getLogger(StrutsBackgroundProcess.class);

private final String threadName;
private final int threadPriority;

Expand All @@ -44,8 +48,8 @@ public class StrutsBackgroundProcess implements BackgroundProcess, Serializable
/**
* Constructs a background process
*
* @param invocation The action invocation
* @param threadName The name of background thread
* @param invocation The action invocation
* @param threadName The name of background thread
* @param threadPriority The priority of background thread
*/
public StrutsBackgroundProcess(ActionInvocation invocation, String threadName, int threadPriority) {
Expand All @@ -61,11 +65,15 @@ public BackgroundProcess prepare() {
try {
beforeInvocation();
result = invocation.invokeActionOnly();
afterInvocation();
} catch (Exception e) {
try {
afterInvocation();
} catch (Exception ex) {
LOG.warn("Exception during afterInvocation() execution", ex);
}
exception = e;
} finally {
done = true;
done = true;
}
});
processThread.setName(threadName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,19 @@ public void testMultipleProcesses() throws InterruptedException {
assertEquals(100, mutableState.get());
}

public void testErrorableProcesses() throws InterruptedException {
MockActionInvocationWithActionInvoker invocation = new MockActionInvocationWithActionInvoker(() -> {
throw new IllegalStateException("boom");
});

BackgroundProcess bp = new ErrorableBackgroundProcess(invocation, "error").prepare();
executor.execute(bp);

Thread.sleep(100);

assertTrue("afterInvocation not called in case of exception", ((ErrorableBackgroundProcess) bp).isDoneAfter());
}

public void testUnpreparedProcess() throws ExecutionException, InterruptedException, TimeoutException {
// given
MockActionInvocationWithActionInvoker invocation = new MockActionInvocationWithActionInvoker(() -> "done");
Expand Down Expand Up @@ -177,3 +190,22 @@ protected void afterInvocation() throws Exception {
lock.notify();
}
}

class ErrorableBackgroundProcess extends StrutsBackgroundProcess {

private boolean doneAfter;

public ErrorableBackgroundProcess(ActionInvocation invocation, String name) {
super(invocation, name, Thread.NORM_PRIORITY);
}

@Override
protected void afterInvocation() throws Exception {
super.afterInvocation();
doneAfter = true;
}

public boolean isDoneAfter() {
return doneAfter;
}
}

0 comments on commit 0141068

Please sign in to comment.