1 20 package org.apache.mina.integration.jmx; 21 22 import java.net.SocketAddress ; 23 import java.util.Iterator ; 24 25 import org.apache.mina.common.IoService; 26 import org.apache.mina.common.IoSession; 27 import org.apache.mina.management.StatCollector; 28 29 33 public class IoServiceManager implements IoServiceManagerMBean { 34 private IoService service; 35 36 private StatCollector collector = null; 37 38 public IoServiceManager(IoService service) { 39 this.service = service; 40 } 41 42 public int getManagedSessionCount() { 43 44 int count = 0; 45 for (Iterator iter = service.getManagedServiceAddresses().iterator(); iter 46 .hasNext();) { 47 SocketAddress element = (SocketAddress ) iter.next(); 48 49 count += service.getManagedSessions(element).size(); 50 } 51 return count; 52 } 53 54 public void startCollectingStats(int millisecondsPolling) { 55 if (collector != null && collector.isRunning()) { 56 throw new RuntimeException ("Already collecting stats"); 57 } 58 59 collector = new StatCollector(service, millisecondsPolling); 60 collector.start(); 61 62 } 63 64 public void stopCollectingStats() { 65 if (collector != null && collector.isRunning()) 66 collector.stop(); 67 68 } 69 70 public float getTotalByteReadThroughput() { 71 return collector.getBytesReadThroughput(); 72 } 73 74 public float getTotalByteWrittenThroughput() { 75 return collector.getBytesWrittenThroughput(); 76 } 77 78 public float getTotalMessageReadThroughput() { 79 return collector.getMsgReadThroughput(); 80 } 81 82 public float getTotalMessageWrittenThroughput() { 83 return collector.getMsgWrittenThroughput(); 84 } 85 86 public float getAverageByteReadThroughput() { 87 return collector.getBytesReadThroughput() / collector.getSessionCount(); 88 } 89 90 public float getAverageByteWrittenThroughput() { 91 return collector.getBytesWrittenThroughput() 92 / collector.getSessionCount(); 93 } 94 95 public float getAverageMessageReadThroughput() { 96 return collector.getMsgReadThroughput() / collector.getSessionCount(); 97 } 98 99 public float getAverageMessageWrittenThroughput() { 100 return collector.getMsgWrittenThroughput() 101 / collector.getSessionCount(); 102 } 103 104 public void closeAllSessions() { 105 for (Iterator iter = service.getManagedServiceAddresses().iterator(); iter 106 .hasNext();) { 107 SocketAddress element = (SocketAddress ) iter.next(); 108 109 for (Iterator iter2 = service.getManagedSessions(element) 110 .iterator(); iter2.hasNext();) { 111 IoSession session = (IoSession) iter2.next(); 112 session.close(); 113 } 114 } 115 116 } 117 } 118 | Popular Tags |