1 25 26 package org.objectweb.petals.monitoring; 27 28 import java.util.ArrayList ; 29 import java.util.Collections ; 30 import java.util.Comparator ; 31 import java.util.HashMap ; 32 import java.util.List ; 33 34 import org.objectweb.fractal.fraclet.annotation.FractalComponent; 35 import org.objectweb.fractal.fraclet.annotation.Interface; 36 import org.objectweb.fractal.fraclet.annotation.LifeCycle; 37 import org.objectweb.fractal.fraclet.annotation.LifeCycleType; 38 import org.objectweb.fractal.fraclet.annotation.Monolog; 39 import org.objectweb.fractal.fraclet.annotation.Provides; 40 import org.objectweb.fractal.fraclet.annotation.Requires; 41 import org.objectweb.util.monolog.api.Logger; 42 43 import org.objectweb.petals.jbi.component.context.ComponentContextImpl; 44 import org.objectweb.petals.jbi.messaging.MessageExchangeImpl; 45 import org.objectweb.petals.jbi.routing.Router; 46 import org.objectweb.petals.jbi.routing.RoutingException; 47 import org.objectweb.petals.util.LoggingUtil; 48 49 54 @FractalComponent 55 @Provides(interfaces = { 56 @Interface(name = "service", signature = org.objectweb.petals.jbi.routing.Router.class), 57 @Interface(name = "monitoring-service", signature = org.objectweb.petals.monitoring.RouterMonitor.class)}) 58 public class RouterMonitorImpl implements Router, RouterMonitor { 59 60 private boolean monitoring = false; 61 62 @Requires(name = "router", signature = org.objectweb.petals.jbi.routing.Router.class) 63 private Router router; 64 65 @Monolog(name="logger") 66 protected Logger logger; 67 68 protected LoggingUtil log; 69 70 private HashMap <String , ExchangeStateReport> exchangeReports = new HashMap <String , ExchangeStateReport>(); 71 72 private TimeComparator timeComparator = new TimeComparator(); 73 74 78 public void send(ComponentContextImpl source, MessageExchangeImpl exchange, 79 long timeOut) throws RoutingException { 80 81 if (monitoring) { 82 report(exchange); 83 } 84 85 router.send(source, exchange, timeOut); 86 } 87 88 public void receive(MessageExchangeImpl exchange) throws RoutingException { 89 90 if (monitoring) { 91 report(exchange); 92 } 93 94 router.receive(exchange); 95 } 96 97 private void report(MessageExchangeImpl exchange) { 98 ExchangeStateReport report = exchangeReports.get(exchange 99 .getExchangeId()); 100 if (report == null) { 101 report = new ExchangeStateReport(exchange); 102 exchangeReports.put(exchange.getExchangeId(), report); 103 } 104 report.reportState(exchange); 105 106 } 107 108 112 public boolean isMonitoring() { 113 return monitoring; 114 } 115 116 119 public void setMonitoring(boolean monitoring) { 120 this.monitoring = monitoring; 121 if (! monitoring) { 122 exchangeReports = new HashMap <String , ExchangeStateReport>(); 123 } 124 } 125 126 127 135 public List <ExchangeStateReport> getReports(long sinceDate) { 136 log.start(); 137 List <ExchangeStateReport> list = new ArrayList <ExchangeStateReport>(); 139 140 for (ExchangeStateReport report : exchangeReports.values()) { 141 if (report.getReportList().size() == 0 142 || report.getReportList().get(0).time >= sinceDate) { 143 list.add(report); 144 } 145 } 146 Collections.sort(list, timeComparator); 148 149 log.end(); 151 return list; 152 } 153 154 class TimeComparator implements Comparator <ExchangeStateReport> { 155 156 160 public int compare(ExchangeStateReport o1, ExchangeStateReport o2) { 161 List <ExchangeStateCapture> l1 = o1.getReportList(); 162 List <ExchangeStateCapture> l2 = o2.getReportList(); 163 164 int comp = 0; 165 if (l1.size() == 0 || l2.size() == 0) { 166 comp = l1.size() - l2.size(); 169 } else { 170 comp = (int) (l2.get(0).time - l1.get(0).time); 171 } 172 173 return comp; 174 } 175 176 } 177 178 179 180 184 @LifeCycle(on=LifeCycleType.START) 185 protected void start() { 186 log = new LoggingUtil(logger); 187 } 188 } 189 | Popular Tags |