Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package org.apache.marmotta.platform.core.services.task;
 
 import java.util.Date;
 import java.util.List;
 import java.util.Map;
 
 
 
 
 public class TaskManagerServiceImpl implements TaskManagerService {
 
     private static final String  DEFAULT_GROUP = "default";
 
     private final ConcurrentMap<ThreadStack<TaskImpl>> tasks;
 
     private final ThreadWatchdog watchdog;
 
     @Inject
     private Logger log;
 
     @Inject
     public TaskManagerServiceImpl() {
          = new MapMaker().makeMap();
          = new ThreadWatchdog(15000);
     }
 
     @PostConstruct
     public void startup() {
         .start();
     }
 
     /* (non-Javadoc)
      * @see xx.test.taks.TaskManagerService#endTask(xx.test.taks.TaskInfo)
      */
     @Override
     public void endTask(TaskInfo task) {
         final Stack<TaskImplstack = getStack();
         if (stack.contains(task)) {
             while (!stack.isEmpty()) {
                 Task t = stack.pop();
                 if (t.equals(task)) {
                     .debug("Ending task {}.{}"t.getGroup(), t.getName());
                     break;
                 } else {
                     .debug("Ending task {}.{} because it's a sub-task"t.getGroup(), t.getName());
                     t.endTask();
                 }
             }
             if (!stack.isEmpty()) {
                 stack.peek().subTaskEnded();
             }
         } else {
             .debug("Not ending task {} because thread {} is not the owner"task.getName(), Thread.currentThread().getName());
         }
         if (stack.isEmpty()) {
             .remove(Thread.currentThread());
         }
     }
 
     /* (non-Javadoc)
      * @see xx.test.taks.TaskManagerService#getTasks()
      */
     @Override
     public List<TaskInfogetTasks() {
        LinkedList<TaskInfots = new LinkedList<TaskInfo>();
        for (Stack<TaskImplstack : .values()) {
            ts.addAll(stack);
        }
        return Collections.unmodifiableList(ts);
    }
    /* (non-Javadoc)
     * @see xx.test.taks.TaskManagerService#getTask()
     */
    @Override
    public Task getTask() {
        return createTask(Thread.currentThread().getName());
    }
    /* (non-Javadoc)
     * @see xx.test.taks.TaskManagerService#createTask(java.lang.String)
     */
    @Override
    public Task createTask(String nameString group) {
        final Stack<TaskImplstack = getStack();
        final Task task;
        if (stack.isEmpty())
            return createSubTask(namegroup);
        else {
            task = stack.peek();
            task.updateName(name);
            if (group != null) {
                task.updateGroup(group);
            }
        }
        return task;
    }
    @Override
    public Task createTask(String string) {
        return createTask(stringnull);
    }
    /* (non-Javadoc)
     * @see xx.test.taks.TaskManagerService#createSubTask(java.lang.String)
     */
    @Override
    public Task createSubTask(String name) {
        return createSubTask(namenull);
    }
    @Override
    public Task createSubTask(String nameString group) {
        final Stack<TaskImplstack = getStack();
        final TaskImpl task;
        synchronized (stack) {
            task = new TaskImpl(thiscreateUUID(), namegroup != null ? group : );
            if (!stack.isEmpty()) {
                stack.peek().subTastStarting(task);
            }
            stack.push(task);
        }
        return task;
    }
    private String createUUID() {
        final Thread t = Thread.currentThread();
        String string = String.format("%d: %s %tR"t.getId(), t.getName(), new Date());
        return HashUtils.md5sum(string);
    }
    private Stack<TaskImplgetStack() {
        final Thread key = Thread.currentThread();
        Stack<TaskImplstack = .get(key);
        if (stack == null) {
            stack = new Stack<TaskImpl>();
            .put(keystack);
        }
        return stack;
    }
    /* (non-Javadoc)
     * @see xx.test.taks.TaskManagerService#getTasksByGroup()
     */
    @Override
    public Map<StringList<TaskInfo>> getTasksByGroup() {
        Map<StringList<TaskInfo>> result = new LinkedHashMap<StringList<TaskInfo>>();
        for (TaskInfo task : getTasks()) {
            final String group = task.getGroup();
            List<TaskInfolist = result.get(group);
            if (list == null) {
                list = new LinkedList<TaskInfo>();
                result.put(grouplist);
            }
            list.add(task);
        }
        return result;
    }
    /* (non-Javadoc)
     * @see xx.test.taks.TaskManagerService#getTasksByThread()
     */
    @Override
        for (Map.Entry<ThreadStack<TaskImpl>> e : .entrySet()) {
            Stack<TaskInfolist = new Stack<TaskInfo>();
            list.addAll(e.getValue());
            result.put(new WeakReference<Thread>(e.getKey()), list);
        }
        return result;
    }
    private class ThreadWatchdog extends Thread {
        private final long millis;
        private boolean running;
        public ThreadWatchdog(long millis) {
            super("TaskThreadWatchdog");
            this. = millis;
        }
        @Override
        public void run() {
             = true;
            .trace("Watchdog: starting up");
            final Task task = getTask();
            int count = 0;
            while () {
                task.updateMessage("cleaning up");
                task.updateProgress(++count);
                try {
                    HashSet<Threaddead = new HashSet<Thread>();
                    for (Thread t : .keySet()) {
                        if (!t.isAlive()) {
                            dead.add(t);
                        }
                    }
                    for (Thread t : dead) {
                        .debug("Watchdog: cleaning up dead thread " + t.getName());
                        .remove(t);
                        t = null;
                    }
                    dead.clear();
                    dead = null;
                    task.updateMessage("sleeping");
                    synchronized (ThreadWatchdog.this) {
                        this.wait();
                    }
                } catch (InterruptedException e) {
                    // ignore
                }
            }
            task.endTask();
            .trace("Watchdog: shutdown");
        }
        public void shutdown() {
             = false;
            synchronized (this) {
                this.notify();
            }
        }
    }
    @PreDestroy
    public void shutdown() {
        .shutdown();
    }
New to GrepCode? Check out our FAQ X