1 14 package wingset; 15 16 import org.wings.*; 17 import org.wings.event.SRenderEvent; 18 import org.wings.event.SRenderListener; 19 import org.wings.session.WingsStatistics; 20 21 import java.awt.event.ActionEvent ; 22 import java.awt.event.ActionListener ; 23 import java.text.DecimalFormat ; 24 25 29 public class MemUsageExample extends WingSetPane { 30 31 private static final long SECOND = 1000; 32 private static final long MINUTE = 60 * SECOND; 33 private static final long HOUR = 60 * MINUTE; 34 private static final long DAY = 24 * HOUR; 35 36 private static final DecimalFormat megaByteFormatter = new DecimalFormat ("#.###"); 37 38 private static final String getByteString(long bytes) { 39 if (bytes < 1024) { 40 return Long.toString(bytes); 41 } 43 if (bytes < 1024 * 1024) { 44 return Long.toString(bytes / (1024)) + "kB"; 45 } 47 return megaByteFormatter.format(new Double (((double) bytes) / (1024 * 1024))) + "MB"; 48 49 } 50 51 private static final String getUptimeString(long uptime) { 52 StringBuffer result = new StringBuffer (); 53 54 boolean doAppend = false; 55 56 if (uptime / DAY > 0) { 57 result.append(uptime / DAY).append("d "); 58 uptime %= DAY; 59 doAppend = true; 60 } 61 62 if (uptime / HOUR > 0 || doAppend) { 63 result.append(uptime / HOUR).append("h "); 64 uptime %= HOUR; 65 doAppend = true; 66 } 67 68 if (uptime / MINUTE > 0 || doAppend) { 69 result.append(uptime / MINUTE).append("m "); 70 uptime %= MINUTE; 71 doAppend = true; 72 } 73 74 if (uptime / SECOND > 0 || doAppend) { 75 result.append(uptime / SECOND).append("s "); 76 uptime %= SECOND; 77 } 78 79 result.append(uptime).append("ms"); 80 81 return result.toString(); 82 } 83 84 public SComponent createExample() { 85 final SButton gc = new SButton("gc"); 86 87 final SProgressBar infoBar = new SProgressBar(); 88 final SLabel totalMemory = new SLabel(); 89 final SLabel freeMemory = new SLabel(); 90 final SLabel usedMemory = new SLabel(); 91 final SLabel overallSessions = new SLabel(); 92 final SLabel activeSessions = new SLabel(); 93 final SLabel uptime = new SLabel(); 94 final SLabel requestCount = new SLabel(); 95 final SPanel panel = new SPanel(); 96 97 gc.addActionListener(new ActionListener () { 98 public void actionPerformed(ActionEvent e) { 99 System.gc(); 100 } 101 }); 102 103 SRenderListener renderListener = new SRenderListener() { 104 public void startRendering(SRenderEvent e) { 105 long free = Runtime.getRuntime().freeMemory(); 106 long total = Runtime.getRuntime().totalMemory(); 107 108 infoBar.setMaximum((int) total); 109 infoBar.setValue((int) (total - free)); 110 infoBar.setString(getByteString(total - free)); 111 112 totalMemory.setText(getByteString(total)); 113 freeMemory.setText(getByteString(free)); 114 usedMemory.setText(getByteString(total - free)); 115 overallSessions.setText(Integer.toString(WingsStatistics.getStatistics().getOverallSessionCount())); 116 activeSessions.setText(Integer.toString(WingsStatistics.getStatistics().getActiveSessionCount())); 117 requestCount.setText(Integer.toString(WingsStatistics.getStatistics().getRequestCount())); 118 uptime.setText(getUptimeString(WingsStatistics.getStatistics().getUptime())); 119 } 120 121 public void doneRendering(SRenderEvent e) {} 122 }; 123 panel.addRenderListener(renderListener); 124 125 126 infoBar.setUnfilledColor(java.awt.Color.gray); 127 infoBar.setFilledColor(java.awt.Color.red); 128 infoBar.setForeground(java.awt.Color.red); 129 infoBar.setBorderColor(java.awt.Color.black); 130 infoBar.setStringPainted(true); 131 infoBar.setPreferredSize(new SDimension(200, 5)); 132 133 134 try { 135 java.net.URL templateURL = 136 getSession().getServletContext().getResource("/templates/MemUsageExample.thtml"); 137 STemplateLayout layout = new STemplateLayout(templateURL); 138 panel.setLayout(layout); 139 } catch (Exception ex) { 140 ex.printStackTrace(); 141 } 142 143 panel.add(gc, "GC"); 144 panel.add(infoBar, "InfoBar"); 145 panel.add(totalMemory, "TotalMemory"); 146 panel.add(freeMemory, "FreeMemory"); 147 panel.add(usedMemory, "UsedMemory"); 148 panel.add(activeSessions, "ActiveSessions"); 149 panel.add(overallSessions, "OverallSessions"); 150 panel.add(requestCount, "RequestCount"); 151 panel.add(uptime, "Uptime"); 152 153 154 return panel; 155 } 156 157 158 } 159 160 161 | Popular Tags |