View Javadoc
1   package org.apachi.onami.lifecycle.warmup;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import javax.inject.Singleton;
23  
24  import java.util.HashMap;
25  import java.util.Map;
26  import java.util.concurrent.TimeUnit;
27  import java.util.concurrent.atomic.AtomicBoolean;
28  import java.util.concurrent.atomic.AtomicLong;
29  
30  @Singleton
31  public class RecorderSleepSettings
32  {
33      private static final int DEFAULT_SLEEP_MS = 1000;
34  
35      private final AtomicLong baseSleepMs = new AtomicLong( DEFAULT_SLEEP_MS );
36  
37      private final Map<String, Long> baseSleepMsForString = new HashMap<String, Long>();
38  
39      private final AtomicBoolean randomize = new AtomicBoolean( true );
40  
41      public void setBaseSleep( long time, TimeUnit unit )
42      {
43          baseSleepMs.set( unit.toMillis( time ) );
44      }
45  
46      public void setBaseSleepFor( String s, long time, TimeUnit unit )
47      {
48          baseSleepMsForString.put( s, unit.toMillis( time ) );
49      }
50  
51      public long getSleepMsFor( String s )
52      {
53          long sleepMs = baseSleepMsForString.containsKey( s ) ? baseSleepMsForString.get( s ) : baseSleepMs.get();
54          if ( randomize.get() )
55          {
56              sleepMs = ( (int) ( sleepMs * Math.random() ) + 1 );
57          }
58          return sleepMs;
59      }
60  
61      public void setRandomize( boolean randomize )
62      {
63          this.randomize.set( randomize );
64      }
65  }