Skip to content

Commit

Permalink
Added logic to register a destroy lister to do cleanup when undeployi…
Browse files Browse the repository at this point in the history
…ng the webapp (e.g. remove running threads)
  • Loading branch information
Willem Elbers committed Oct 3, 2024
1 parent ac6745a commit 8b7709f
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

public interface VirtualCollectionRegistry {

void registerDestroyListener(VirtualCollectionRegistryDestroyListener listener);

/**
* Will store the specified collection; it will also set the owner according
* to the specified principal and set its state to
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (C) 2024 CLARIN
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package eu.clarin.cmdi.virtualcollectionregistry;

/**
*
* @author wilelb
*/
public interface VirtualCollectionRegistryDestroyListener {
public void handleDestroy();
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.security.Principal;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
Expand Down Expand Up @@ -52,6 +53,8 @@ public class VirtualCollectionRegistryImpl implements VirtualCollectionRegistry,
@Autowired
private CreatorService creatorService;

private final List<VirtualCollectionRegistryDestroyListener> destroyListeners = new LinkedList<>();

private static final Logger logger
= LoggerFactory.getLogger(VirtualCollectionRegistryImpl.class);

Expand All @@ -65,6 +68,11 @@ public class VirtualCollectionRegistryImpl implements VirtualCollectionRegistry,
private final ScheduledExecutorService maintenanceExecutor
= createSingleThreadScheduledExecutor("VirtualCollectionRegistry-Maintenance");

@Override
public void registerDestroyListener(VirtualCollectionRegistryDestroyListener listener) {
this.destroyListeners.add(listener);
}

@Override
public void afterPropertiesSet() throws VirtualCollectionRegistryException {
// called by Spring directly after Bean construction
Expand Down Expand Up @@ -113,6 +121,11 @@ public void run() {

@Override
public void destroy() throws VirtualCollectionRegistryException, InterruptedException {
logger.info("Stopping Virtual Collection Registry maintenance schedule");
for(VirtualCollectionRegistryDestroyListener listener : this.destroyListeners) {
listener.handleDestroy();
}

logger.info("Stopping Virtual Collection Registry maintenance schedule");
maintenanceExecutor.shutdown();
if (!maintenanceExecutor.awaitTermination(60, TimeUnit.SECONDS)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package eu.clarin.cmdi.virtualcollectionregistry.gui.pages.crud.v2;

import eu.clarin.cmdi.virtualcollectionregistry.VirtualCollectionRegistry;
import eu.clarin.cmdi.virtualcollectionregistry.VirtualCollectionRegistryDestroyListener;
import eu.clarin.cmdi.virtualcollectionregistry.VirtualCollectionRegistryException;
import eu.clarin.cmdi.virtualcollectionregistry.gui.Application;
import eu.clarin.cmdi.virtualcollectionregistry.gui.pages.BasePage;
Expand Down Expand Up @@ -77,7 +78,8 @@ public CreateAndEditVirtualCollectionPageV2(Long id, final Page previousPage) th
vc = vcr.retrieveVirtualCollection(id);
if (vc != null) {
this.checkAccess(vc);
}
logger.info("Retrieved collection: {}", vc.toString());
}
}

if(vc == null) {
Expand Down Expand Up @@ -165,7 +167,16 @@ public void handleEvent(Event<VirtualCollection> event) {
});
add(crud);

vcr.registerDestroyListener(new VirtualCollectionRegistryDestroyListener() {
@Override
public void handleDestroy() {
logger.info("Destroying CreateAndEditVirtualCollectionPageV2");
crud.handleDestroy();
}
});

if(vc != null) {
logger.info("Editing collection: {}", vc.toString());
crud.editCollection(vc);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import eu.clarin.cmdi.virtualcollectionregistry.model.GeneratedByQuery;
import eu.clarin.cmdi.virtualcollectionregistry.model.VirtualCollection;
import java.security.Principal;
import javax.print.attribute.standard.JobState;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
Expand Down Expand Up @@ -357,6 +358,10 @@ public void onUpdate(AjaxRequestTarget target) {
validate();
}

public void handleDestroy() {
referencesEditor.reset();
}

private void toggleHelpMode() {
boolean showHelp = toggleHelpModeModel.getObject();
for(AbstractField f: fields) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ public ReferencesEditor(String id, String label, Model<Boolean> advancedEditorMo
final WebMarkupContainer ajaxWrapper = new WebMarkupContainer("ajaxwrapper");
ajaxWrapper.setOutputMarkupId(true);


localDialog = new ModalConfirmDialog("references_modal");
localDialog.addListener(new Listener() {
@Override
Expand Down

0 comments on commit 8b7709f

Please sign in to comment.