From 11b5b6763c5edd2088dd73d50999f994c3adea6b Mon Sep 17 00:00:00 2001
From: srosse <none@none>
Date: Mon, 2 Jan 2012 14:45:56 +0100
Subject: [PATCH] OO-29: stop the backgroundthreadmanager

---
 .../olat/repository/_spring/repositoryContext.xml    |  9 ++-------
 .../repository/async/BackgroundTaskQueueManager.java | 12 ++++++++++++
 .../olat/repository/async/TaskExecutorThread.java    |  8 ++++++--
 3 files changed, 20 insertions(+), 9 deletions(-)

diff --git a/src/main/java/org/olat/repository/_spring/repositoryContext.xml b/src/main/java/org/olat/repository/_spring/repositoryContext.xml
index 7c9c4d276aa..7a20d1e7770 100644
--- a/src/main/java/org/olat/repository/_spring/repositoryContext.xml
+++ b/src/main/java/org/olat/repository/_spring/repositoryContext.xml
@@ -1,14 +1,9 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns="http://www.springframework.org/schema/beans"
 	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-	xmlns:context="http://www.springframework.org/schema/context" 
 	xsi:schemaLocation="
   http://www.springframework.org/schema/beans 
-  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
-  http://www.springframework.org/schema/context 
-  http://www.springframework.org/schema/context/spring-context-3.0.xsd">
-
-<context:property-placeholder location="classpath:serviceconfig/olat.properties, classpath:olat.local.properties" />
+  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 
 <bean id="repositoryModule" class="org.olat.repository.RepositoryModule" depends-on="userModule">
 
@@ -32,7 +27,7 @@
 </bean>
 
 <!-- BackgroundTaskQueueManager for increment launch-,download-counter and set LastUsage in background thread -->
-<bean id="org.olat.core.commons.persistence.async.BackgroundTaskQueueManager" class="org.olat.repository.async.BackgroundTaskQueueManager">
+<bean id="org.olat.core.commons.persistence.async.BackgroundTaskQueueManager" class="org.olat.repository.async.BackgroundTaskQueueManager" destroy-method="destroy">
 	<!-- Number of retries when a task failed e.g. incrementLaunchCounter try 20 times to increment when a exception occurs -->
 	<property name="maxRetry" value="20"/>
 </bean>	
diff --git a/src/main/java/org/olat/repository/async/BackgroundTaskQueueManager.java b/src/main/java/org/olat/repository/async/BackgroundTaskQueueManager.java
index 3a86f13c617..a50c83e9cd5 100644
--- a/src/main/java/org/olat/repository/async/BackgroundTaskQueueManager.java
+++ b/src/main/java/org/olat/repository/async/BackgroundTaskQueueManager.java
@@ -57,6 +57,18 @@ public class BackgroundTaskQueueManager  extends BasicManager {
 		taskExecutor.start();
 	}
 	
+	/**
+	 * [used by spring]
+	 */
+	public void destroy() {
+		taskExecutor.stopExecutor();
+		addTask(new BackgroundTask() {
+			@Override
+			public void execute() { /* */ }
+			@Override
+			public void setMaxRetry(int maxRetry) { /* */ }
+		});
+	}
 
 	public Queue<BackgroundTask> getQueue() {
 		return backgroundTaskQueue;
diff --git a/src/main/java/org/olat/repository/async/TaskExecutorThread.java b/src/main/java/org/olat/repository/async/TaskExecutorThread.java
index 3c00ae96b18..ac052645138 100644
--- a/src/main/java/org/olat/repository/async/TaskExecutorThread.java
+++ b/src/main/java/org/olat/repository/async/TaskExecutorThread.java
@@ -38,16 +38,20 @@ import org.olat.core.logging.Tracing;
  */
 public class TaskExecutorThread extends Thread {
 	private static OLog log = Tracing.createLoggerFor(TaskExecutorThread.class);
-	Queue<BackgroundTask> taskQueue;
+	private Queue<BackgroundTask> taskQueue;
+	private volatile boolean running = true;
 
 	public TaskExecutorThread(Queue<BackgroundTask> taskQueue) {
 		super("TaskExecutorThread");
 		this.taskQueue = taskQueue;
 	}
 	
+	public void stopExecutor() {
+		running = false;
+	}
+	
 	public void run() {
 		log.info("TaskExecutorThread started");
-		boolean running = true;
 		while(running) {
 			synchronized(taskQueue) {
 				if (taskQueue.size() == 0) {
-- 
GitLab