1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-25 21:42:07 +01:00

[core] Introduce ScheduledAction.Kind for blocking and non-blocking actions

This commit is contained in:
Florian Schmaus 2020-05-16 21:18:27 +02:00
parent 4e5536e227
commit 72c5dc5886
4 changed files with 35 additions and 7 deletions

View file

@ -2163,7 +2163,7 @@ public abstract class AbstractXMPPConnection implements XMPPConnection {
}
protected static ScheduledAction schedule(Runnable runnable, long delay, TimeUnit unit) {
return SMACK_REACTOR.schedule(runnable, delay, unit);
return SMACK_REACTOR.schedule(runnable, delay, unit, ScheduledAction.Kind.NonBlocking);
}
protected void onStreamOpen(XmlPullParser parser) {

View file

@ -54,6 +54,14 @@ public abstract class Manager {
}
protected static final ScheduledAction schedule(Runnable runnable, long delay, TimeUnit unit) {
return AbstractXMPPConnection.SMACK_REACTOR.schedule(runnable, delay, unit);
return schedule(runnable, delay, unit, ScheduledAction.Kind.NonBlocking);
}
protected static final ScheduledAction scheduleBlocking(Runnable runnable, long delay, TimeUnit unit) {
return schedule(runnable, delay, unit, ScheduledAction.Kind.Blocking);
}
protected static final ScheduledAction schedule(Runnable runnable, long delay, TimeUnit unit, ScheduledAction.Kind scheduledActionKind) {
return AbstractXMPPConnection.SMACK_REACTOR.schedule(runnable, delay, unit, scheduledActionKind);
}
}

View file

@ -20,16 +20,25 @@ import java.util.Date;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;
import org.jivesoftware.smack.util.Async;
public class ScheduledAction implements Delayed {
final Runnable action;
enum Kind {
NonBlocking,
Blocking,
}
private final Runnable action;
final Date releaseTime;
final SmackReactor smackReactor;
final Kind kind;
ScheduledAction(Runnable action, Date releaseTime, SmackReactor smackReactor) {
ScheduledAction(Runnable action, Date releaseTime, SmackReactor smackReactor, Kind kind) {
this.action = action;
this.releaseTime = releaseTime;
this.smackReactor = smackReactor;
this.kind = kind;
}
/**
@ -68,4 +77,15 @@ public class ScheduledAction implements Delayed {
long delayInMillis = getTimeToDueMillis();
return unit.convert(delayInMillis, TimeUnit.MILLISECONDS);
}
void run() {
switch (kind) {
case NonBlocking:
action.run();
break;
case Blocking:
Async.go(() -> action.run());
break;
}
}
}

View file

@ -145,10 +145,10 @@ public class SmackReactor {
}
}
ScheduledAction schedule(Runnable runnable, long delay, TimeUnit unit) {
ScheduledAction schedule(Runnable runnable, long delay, TimeUnit unit, ScheduledAction.Kind scheduledActionKind) {
long releaseTimeEpoch = System.currentTimeMillis() + unit.toMillis(delay);
Date releaseTimeDate = new Date(releaseTimeEpoch);
ScheduledAction scheduledAction = new ScheduledAction(runnable, releaseTimeDate, this);
ScheduledAction scheduledAction = new ScheduledAction(runnable, releaseTimeDate, this, scheduledActionKind);
scheduledActions.add(scheduledAction);
selector.wakeup();
return scheduledAction;
@ -206,7 +206,7 @@ public class SmackReactor {
}
if (dueScheduledAction != null) {
dueScheduledAction.action.run();
dueScheduledAction.run();
return;
}