001/*
002 * (C) Copyright 2012 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Florent Guillaume
018 */
019package org.nuxeo.ecm.admin.work;
020
021import java.io.Serializable;
022import java.util.ArrayList;
023import java.util.Collections;
024import java.util.HashMap;
025import java.util.List;
026import java.util.Map;
027
028import org.jboss.seam.ScopeType;
029import org.jboss.seam.annotations.Name;
030import org.jboss.seam.annotations.Scope;
031import org.jboss.seam.annotations.web.RequestParameter;
032import org.nuxeo.ecm.core.work.SleepWork;
033import org.nuxeo.ecm.core.work.api.WorkManager;
034import org.nuxeo.ecm.core.work.api.WorkQueueMetrics;
035import org.nuxeo.runtime.api.Framework;
036
037/**
038 * Seam bean that wraps the {@link WorkManager} service to provide a JSF admin UI.
039 */
040@Name("workManagerAdmin")
041@Scope(ScopeType.PAGE)
042public class WorkManagerAdminBean implements Serializable {
043
044    private static final long serialVersionUID = 1L;
045
046    @RequestParameter("queueId")
047    protected String queueId;
048
049    protected WorkManager getWorkManager() {
050        return Framework.getService(WorkManager.class);
051    }
052
053    public List<Map<String, Object>> getWorkQueuesInfo() {
054        List<Map<String, Object>> info = new ArrayList<>();
055        WorkManager workManager = getWorkManager();
056        List<String> workQueueIds = workManager.getWorkQueueIds();
057        Collections.sort(workQueueIds);
058        for (String queueId : workQueueIds) {
059            WorkQueueMetrics metrics = workManager.getMetrics(queueId);
060
061            Map<String, Object> map = new HashMap<>();
062            map.put("id", queueId);
063            map.put("scheduled", metrics.scheduled);
064            map.put("completed", metrics.completed);
065            map.put("running", metrics.running);
066            info.add(map);
067        }
068        return info;
069    }
070
071    public long getCurrentTimeMillis() {
072        return System.currentTimeMillis();
073    }
074
075    public String startTestWork() {
076        getWorkManager().schedule(new SleepWork(10000));
077        return null;
078    }
079
080}