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.core.work; 020 021/** 022 * Simple work that just sleeps, mostly used for tests. 023 */ 024public class SleepWork extends AbstractWork { 025 026 private static final long serialVersionUID = 1L; 027 028 protected long durationMillis; 029 030 protected String category; 031 032 /** 033 * Creates a work instance that does nothing but sleep. 034 * 035 * @param durationMillis the sleep duration 036 */ 037 public SleepWork(long durationMillis) { 038 this(durationMillis, "SleepWork", false); 039 } 040 041 /** 042 * If debug is true, then the various debug* methods must be called in the proper order for the work to start and 043 * stop: {@link #debugStart}, {@link #debugFinish}. 044 * 045 * @param durationMillis the sleep duration 046 * @param debug {@code true} for debug 047 */ 048 public SleepWork(long durationMillis, boolean debug) { 049 this(durationMillis, "SleepWork", debug); 050 } 051 052 public SleepWork(long durationMillis, boolean debug, String id) { 053 this(durationMillis, "SleepWork", debug, id); 054 } 055 056 public SleepWork(long durationMillis, String category, boolean debug) { 057 super(); 058 init(durationMillis, category, debug); 059 } 060 061 public SleepWork(long durationMillis, String category, boolean debug, String id) { 062 super(id); 063 init(durationMillis, category, debug); 064 } 065 066 private void init(long durationMillis, String category, boolean debug) { 067 this.durationMillis = durationMillis; 068 this.category = category; 069 setProgress(Progress.PROGRESS_0_PC); 070 } 071 072 @Override 073 public String getCategory() { 074 return category; 075 } 076 077 @Override 078 public String getTitle() { 079 return "Sleep " + durationMillis + " ms"; 080 } 081 082 @Override 083 public void work() { 084 try { 085 doWork(); 086 } catch (InterruptedException e) { 087 // restore interrupted status 088 Thread.currentThread().interrupt(); 089 throw new RuntimeException(e); 090 } 091 } 092 093 protected void doWork() throws InterruptedException { 094 for (;;) { 095 long elapsed = System.currentTimeMillis() - getStartTime(); 096 if (elapsed > durationMillis) { 097 break; 098 } 099 setProgress(new Progress(100F * elapsed / durationMillis)); 100 101 if (isSuspending()) { 102 durationMillis -= elapsed; // save state 103 suspended(); 104 return; 105 } 106 107 Thread.sleep(10); 108 } 109 110 } 111 112 @Override 113 public String toString() { 114 return getClass().getSimpleName() + "(" + (getId().length() > 10 ? "" : (getId() + ", ")) + durationMillis 115 + "ms, " + getProgress() + ")"; 116 } 117 118}