Skip to content

Commit

Permalink
transition support for PAUSE deprecation
Browse files Browse the repository at this point in the history
  • Loading branch information
ar committed Sep 12, 2023
1 parent b91d37d commit bcba1e8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
38 changes: 30 additions & 8 deletions jpos/src/main/java/org/jpos/transaction/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Stream;

import static org.jpos.transaction.ContextConstants.*;
Expand All @@ -40,10 +42,9 @@ public class Context implements Externalizable, Loggeable, Cloneable, Pausable {
private transient Map<Object,Object> map; // transient map
private Map<Object,Object> pmap; // persistent (serializable) map
private transient boolean trace = false;
private final Semaphore paused = new Semaphore(1);
private CompletableFuture<Integer> pausedFuture;
private long timeout;

private final Lock lock = new ReentrantLock();
public Context () {
super ();
}
Expand Down Expand Up @@ -535,16 +536,37 @@ public void setTrace(boolean trace) {

@Override
public Future<Integer> pause() {
paused.acquireUninterruptibly();
pausedFuture = new CompletableFuture<>();
try {
lock.lock();
if (pausedFuture == null)
pausedFuture = new CompletableFuture<>();
else if (!pausedFuture.isDone())
throw new IllegalStateException("already paused");
} finally {
lock.unlock();
}
return pausedFuture;
}

@Override
public void resume(int result) {
pausedFuture.complete(result);
pausedFuture = null;
paused.release();
try {
lock.lock();
if (pausedFuture == null)
pausedFuture = new CompletableFuture<>();
pausedFuture.complete(result);
} finally {
lock.unlock();
}
}
@Override
public void reset () {
try {
lock.lock();
pausedFuture = null;
} finally {
lock.unlock();
}
}

@Override
Expand Down
2 changes: 2 additions & 0 deletions jpos/src/main/java/org/jpos/transaction/Pausable.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ public interface Pausable {
long getTimeout();
Future<Integer> pause();
void resume(int result);

void reset();
}
Original file line number Diff line number Diff line change
Expand Up @@ -1155,6 +1155,8 @@ private int pauseAndWait(Serializable context, int action) {
ctx.log(e);
} catch (TimeoutException e) {
action &= (PREPARED ^ 0xFFFF); // turn off 'PREPARED' - we need to abort
} finally {
pausable.reset();
}
} finally {
pausedSessions.decrementAndGet();
Expand Down

0 comments on commit bcba1e8

Please sign in to comment.