1 /*****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
18 ****************************************************************/
19
20
21 package org.apache.james.postage.execution;
22
23 import org.apache.james.postage.SamplingException;
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26
27 import java.util.Timer;
28 import java.util.TimerTask;
29
30 /***
31 * initiate one shot of sample data generation
32 */
33 public class SampleController extends TimerTask {
34
35 private static Log log = LogFactory.getLog(SampleController.class);
36
37 private Sampler m_sampler;
38 private int m_samplesPerMinute;
39 private Timer m_timer;
40 private int m_secondsDelayOnStop = 0;
41
42 public SampleController(Sampler sampler, int samplesPerMinute) {
43 m_sampler = sampler;
44 m_samplesPerMinute = samplesPerMinute;
45 }
46
47 public SampleController(Sampler sampler, int samplesPerMinute, int secondsDelayOnStop) {
48 this(sampler, samplesPerMinute);
49 m_secondsDelayOnStop = secondsDelayOnStop;
50 }
51
52 public void runThreaded() {
53 if (m_samplesPerMinute < 1) {
54 log.warn("sample controller effectivly disabled with sample-per-minute value = " + m_samplesPerMinute);
55 return;
56 }
57 m_timer = new Timer(true);
58 m_timer.schedule(this, 5, 60*1000/m_samplesPerMinute);
59 }
60
61 public void stop() {
62 if (m_timer != null) m_timer.cancel();
63
64 if (m_secondsDelayOnStop > 0) {
65 try {
66 log.warn("sampler is waiting additional seconds: " + m_secondsDelayOnStop + " (type = " + m_sampler.getClass().getName() + ")");
67 Thread.sleep(m_secondsDelayOnStop * 1000);
68 log.warn("sampler delay passed.");
69 } catch (InterruptedException e) {
70 ;
71 }
72 }
73 }
74
75 public void run() {
76 try {
77 m_sampler.doSample();
78 } catch (SamplingException e) {
79 log.warn("taking sample failed", e);
80 }
81 }
82 }