Skip to content

Commit c1ba3e2

Browse files
committed
PERF : Remove correctly UIRunnables when closing UIContext
1 parent dc465b6 commit c1ba3e2

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

ponysdk/src/main/java/com/ponysdk/core/server/application/UIContext.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,7 @@ public void onDestroy() {
626626
acquire();
627627
try {
628628
doDestroy();
629+
context.deregisterUIContext(ID);
629630
} finally {
630631
release();
631632
}
@@ -640,16 +641,7 @@ void destroyFromApplication() {
640641
if (!isAlive()) return;
641642
acquire();
642643
try {
643-
alive = false;
644-
destroyListeners.forEach(listener -> {
645-
try {
646-
listener.onBeforeDestroy(this);
647-
} catch (final AlreadyDestroyedApplication e) {
648-
if (log.isDebugEnabled()) log.debug("Exception while destroying UIContext #" + getID(), e);
649-
} catch (final Exception e) {
650-
log.error("Exception while destroying UIContext #" + getID(), e);
651-
}
652-
});
644+
doDestroy();
653645
socket.close();
654646
} finally {
655647
release();
@@ -666,6 +658,7 @@ public void destroy() {
666658
acquire();
667659
try {
668660
doDestroy();
661+
context.deregisterUIContext(ID);
669662
socket.close();
670663
} finally {
671664
release();
@@ -686,7 +679,6 @@ private void doDestroy() {
686679
log.error("Exception while destroying UIContext #" + getID(), e);
687680
}
688681
});
689-
context.deregisterUIContext(ID);
690682
}
691683

692684
/**

ponysdk/src/main/java/com/ponysdk/core/server/concurrent/PScheduler.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,19 +149,26 @@ private <R> Consumer<R> delegate0(final Consumer<R> callback, final UIContext ui
149149
return new UIDelegator<>(callback, uiContext);
150150
}
151151

152+
private void destroy(final UIContext uiContext) {
153+
final Set<UIRunnable> uiRunnables = runnablesByUIContexts.remove(uiContext);
154+
if (uiRunnables != null) uiRunnables.forEach(UIRunnable::onCancel);
155+
executor.purge();
156+
}
157+
152158
private void purge(final UIRunnable uiRunnable) {
159+
final Set<UIRunnable> uiRunnables = runnablesByUIContexts.get(uiRunnable.getUIContext());
160+
if (uiRunnables != null) uiRunnables.remove(uiRunnable);
153161
executor.purge();
154-
runnablesByUIContexts.get(uiRunnable.getUiContext()).remove(uiRunnable);
155162
}
156163

157164
private void registerTask(final UIRunnable runnable) {
158-
final UIContext uiContext = runnable.getUiContext();
165+
final UIContext uiContext = runnable.getUIContext();
159166
Set<UIRunnable> runnables = runnablesByUIContexts.get(uiContext);
160167
if (runnables == null) {
161168
runnables = Collections.newSetFromMap(new ConcurrentHashMap<>());
162169
runnables.add(runnable);
163170
runnablesByUIContexts.put(uiContext, runnables);
164-
uiContext.addContextDestroyListener(context -> runnablesByUIContexts.remove(context).forEach(UIRunnable::cancel));
171+
uiContext.addContextDestroyListener(this::destroy);
165172
} else {
166173
runnables.add(runnable);
167174
}
@@ -192,9 +199,7 @@ public void run() {
192199
log.error("Error occurred", throwable);
193200
cancel();
194201
} finally {
195-
if (!repeated) {
196-
scheduler.purge(this);
197-
}
202+
if (!repeated) scheduler.purge(this);
198203
}
199204
}
200205

@@ -203,16 +208,20 @@ public boolean execute() {
203208
}
204209

205210
public void cancel() {
211+
onCancel();
212+
scheduler.purge(this);
213+
}
214+
215+
public void onCancel() {
206216
this.cancelled = true;
207217
this.future.cancel(false);
208-
scheduler.purge(this);
209218
}
210219

211220
void setFuture(final ScheduledFuture<?> future) {
212221
this.future = future;
213222
}
214223

215-
public UIContext getUiContext() {
224+
public UIContext getUIContext() {
216225
return uiContext;
217226
}
218227
}

0 commit comments

Comments
 (0)