1 22 package org.jboss.mq.server; 23 24 import java.text.DateFormat ; 25 import java.util.ArrayList ; 26 import java.util.Calendar ; 27 import java.util.Date ; 28 import java.util.GregorianCalendar ; 29 30 import org.jboss.mq.MessageStatistics; 31 32 40 public class MessageCounter 41 { 42 String destName; 44 String destSubscription; 45 boolean destTopic; 46 boolean destDurable; 47 48 BasicQueue destQueue; 50 51 int countTotal; 53 int countTotalLast; 54 int depthLast; 55 long timeLastUpdate; 56 57 int dayCounterMax; 59 ArrayList dayCounter; 60 61 68 public static MessageStatistics[] getMessageStatistics(MessageCounter[] counter) throws Exception 69 { 70 MessageStatistics[] stats = new MessageStatistics[counter.length]; 71 for (int i = 0; i < counter.length; ++i) 72 { 73 stats[i] = new MessageStatistics(); 74 stats[i].setName(counter[i].getDestinationName()); 75 stats[i].setSubscriptionID(counter[i].getDestinationSubscription()); 76 stats[i].setTopic(counter[i].getDestinationTopic()); 77 stats[i].setDurable(counter[i].getDestinationDurable()); 78 stats[i].setCount(counter[i].getCount()); 79 stats[i].setCountDelta(counter[i].getCountDelta()); 80 stats[i].setDepth(counter[i].getDepth()); 81 stats[i].setDepthDelta(counter[i].getDepthDelta()); 82 stats[i].setTimeLastUpdate(counter[i].getLastUpdate()); 83 } 84 return stats; 85 } 86 87 97 public MessageCounter( 98 String name, 99 String subscription, 100 BasicQueue queue, 101 boolean topic, 102 boolean durable, 103 int daycountmax) 104 { 105 destName = name; 107 destSubscription = subscription; 108 destTopic = topic; 109 destDurable = durable; 110 destQueue = queue; 111 112 resetCounter(); 114 115 dayCounter = new ArrayList (); 117 118 setHistoryLimit(daycountmax); 119 } 120 121 124 public String toString() 125 { 126 return getCounterAsString(); 127 } 128 129 132 public void incrementCounter() 133 { 134 countTotal++; 136 137 timeLastUpdate = System.currentTimeMillis(); 139 140 updateHistory(true); 142 } 143 144 149 public String getDestinationName() 150 { 151 return destName; 152 } 153 154 159 public String getDestinationSubscription() 160 { 161 return destSubscription; 162 } 163 164 169 public boolean getDestinationTopic() 170 { 171 return destTopic; 172 } 173 174 180 public boolean getDestinationDurable() 181 { 182 return destDurable; 183 } 184 185 191 public int getCount() 192 { 193 return countTotal; 194 } 195 196 201 public int getCountDelta() 202 { 203 int delta = countTotal - countTotalLast; 204 205 countTotalLast = countTotal; 206 207 return delta; 208 } 209 210 216 public int getDepth() 217 { 218 return destQueue.getQueueDepth(); 219 } 220 221 227 public int getDepthDelta() 228 { 229 int current = destQueue.getQueueDepth(); 230 int delta = current - depthLast; 231 232 depthLast = current; 233 234 return delta; 235 } 236 237 242 public long getLastUpdate() 243 { 244 return timeLastUpdate; 245 } 246 247 250 public void resetCounter() 251 { 252 countTotal = 0; 253 countTotalLast = 0; 254 depthLast = 0; 255 timeLastUpdate = 0; 256 } 257 258 266 public String getCounterAsString() 267 { 268 String ret; 269 270 if (destTopic) 272 ret = "Topic,"; 273 else 274 ret = "Queue,"; 275 276 ret += destName + ","; 278 279 if (destSubscription != null) 281 ret += destSubscription + ","; 282 else 283 ret += "-,"; 284 285 if (destTopic) 287 { 288 if (destDurable) 290 ret += "true,"; 291 else 292 ret += "false,"; 293 } 294 else 295 { 296 ret += "-,"; 298 } 299 300 ret += getCount() + "," + getCountDelta() + "," + getDepth() + "," + getDepthDelta() + ","; 302 303 if (timeLastUpdate > 0) 305 { 306 DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM); 307 308 ret += dateFormat.format(new Date (timeLastUpdate)); 309 } 310 else 311 { 312 ret += "-"; 313 } 314 315 return ret; 316 } 317 318 323 public int getHistoryLimit() 324 { 325 return dayCounterMax; 326 } 327 328 333 public void setHistoryLimit(int daycountmax) 334 { 335 boolean bInitialize = false; 336 337 dayCounterMax = daycountmax; 339 340 synchronized (dayCounter) 342 { 343 if (dayCounterMax > 0) 344 { 345 int delta = dayCounter.size() - dayCounterMax; 347 348 for (int i = 0; i < delta; i++) 349 { 350 dayCounter.remove(0); 353 } 354 355 bInitialize = dayCounter.isEmpty(); 357 } 358 else if (dayCounterMax == 0) 359 { 360 dayCounter.clear(); 362 } 363 else 364 { 365 367 bInitialize = dayCounter.isEmpty(); 369 } 370 371 if (bInitialize) 373 { 374 dayCounter.add(new DayCounter(new GregorianCalendar (), true)); 375 } 376 } 377 } 378 379 382 private void updateHistory(boolean incrementCounter) 383 { 384 if (dayCounter.isEmpty()) 386 { 387 return; 388 } 389 390 synchronized (dayCounter) 392 { 393 DayCounter counterLast = (DayCounter) dayCounter.get(dayCounter.size() - 1); 394 395 GregorianCalendar calNow = new GregorianCalendar (); 396 GregorianCalendar calLast = counterLast.getDate(); 397 398 calNow.clear(Calendar.AM_PM); 400 calNow.clear(Calendar.HOUR); 401 calNow.clear(Calendar.HOUR_OF_DAY); 402 calNow.clear(Calendar.MINUTE); 403 calNow.clear(Calendar.SECOND); 404 calNow.clear(Calendar.MILLISECOND); 405 406 calLast.clear(Calendar.AM_PM); 407 calLast.clear(Calendar.HOUR); 408 calLast.clear(Calendar.HOUR_OF_DAY); 409 calLast.clear(Calendar.MINUTE); 410 calLast.clear(Calendar.SECOND); 411 calLast.clear(Calendar.MILLISECOND); 412 413 long millisPerDay = 86400000; long millisDelta = calNow.getTime().getTime() - calLast.getTime().getTime(); 415 416 int dayDelta = (int) (millisDelta / millisPerDay); 417 418 if (dayDelta > 0) 419 { 420 counterLast.finalizeDayCounter(); 422 423 DayCounter counterNew; 425 426 for (int i = 1; i < dayDelta; i++) 427 { 428 calLast.add(Calendar.DAY_OF_YEAR, 1); 430 431 counterNew = new DayCounter(calLast, false); 432 counterNew.finalizeDayCounter(); 433 434 dayCounter.add(counterNew); 435 } 436 437 counterNew = new DayCounter(calNow, false); 439 440 dayCounter.add(counterNew); 441 442 setHistoryLimit(dayCounterMax); 444 } 445 446 counterLast = (DayCounter) dayCounter.get(dayCounter.size() - 1); 448 counterLast.updateDayCounter(incrementCounter); 449 } 450 } 451 452 455 public void resetHistory() 456 { 457 int max = dayCounterMax; 458 459 setHistoryLimit(0); 460 setHistoryLimit(max); 461 } 462 463 475 public String getHistoryAsString() 476 { 477 String ret = ""; 478 479 updateHistory(false); 481 482 synchronized (dayCounter) 484 { 485 ret += dayCounter.size() + "\n"; 487 488 for (int i = 0; i < dayCounter.size(); i++) 490 { 491 DayCounter counter = (DayCounter) dayCounter.get(i); 492 493 ret += counter.getDayCounterAsString() + "\n"; 494 } 495 } 496 497 return ret; 498 } 499 500 503 static class DayCounter 504 { 505 static final int HOURS = 24; 506 507 GregorianCalendar date = null; 508 int[] counters = new int[HOURS]; 509 510 517 DayCounter(GregorianCalendar date, boolean isStartDay) 518 { 519 this.date = (GregorianCalendar ) date.clone(); 521 522 int hour = date.get(Calendar.HOUR_OF_DAY); 525 526 for (int i = 0; i < HOURS; i++) 527 { 528 if (i < hour) 529 { 530 if (isStartDay) 531 counters[i] = -1; 532 else 533 counters[i] = 0; 534 } 535 else 536 { 537 counters[i] = -1; 538 } 539 } 540 541 counters[hour] = 0; 543 } 544 545 550 GregorianCalendar getDate() 551 { 552 return (GregorianCalendar ) date.clone(); 553 } 554 555 560 void updateDayCounter(boolean incrementCounter) 561 { 562 GregorianCalendar cal = new GregorianCalendar (); 564 565 int currentIndex = cal.get(Calendar.HOUR_OF_DAY); 566 567 boolean bUpdate = false; 570 571 for (int i = 0; i <= currentIndex; i++) 572 { 573 if (counters[i] > -1) 574 { 575 bUpdate = true; 579 } 580 581 if (bUpdate == true) 582 { 583 if (counters[i] == -1) 584 counters[i] = 0; 585 } 586 } 587 588 if (incrementCounter) 590 { 591 counters[currentIndex]++; 592 } 593 } 594 595 598 void finalizeDayCounter() 599 { 600 boolean bFinalize = false; 603 604 for (int i = 0; i < HOURS; i++) 605 { 606 if (counters[i] > -1) 607 { 608 bFinalize = true; 612 } 613 614 if (bFinalize) 615 { 616 if (counters[i] == -1) 617 counters[i] = 0; 618 } 619 } 620 } 621 622 628 String getDayCounterAsString() 629 { 630 DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT); 632 633 String strData = dateFormat.format(date.getTime()); 634 635 for (int i = 0; i < HOURS; i++) 637 { 638 strData += "," + counters[i]; 639 } 640 641 return strData; 642 } 643 } 644 } 645 | Popular Tags |