1 package example; 2 3 import com.caucho.util.ThreadPool; 4 import com.caucho.util.ThreadTask; 5 6 import java.io.*; 7 8 import java.text.DateFormat ; 9 import java.text.NumberFormat ; 10 11 import java.util.Date ; 12 import java.util.logging.Level ; 13 import java.util.logging.Logger ; 14 15 import java.util.concurrent.Executor ; 16 17 import javax.annotation.Resource; 18 19 import javax.servlet.*; 20 import javax.servlet.http.*; 21 22 25 public class PeriodicTaskServlet extends HttpServlet { 26 static protected final Logger log = 27 Logger.getLogger(PeriodicTaskServlet.class.getName()); 28 29 int _refreshRate = 5; 30 31 @Resource 32 private Executor _executor; 33 34 private PeriodicTask _periodicTask; 35 36 private NumberFormat _numberFormat ; 37 private DateFormat _dateFormat ; 38 39 public PeriodicTaskServlet() 40 { 41 } 42 43 public void setPeriodicTask(PeriodicTask periodicTask) 44 { 45 _periodicTask = periodicTask; 46 } 47 48 52 public void setRefreshRate(int refreshRate) 53 { 54 _refreshRate = 5; 55 } 56 57 public void init() 58 throws ServletException 59 { 60 _numberFormat = NumberFormat.getInstance(); 61 _dateFormat = DateFormat.getInstance(); 62 63 String p; 64 65 p = getInitParameter("refresh-rate"); 66 if (p != null) 67 setRefreshRate(Integer.parseInt(p)); 68 69 if (_periodicTask == null) 70 throw new ServletException("`period-task' is required"); 71 } 72 73 protected void doGet(HttpServletRequest request, HttpServletResponse response) 74 throws ServletException, IOException 75 { 76 final PeriodicTask task = _periodicTask; 77 78 String msg = null; 79 80 if (request.getParameter("RUN") != null) { 81 if (task.isActive()) 82 msg = "Already active."; 83 else { 84 ThreadTask threadTask = new ThreadTask() { 88 public void run() 89 { 90 task.run(); 91 } 92 }; 93 _executor.execute(threadTask); 94 Thread.yield(); 95 response.sendRedirect(request.getRequestURI()); 96 } 97 } 98 99 response.setContentType("text/html"); 100 PrintWriter out = response.getWriter(); 101 102 response.setHeader("Cache-Control","no-cache,post-check=0,pre-check=0"); 104 response.setHeader("Pragma","no-cache"); 105 response.setHeader("Expires","Thu,01Dec199416:00:00GMT"); 106 107 if (_refreshRate > 0) { 108 response.addHeader( "refresh", String.valueOf(_refreshRate) + "; URL=" + request.getRequestURI()); 109 } 110 111 out.println("<html>"); 112 out.println("<head><title>PeriodicTask</title></head>"); 113 out.println("<body>"); 114 out.println("<h1>PeriodicTask</h1>"); 115 116 if (msg != null) { 117 out.print("<p><b>"); 118 printSafeHtml(out,msg); 119 out.println("</b></p>"); 120 } 121 122 out.println("<table border='0'>"); 123 124 printHeading(out,"configuration"); 125 126 printPeriod(out,"estimated-average-time:", task.getEstimatedAverageTime()); 127 128 printHeading(out,"statistics"); 129 130 printField(out,"active", task.isActive()); 131 printPeriod(out,"estimated-time-remaining",task.getEstimatedTimeRemaining()); 132 printDate(out,"last-active-time", task.getLastActiveTime()); 133 printField(out,"total-active-count", task.getTotalActiveCount()); 134 printPeriod(out,"total-active-time", task.getTotalActiveTime()); 135 printPeriod(out,"average-active-time", task.getAverageActiveTime()); 136 137 printHeading(out,"actions"); 138 printActions(out,new String [][] { {"RUN", "Run"} }); 139 140 out.println("</table border='0'>"); 141 142 143 out.println("</body>"); 144 out.println("</html>"); 145 } 146 147 protected void printHeading(PrintWriter out, String heading) 148 { 149 out.print("<tr><td colspan='2'><h2>"); 150 printSafeHtml(out,heading); 151 out.println("</h2></td></tr>"); 152 } 153 154 protected void printField(PrintWriter out, String name, String value) 155 { 156 out.print("<tr><td>"); 157 printSafeHtml(out,name); 158 out.print("</td><td>"); 159 printSafeHtml(out,value); 160 out.print("</td></tr>"); 161 } 162 163 protected void printField(PrintWriter out, String name, boolean value) 164 { 165 printField(out,name, value == true ? "true" : "false"); 166 } 167 168 protected void printField(PrintWriter out, String name, long value) 169 { 170 printField(out,name, _numberFormat.format(value)); 171 } 172 protected void printDate(PrintWriter out, String name, long date) 173 { 174 printField(out,name, _dateFormat.format(new Date (date))); 175 } 176 177 protected void printPeriod(PrintWriter out, String name, long millis) 178 { 179 double sec = millis / 1000; 180 printField(out,name, _numberFormat.format(millis) + "sec"); 181 } 182 183 protected void printActions(PrintWriter out, String [][] actions) 184 { 185 out.println("<tr><td colspan='2'><form>"); 186 for (int i = 0; i < actions.length; i++) { 187 out.print("<input type='submit' name='"); 188 printSafeHtml(out,actions[i][0]); 189 out.print("' value='"); 190 printSafeHtml(out,actions[i][1]); 191 out.println("'/>"); 192 } 193 out.println("</form></td></tr>"); 194 } 195 196 protected void printSafeHtml(PrintWriter out, String text) 197 { 198 int len = text.length(); 199 200 for (int i = 0; i < len; i++) { 201 char ch = text.charAt(i); 202 switch (ch) { 203 case '<': 204 out.print("<"); 205 break; 206 case '>': 207 out.print(">"); 208 break; 209 case '&': 210 out.print("&"); 211 break; 212 default: 213 out.print(ch); 214 } 215 } 216 } 217 } 218 | Popular Tags |