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.Work.State; 034import org.nuxeo.ecm.core.work.api.WorkManager; 035import org.nuxeo.ecm.core.work.api.WorkQueueMetrics; 036import org.nuxeo.runtime.api.Framework; 037 038/** 039 * Seam bean that wraps the {@link WorkManager} service to provide a JSF admin UI. 040 */ 041@Name("workManagerAdmin") 042@Scope(ScopeType.PAGE) 043public class WorkManagerAdminBean implements Serializable { 044 045 private static final long serialVersionUID = 1L; 046 047 @RequestParameter("queueId") 048 protected String queueId; 049 050 protected WorkManager getWorkManager() { 051 return Framework.getLocalService(WorkManager.class); 052 } 053 054 public List<Map<String, Object>> getWorkQueuesInfo() { 055 List<Map<String, Object>> info = new ArrayList<Map<String, Object>>(); 056 WorkManager workManager = getWorkManager(); 057 List<String> workQueueIds = workManager.getWorkQueueIds(); 058 Collections.sort(workQueueIds); 059 for (String queueId : workQueueIds) { 060 WorkQueueMetrics metrics = workManager.getMetrics(queueId); 061 062 Map<String, Object> map = new HashMap<String, Object>(); 063 map.put("id", queueId); 064 map.put("scheduled", metrics.scheduled); 065 map.put("completed", metrics.completed); 066 map.put("running", metrics.running); 067 map.put("runningWorks", workManager.listWork(queueId, State.RUNNING)); 068 info.add(map); 069 } 070 return info; 071 } 072 073 public long getCurrentTimeMillis() { 074 return System.currentTimeMillis(); 075 } 076 077 public String startTestWork() { 078 getWorkManager().schedule(new SleepWork(10000)); 079 return null; 080 } 081 082}