001/* 002 * (C) Copyright 2012-2018 Nuxeo (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 021import java.util.UUID; 022import java.util.concurrent.atomic.AtomicInteger; 023 024/** 025 * Simple work that just sleeps, mostly used for tests. 026 */ 027public class SleepWork extends AbstractWork { 028 029 public static final String CATEGORY = "SleepWork"; 030 031 private static final long serialVersionUID = 1L; 032 033 protected long durationMillis; 034 035 protected String category; 036 037 protected AtomicInteger count = new AtomicInteger(); 038 039 protected String partitionKey; 040 041 protected boolean idempotent = true; 042 043 /** 044 * Creates a work instance that does nothing but sleep. 045 * 046 * @param durationMillis the sleep duration 047 * @since 10.2 048 */ 049 public SleepWork(long durationMillis, String category, String id) { 050 super(id); 051 this.durationMillis = durationMillis; 052 this.category = category; 053 partitionKey = String.valueOf(count.incrementAndGet()); 054 setProgress(Progress.PROGRESS_0_PC); 055 } 056 057 /** 058 * @since 10.2 059 */ 060 public SleepWork(long durationMillis, String id) { 061 this(durationMillis, CATEGORY, id); 062 } 063 064 /** 065 * @since 10.2 066 */ 067 public SleepWork(long durationMillis) { 068 this(durationMillis, CATEGORY, UUID.randomUUID().toString()); 069 } 070 071 /** 072 * If debug is true, then the various debug* methods must be called in the proper order for the work to start and 073 * stop: {@link #debugStart}, {@link #debugFinish}. 074 * 075 * @param durationMillis the sleep duration 076 * @param debug {@code true} for debug 077 * @deprecated since 10.2 debug flag is unused 078 */ 079 @Deprecated 080 public SleepWork(long durationMillis, boolean debug) { 081 this(durationMillis, null, CATEGORY); 082 } 083 084 /** 085 * @deprecated since 10.2 debug flag is unused 086 */ 087 @Deprecated 088 public SleepWork(long durationMillis, boolean debug, String id) { 089 this(durationMillis, null, id); 090 } 091 092 /** 093 * @deprecated since 10.2 debug flag is unused 094 */ 095 @Deprecated 096 public SleepWork(long durationMillis, String category, boolean debug) { 097 this(durationMillis, category, CATEGORY); 098 } 099 100 /** 101 * @deprecated since 10.2 debug flag is unused 102 */ 103 @Deprecated 104 public SleepWork(long durationMillis, String category, boolean debug, String id) { 105 this(durationMillis, category, id); 106 } 107 108 @Override 109 public String getCategory() { 110 return category; 111 } 112 113 @Override 114 public String getTitle() { 115 return "Sleep " + durationMillis + " ms"; 116 } 117 118 @Override 119 public void work() { 120 try { 121 doWork(); 122 } catch (InterruptedException e) { 123 Thread.currentThread().interrupt(); 124 throw new RuntimeException(e); 125 } 126 } 127 128 protected void doWork() throws InterruptedException { 129 for (;;) { 130 long elapsed = System.currentTimeMillis() - getStartTime(); 131 if (elapsed > durationMillis) { 132 break; 133 } 134 setProgress(new Progress(100F * elapsed / durationMillis)); 135 136 if (isSuspending()) { 137 durationMillis -= elapsed; // save state 138 suspended(); 139 return; 140 } 141 142 if (WorkStateHelper.isCanceled(getId())) { 143 durationMillis -= elapsed; // save state 144 return; 145 } 146 147 Thread.sleep(10); 148 } 149 150 } 151 152 @Override 153 public String getPartitionKey() { 154 return partitionKey; 155 } 156 157 @Override 158 public boolean isIdempotent() { 159 return idempotent; 160 } 161 162 public void setIdempotent(boolean idempotent) { 163 this.idempotent = idempotent; 164 } 165 166 @Override 167 public String toString() { 168 return getClass().getSimpleName() + "(" + (getId().length() > 10 ? "" : getId() + ", ") + durationMillis 169 + "ms, " + getProgress() + ")"; 170 } 171 172}