Skip to content

Commit

Permalink
add soft-stop support
Browse files Browse the repository at this point in the history
  • Loading branch information
ar committed Feb 17, 2025
1 parent 59513da commit f6ff427
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions jpos/src/main/java/org/jpos/q2/iso/ChannelAdaptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.jdom2.Element;
import org.jpos.core.ConfigurationException;
import org.jpos.core.Environment;
import org.jpos.core.annotation.Config;
import org.jpos.core.handlers.exception.ExceptionHandlerAware;
import org.jpos.core.handlers.exception.ExceptionHandlerConfigAware;
import org.jpos.iso.*;
Expand All @@ -43,9 +44,11 @@
import java.io.IOException;
import java.io.PrintStream;
import java.net.SocketTimeoutException;
import java.time.Duration;
import java.time.Instant;
import java.time.ZoneId;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
Expand Down Expand Up @@ -77,12 +80,15 @@ public class ChannelAdaptor

private Counter msgOutCounter;
private Counter msgInCounter;
@Config("soft-stop") private long softStop;

public ChannelAdaptor () {
super ();
resetCounters();
}
public void initService() throws ConfigurationException {
if (softStop < 0)
throw new ConfigurationException ("Invalid soft-stop %d".formatted(Long.valueOf(softStop)));
initSpaceAndQueues();
NameRegistrar.register (getName(), this);
executor = QFactory.executorService(cfg.getBoolean("virtual-threads", false));
Expand Down Expand Up @@ -291,7 +297,8 @@ public Sender () {
}
public void run () {
Thread.currentThread().setName ("channel-sender-" + in);
while (running ()){

while (running()){
try {
checkConnection ();
if (!running())
Expand Down Expand Up @@ -328,7 +335,25 @@ public Receiver () {
}
public void run () {
Thread.currentThread().setName ("channel-receiver-"+out);
while (running()) {
boolean shuttingDown = false;
Instant shutdownDeadline = null;
final Duration gracePeriod = Duration.ofMillis(softStop);
while (true) {
if (!shuttingDown && !running()) {
if (gracePeriod.isZero())
break;
shuttingDown = true;
shutdownDeadline = Instant.now().plus(gracePeriod);
getLog().info("soft-stop (%s)".formatted(shutdownDeadline.atZone(ZoneId.systemDefault())));
}
final boolean shouldExit = shuttingDown
? Instant.now().isAfter(shutdownDeadline)
: !running();

if (shouldExit) {
getLog().info ("stop");
break;
}
try {
Object r = sp.rd (ready, 5000L);
if (r == null) {
Expand Down

0 comments on commit f6ff427

Please sign in to comment.