1 /***
2 * Copyright 2003-2010 Terracotta, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package net.sf.ehcache.management.sampled;
18
19 import java.beans.PropertyChangeEvent;
20 import java.beans.PropertyChangeListener;
21 import java.util.HashMap;
22 import java.util.Map;
23
24 import javax.management.MBeanNotificationInfo;
25 import javax.management.NotCompliantMBeanException;
26 import javax.management.Notification;
27
28 import net.sf.ehcache.Ehcache;
29 import net.sf.ehcache.config.CacheConfiguration;
30 import net.sf.ehcache.config.CacheConfigurationListener;
31 import net.sf.ehcache.config.TerracottaConfiguration.Consistency;
32 import net.sf.ehcache.config.TerracottaConfiguration.StorageStrategy;
33 import net.sf.ehcache.hibernate.management.impl.BaseEmitterBean;
34 import net.sf.ehcache.util.CacheTransactionHelper;
35 import net.sf.ehcache.writer.writebehind.WriteBehindManager;
36
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 /***
41 * An implementation of {@link SampledCacheMBean}
42 *
43 * <p />
44 *
45 * @author <a href="mailto:asanoujam@terracottatech.com">Abhishek Sanoujam</a>
46 * @since 1.7
47 */
48 public class SampledCache extends BaseEmitterBean implements SampledCacheMBean, CacheConfigurationListener, PropertyChangeListener {
49 private static final Logger LOG = LoggerFactory.getLogger(SampledCache.class.getName());
50
51 private static final MBeanNotificationInfo[] NOTIFICATION_INFO;
52
53 private final Ehcache cache;
54 private final String immutableCacheName;
55
56 static {
57 final String[] notifTypes = new String[] {CACHE_ENABLED, CACHE_CHANGED, CACHE_FLUSHED, CACHE_STATISTICS_ENABLED,
58 CACHE_STATISTICS_RESET, };
59 final String name = Notification.class.getName();
60 final String description = "Ehcache SampledCache Event";
61 NOTIFICATION_INFO = new MBeanNotificationInfo[] {new MBeanNotificationInfo(notifTypes, name, description), };
62 }
63
64 /***
65 * Constructor accepting the backing {@link Ehcache}
66 *
67 * @param cache
68 */
69 public SampledCache(Ehcache cache) throws NotCompliantMBeanException {
70 super(SampledCacheMBean.class);
71 this.cache = cache;
72 immutableCacheName = cache.getName();
73 cache.getCacheConfiguration().addConfigurationListener(this);
74 cache.addPropertyChangeListener(this);
75 }
76
77 /***
78 * Method which returns the name of the cache at construction time.
79 * Package protected method.
80 *
81 * @return The name of the cache
82 */
83 String getImmutableCacheName() {
84 return immutableCacheName;
85 }
86
87 /***
88 * {@inheritDoc}
89 */
90 public boolean isEnabled() {
91 return !cache.isDisabled();
92 }
93
94 /***
95 * {@inheritDoc}
96 */
97 public void setEnabled(boolean enabled) {
98 try {
99 cache.setDisabled(!enabled);
100 } catch (RuntimeException e) {
101 throw newPlainException(e);
102 }
103 }
104
105 /***
106 * {@inheritDoc}
107 * @deprecated use {@link #isClusterBulkLoadEnabled()} instead
108 */
109 @Deprecated
110 public boolean isClusterCoherent() {
111 try {
112 return cache.isClusterCoherent();
113 } catch (RuntimeException e) {
114 throw newPlainException(e);
115 }
116 }
117
118 /***
119 * {@inheritDoc}
120 */
121 public boolean isClusterBulkLoadEnabled() {
122 try {
123 return cache.isClusterBulkLoadEnabled();
124 } catch (RuntimeException e) {
125 throw newPlainException(e);
126 }
127 }
128
129 /***
130 * {@inheritDoc}
131 * @deprecated use {@link #isNodeBulkLoadEnabled()} instead
132 */
133 @Deprecated
134 public boolean isNodeCoherent() {
135 try {
136 return cache.isNodeCoherent();
137 } catch (RuntimeException e) {
138 throw newPlainException(e);
139 }
140 }
141
142 /***
143 * {@inheritDoc}
144 */
145 public boolean isNodeBulkLoadEnabled() {
146 return !isNodeCoherent();
147 }
148
149 /***
150 * {@inheritDoc}
151 * @deprecated use {@link #setNodeBulkLoadEnabled(boolean)} instead
152 */
153 @Deprecated
154 public void setNodeCoherent(boolean coherent) {
155 boolean isNodeCoherent = isNodeCoherent();
156 if (coherent != isNodeCoherent) {
157 if (!coherent && getTransactional()) {
158 LOG.warn("a transactional cache cannot be incoherent");
159 return;
160 }
161 try {
162 cache.setNodeCoherent(coherent);
163 } catch (RuntimeException e) {
164 throw newPlainException(e);
165 }
166 }
167 }
168
169 /***
170 * {@inheritDoc}
171 */
172 public void setNodeBulkLoadEnabled(boolean bulkLoadEnabled) {
173 if (bulkLoadEnabled && getTransactional()) {
174 LOG.warn("a transactional cache cannot be put into bulk-load mode");
175 return;
176 }
177 setNodeCoherent(!bulkLoadEnabled);
178 }
179
180 private RuntimeException newPlainException(RuntimeException e) {
181 String type = e.getClass().getName();
182 if (type.startsWith("java.") || type.startsWith("javax.")) {
183 return e;
184 } else {
185 RuntimeException result = new RuntimeException(e.getMessage());
186 result.setStackTrace(e.getStackTrace());
187 return result;
188 }
189 }
190
191 /***
192 * {@inheritDoc}
193 */
194 public void flush() {
195 try {
196 cache.flush();
197 sendNotification(CACHE_FLUSHED, getCacheAttributes(), getImmutableCacheName());
198 } catch (RuntimeException e) {
199 throw newPlainException(e);
200 }
201 }
202
203 /***
204 * {@inheritDoc}
205 */
206 public String getCacheName() {
207 return cache.getName();
208 }
209
210 /***
211 * {@inheritDoc}
212 */
213 public String getStatus() {
214 return cache.getStatus().toString();
215 }
216
217 /***
218 * {@inheritDoc}
219 */
220 public void removeAll() {
221 CacheTransactionHelper.beginTransactionIfNeeded(cache);
222 try {
223 cache.removeAll();
224 sendNotification(CACHE_CLEARED, getCacheAttributes(), getImmutableCacheName());
225 } catch (RuntimeException e) {
226 throw newPlainException(e);
227 } finally {
228 try {
229 CacheTransactionHelper.commitTransactionIfNeeded(cache);
230 } catch (RuntimeException e2) {
231 throw newPlainException(e2);
232 }
233 }
234 }
235
236 /***
237 * {@inheritDoc}
238 */
239 public long getAverageGetTimeMostRecentSample() {
240 return cache.getSampledCacheStatistics().getAverageGetTimeMostRecentSample();
241 }
242
243 /***
244 * {@inheritDoc}
245 */
246 public long getCacheEvictionRate() {
247 return getCacheElementEvictedMostRecentSample();
248 }
249
250 /***
251 * {@inheritDoc}
252 */
253 public long getCacheElementEvictedMostRecentSample() {
254 return cache.getSampledCacheStatistics().getCacheElementEvictedMostRecentSample();
255 }
256
257 /***
258 * {@inheritDoc}
259 */
260 public long getCacheExpirationRate() {
261 return getCacheElementExpiredMostRecentSample();
262 }
263
264 /***
265 * {@inheritDoc}
266 */
267 public long getCacheElementExpiredMostRecentSample() {
268 return cache.getSampledCacheStatistics().getCacheElementExpiredMostRecentSample();
269 }
270
271 /***
272 * {@inheritDoc}
273 */
274 public long getCachePutRate() {
275 return getCacheElementPutMostRecentSample();
276 }
277
278 /***
279 * {@inheritDoc}
280 */
281 public long getCacheElementPutMostRecentSample() {
282 return cache.getSampledCacheStatistics().getCacheElementPutMostRecentSample();
283 }
284
285 /***
286 * {@inheritDoc}
287 */
288 public long getCacheRemoveRate() {
289 return getCacheElementRemovedMostRecentSample();
290 }
291
292 /***
293 * {@inheritDoc}
294 */
295 public long getCacheElementRemovedMostRecentSample() {
296 return cache.getSampledCacheStatistics().getCacheElementRemovedMostRecentSample();
297 }
298
299 /***
300 * {@inheritDoc}
301 */
302 public long getCacheUpdateRate() {
303 return getCacheElementUpdatedMostRecentSample();
304 }
305
306 /***
307 * {@inheritDoc}
308 */
309 public long getCacheElementUpdatedMostRecentSample() {
310 return cache.getSampledCacheStatistics().getCacheElementUpdatedMostRecentSample();
311 }
312
313 /***
314 * {@inheritDoc}
315 */
316 public long getCacheInMemoryHitRate() {
317 return getCacheHitInMemoryMostRecentSample();
318 }
319
320 /***
321 * {@inheritDoc}
322 */
323 public long getCacheHitInMemoryMostRecentSample() {
324 return cache.getSampledCacheStatistics().getCacheHitInMemoryMostRecentSample();
325 }
326
327 /***
328 * {@inheritDoc}
329 */
330 public long getCacheOffHeapHitRate() {
331 return getCacheHitOffHeapMostRecentSample();
332 }
333
334 /***
335 * {@inheritDoc}
336 */
337 public long getCacheHitOffHeapMostRecentSample() {
338 return cache.getSampledCacheStatistics().getCacheHitOffHeapMostRecentSample();
339 }
340
341 /***
342 * {@inheritDoc}
343 */
344 public long getCacheHitRate() {
345 return getCacheHitMostRecentSample();
346 }
347
348 /***
349 * {@inheritDoc}
350 */
351 public long getCacheHitMostRecentSample() {
352 return cache.getSampledCacheStatistics().getCacheHitMostRecentSample();
353 }
354
355 /***
356 * {@inheritDoc}
357 */
358 public long getCacheOnDiskHitRate() {
359 return getCacheHitOnDiskMostRecentSample();
360 }
361
362 /***
363 * {@inheritDoc}
364 */
365 public long getCacheHitOnDiskMostRecentSample() {
366 return cache.getSampledCacheStatistics().getCacheHitOnDiskMostRecentSample();
367 }
368
369 /***
370 * {@inheritDoc}
371 */
372 public long getCacheMissExpiredMostRecentSample() {
373 return cache.getSampledCacheStatistics().getCacheMissExpiredMostRecentSample();
374 }
375
376 /***
377 * {@inheritDoc}
378 */
379 public long getCacheMissRate() {
380 return getCacheMissMostRecentSample();
381 }
382
383 /***
384 * {@inheritDoc}
385 */
386 public long getCacheMissMostRecentSample() {
387 return cache.getSampledCacheStatistics().getCacheMissMostRecentSample();
388 }
389
390 /***
391 * {@inheritDoc}
392 */
393 public long getCacheInMemoryMissRate() {
394 return getCacheMissInMemoryMostRecentSample();
395 }
396
397 /***
398 * {@inheritDoc}
399 */
400 public long getCacheMissInMemoryMostRecentSample() {
401 return cache.getSampledCacheStatistics().getCacheMissInMemoryMostRecentSample();
402 }
403
404 /***
405 * {@inheritDoc}
406 */
407 public long getCacheOffHeapMissRate() {
408 return getCacheMissOffHeapMostRecentSample();
409 }
410
411 /***
412 * {@inheritDoc}
413 */
414 public long getCacheMissOffHeapMostRecentSample() {
415 return cache.getSampledCacheStatistics().getCacheMissOffHeapMostRecentSample();
416 }
417
418 /***
419 * {@inheritDoc}
420 */
421 public long getCacheOnDiskMissRate() {
422 return getCacheMissOnDiskMostRecentSample();
423 }
424
425 /***
426 * {@inheritDoc}
427 */
428 public long getCacheMissOnDiskMostRecentSample() {
429 return cache.getSampledCacheStatistics().getCacheMissOnDiskMostRecentSample();
430 }
431
432 /***
433 * {@inheritDoc}
434 */
435 public long getCacheMissNotFoundMostRecentSample() {
436 return cache.getSampledCacheStatistics().getCacheMissNotFoundMostRecentSample();
437 }
438
439 /***
440 * {@inheritDoc}
441 */
442 public int getStatisticsAccuracy() {
443 return cache.getSampledCacheStatistics().getStatisticsAccuracy();
444 }
445
446 /***
447 * {@inheritDoc}
448 */
449 public String getStatisticsAccuracyDescription() {
450 return cache.getSampledCacheStatistics().getStatisticsAccuracyDescription();
451 }
452
453 /***
454 * {@inheritDoc}
455 */
456 public void clearStatistics() {
457 try {
458 cache.clearStatistics();
459 sendNotification(CACHE_STATISTICS_RESET, getCacheAttributes(), getImmutableCacheName());
460 } catch (RuntimeException e) {
461 throw newPlainException(e);
462 }
463 }
464
465 /***
466 * {@inheritDoc}
467 */
468 public boolean isStatisticsEnabled() {
469 return cache.isStatisticsEnabled();
470 }
471
472 /***
473 * {@inheritDoc}
474 */
475 public boolean isSampledStatisticsEnabled() {
476 return cache.getSampledCacheStatistics().isSampledStatisticsEnabled();
477 }
478
479 /***
480 * {@inheritDoc}
481 */
482 public boolean isTerracottaClustered() {
483 return this.cache.getCacheConfiguration().isTerracottaClustered();
484 }
485
486 /***
487 * {@inheritDoc}
488 */
489 public String getTerracottaConsistency() {
490 Consistency consistency = this.cache.getCacheConfiguration().getTerracottaConsistency();
491 return consistency != null ? consistency.name() : "na";
492 }
493
494 /***
495 * {@inheritDoc}
496 */
497 public String getTerracottaStorageStrategy() {
498 StorageStrategy storageStrategy = this.cache.getCacheConfiguration().getTerracottaStorageStrategy();
499 return storageStrategy != null ? storageStrategy.name() : "na";
500 }
501
502 /***
503 * {@inheritDoc}
504 *
505 * @see net.sf.ehcache.management.sampled.SampledCacheMBean#enableStatistics()
506 */
507 public void enableStatistics() {
508 if (!cache.isStatisticsEnabled()) {
509 try {
510 cache.setSampledStatisticsEnabled(true);
511 cache.setStatisticsEnabled(true);
512 } catch (RuntimeException e) {
513 throw newPlainException(e);
514 }
515 }
516 }
517
518 /***
519 * {@inheritDoc}
520 *
521 * @see net.sf.ehcache.management.sampled.SampledCacheMBean#disableStatistics()
522 */
523 public void disableStatistics() {
524 if (cache.isStatisticsEnabled()) {
525 try {
526 cache.setSampledStatisticsEnabled(false);
527 cache.setStatisticsEnabled(false);
528 } catch (RuntimeException e) {
529 throw newPlainException(e);
530 }
531 }
532 }
533
534 /***
535 * {@inheritDoc}
536 *
537 * @see net.sf.ehcache.management.sampled.SampledCacheMBean#setStatisticsEnabled(boolean)
538 */
539 public void setStatisticsEnabled(boolean statsEnabled) {
540 boolean oldValue = isStatisticsEnabled();
541 if (oldValue != statsEnabled) {
542 if (statsEnabled) {
543 enableStatistics();
544 } else {
545 disableStatistics();
546 }
547 }
548 }
549
550 /***
551 * {@inheritDoc}
552 *
553 * @see net.sf.ehcache.management.sampled.SampledCacheMBean#enableSampledStatistics()
554 */
555 public void enableSampledStatistics() {
556 if (!cache.isSampledStatisticsEnabled()) {
557 try {
558 cache.setSampledStatisticsEnabled(true);
559 } catch (RuntimeException e) {
560 throw newPlainException(e);
561 }
562 }
563 }
564
565 /***
566 * {@inheritDoc}
567 *
568 * @see net.sf.ehcache.management.sampled.SampledCacheMBean#disableSampledStatistics ()
569 */
570 public void disableSampledStatistics() {
571 if (cache.isSampledStatisticsEnabled()) {
572 try {
573 cache.setSampledStatisticsEnabled(false);
574 } catch (RuntimeException e) {
575 throw newPlainException(e);
576 }
577 }
578 }
579
580 /***
581 * {@inheritDoc}
582 */
583 public float getCacheAverageGetTime() {
584 return getAverageGetTimeMillis();
585 }
586
587 /***
588 * {@inheritDoc}
589 *
590 * @see net.sf.ehcache.management.sampled.SampledCacheMBean#getAverageGetTimeMillis()
591 */
592 public float getAverageGetTimeMillis() {
593 try {
594 return cache.getAverageGetTime();
595 } catch (RuntimeException e) {
596 throw newPlainException(e);
597 }
598 }
599
600 /***
601 * {@inheritDoc}
602 *
603 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getMaxGetTimeMillis()
604 */
605 public long getMaxGetTimeMillis() {
606 try {
607 return cache.getLiveCacheStatistics().getMaxGetTimeMillis();
608 } catch (RuntimeException e) {
609 throw newPlainException(e);
610 }
611 }
612
613 /***
614 * {@inheritDoc}
615 *
616 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getXaCommitCount()
617 */
618 public long getXaCommitCount() {
619 try {
620 return cache.getLiveCacheStatistics().getXaCommitCount();
621 } catch (RuntimeException e) {
622 throw newPlainException(e);
623 }
624 }
625
626 /***
627 * {@inheritDoc}
628 *
629 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getXaRollbackCount()
630 */
631 public long getXaRollbackCount() {
632 try {
633 return cache.getLiveCacheStatistics().getXaRollbackCount();
634 } catch (RuntimeException e) {
635 throw newPlainException(e);
636 }
637 }
638
639 /***
640 * {@inheritDoc}
641 */
642 public boolean getHasWriteBehindWriter() {
643 return cache.getWriterManager() instanceof WriteBehindManager &&
644 cache.getRegisteredCacheWriter() != null;
645 }
646
647 /***
648 * {@inheritDoc}
649 *
650 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getWriterQueueLength()
651 */
652 public long getWriterQueueLength() {
653 try {
654 return cache.getLiveCacheStatistics().getWriterQueueLength();
655 } catch (RuntimeException e) {
656 throw newPlainException(e);
657 }
658 }
659
660 /***
661 * {@inheritDoc}
662 */
663 public int getWriterMaxQueueSize() {
664 return cache.getCacheConfiguration().getCacheWriterConfiguration().getWriteBehindMaxQueueSize();
665 }
666
667 /***
668 * {@inheritDoc}
669 */
670 public int getWriterConcurrency() {
671 return cache.getCacheConfiguration().getCacheWriterConfiguration().getWriteBehindConcurrency();
672 }
673
674 /***
675 * {@inheritDoc}
676 *
677 * @see net.sf.ehcache.statistics.LiveCacheStatistics#getMinGetTimeMillis()
678 */
679 public long getMinGetTimeMillis() {
680 try {
681 return cache.getLiveCacheStatistics().getMinGetTimeMillis();
682 } catch (RuntimeException e) {
683 throw newPlainException(e);
684 }
685 }
686
687 /***
688 * {@inheritDoc}
689 *
690 * @see net.sf.ehcache.management.sampled.SampledCacheMBean#getCacheHitCount()
691 */
692 public long getCacheHitCount() {
693 try {
694 return cache.getLiveCacheStatistics().getCacheHitCount();
695 } catch (RuntimeException e) {
696 throw newPlainException(e);
697 }
698 }
699
700 /***
701 * {@inheritDoc}
702 *
703 * @see net.sf.ehcache.management.sampled.SampledCacheMBean#getCacheMissCount()
704 */
705 public long getCacheMissCount() {
706 try {
707 return cache.getLiveCacheStatistics().getCacheMissCount();
708 } catch (RuntimeException e) {
709 throw newPlainException(e);
710 }
711 }
712
713 /***
714 * {@inheritDoc}
715 *
716 * @see net.sf.ehcache.management.sampled.SampledCacheMBean#getInMemoryMissCount()
717 */
718 public long getInMemoryMissCount() {
719 try {
720 return cache.getLiveCacheStatistics().getInMemoryMissCount();
721 } catch (RuntimeException e) {
722 throw newPlainException(e);
723 }
724 }
725
726 /***
727 * {@inheritDoc}
728 *
729 * @see net.sf.ehcache.management.sampled.SampledCacheMBean#getOffHeapMissCount()
730 */
731 public long getOffHeapMissCount() {
732 try {
733 return cache.getLiveCacheStatistics().getOffHeapMissCount();
734 } catch (RuntimeException e) {
735 throw newPlainException(e);
736 }
737 }
738
739 /***
740 * {@inheritDoc}
741 *
742 * @see net.sf.ehcache.management.sampled.SampledCacheMBean#getOnDiskMissCount()
743 */
744 public long getOnDiskMissCount() {
745 try {
746 return cache.getLiveCacheStatistics().getOnDiskMissCount();
747 } catch (RuntimeException e) {
748 throw newPlainException(e);
749 }
750 }
751
752 /***
753 * {@inheritDoc}
754 *
755 * @see net.sf.ehcache.management.sampled.SampledCacheMBean#getCacheMissCountExpired()
756 */
757 public long getCacheMissCountExpired() {
758 try {
759 return cache.getLiveCacheStatistics().getCacheMissCountExpired();
760 } catch (RuntimeException e) {
761 throw newPlainException(e);
762 }
763 }
764
765 /***
766 * {@inheritDoc}
767 *
768 * @see net.sf.ehcache.management.sampled.SampledCacheMBean#getConfigDiskExpiryThreadIntervalSeconds()
769 */
770 public long getConfigDiskExpiryThreadIntervalSeconds() {
771 return cache.getCacheConfiguration().getDiskExpiryThreadIntervalSeconds();
772 }
773
774 /***
775 * {@inheritDoc}
776 *
777 * @see net.sf.ehcache.management.sampled.SampledCacheMBean#setConfigDiskExpiryThreadIntervalSeconds(long)
778 */
779 public void setConfigDiskExpiryThreadIntervalSeconds(long seconds) {
780 if (getConfigDiskExpiryThreadIntervalSeconds() != seconds) {
781 try {
782 cache.getCacheConfiguration().setDiskExpiryThreadIntervalSeconds(seconds);
783 } catch (RuntimeException e) {
784 throw newPlainException(e);
785 }
786 }
787 }
788
789 /***
790 * {@inheritDoc}
791 *
792 * @see net.sf.ehcache.management.sampled.SampledCacheMBean#getConfigMaxEntriesLocalHeap()
793 */
794 public long getConfigMaxEntriesLocalHeap() {
795 return cache.getCacheConfiguration().getMaxEntriesLocalHeap();
796 }
797
798 /***
799 * {@inheritDoc}
800 *
801 * @see net.sf.ehcache.management.sampled.SampledCacheMBean#getConfigMaxElementsInMemory()
802 */
803 public int getConfigMaxElementsInMemory() {
804 return cache.getCacheConfiguration().getMaxElementsInMemory();
805 }
806
807 /***
808 * {@inheritDoc}
809 *
810 * @see net.sf.ehcache.management.sampled.SampledCacheMBean#setConfigMaxElementsInMemory(int)
811 */
812 public void setConfigMaxElementsInMemory(int maxElements) {
813 if (getConfigMaxElementsInMemory() != maxElements) {
814 try {
815 cache.getCacheConfiguration().setMaxElementsInMemory(maxElements);
816 sendNotification(CACHE_CHANGED, getCacheAttributes(), getImmutableCacheName());
817 } catch (RuntimeException e) {
818 throw newPlainException(e);
819 }
820 }
821 }
822
823 /***
824 * {@inheritDoc}
825 *
826 * @see net.sf.ehcache.management.sampled.SampledCacheMBean#getConfigMaxEntriesLocalDisk()
827 */
828 public long getConfigMaxEntriesLocalDisk() {
829 return cache.getCacheConfiguration().getMaxEntriesLocalDisk();
830 }
831
832 /***
833 * {@inheritDoc}
834 *
835 * @see net.sf.ehcache.management.sampled.SampledCacheMBean#getConfigMaxElementsOnDisk()
836 */
837 public int getConfigMaxElementsOnDisk() {
838 return cache.getCacheConfiguration().getMaxElementsOnDisk();
839 }
840
841 /***
842 * {@inheritDoc}
843 *
844 * @see net.sf.ehcache.management.sampled.SampledCacheMBean#setConfigMaxElementsOnDisk(int)
845 */
846 public void setConfigMaxElementsOnDisk(int maxElements) {
847 if (getConfigMaxElementsOnDisk() != maxElements) {
848 try {
849 cache.getCacheConfiguration().setMaxElementsOnDisk(maxElements);
850 sendNotification(CACHE_CHANGED, getCacheAttributes(), getImmutableCacheName());
851 } catch (RuntimeException e) {
852 throw newPlainException(e);
853 }
854 }
855 }
856
857 /***
858 * {@inheritDoc}
859 *
860 * @see net.sf.ehcache.management.sampled.SampledCacheMBean#getConfigMemoryStoreEvictionPolicy()
861 */
862 public String getConfigMemoryStoreEvictionPolicy() {
863 return cache.getCacheConfiguration().getMemoryStoreEvictionPolicy().toString();
864 }
865
866 /***
867 * {@inheritDoc}
868 *
869 * @see net.sf.ehcache.management.sampled.SampledCacheMBean#setConfigMemoryStoreEvictionPolicy(String)
870 */
871 public void setConfigMemoryStoreEvictionPolicy(String evictionPolicy) {
872 if (!getConfigMemoryStoreEvictionPolicy().equals(evictionPolicy)) {
873 try {
874 cache.getCacheConfiguration().setMemoryStoreEvictionPolicy(evictionPolicy);
875 sendNotification(CACHE_CHANGED, getCacheAttributes(), getImmutableCacheName());
876 } catch (RuntimeException e) {
877 throw newPlainException(e);
878 }
879 }
880 }
881
882 /***
883 * {@inheritDoc}
884 *
885 * @see net.sf.ehcache.management.sampled.SampledCacheMBean#getConfigTimeToIdleSeconds()
886 */
887 public long getConfigTimeToIdleSeconds() {
888 return cache.getCacheConfiguration().getTimeToIdleSeconds();
889 }
890
891 /***
892 * {@inheritDoc}
893 *
894 * @see net.sf.ehcache.management.sampled.SampledCacheMBean#setConfigTimeToIdleSeconds(long)
895 */
896 public void setConfigTimeToIdleSeconds(long tti) {
897 if (getConfigTimeToIdleSeconds() != tti) {
898 try {
899 cache.getCacheConfiguration().setTimeToIdleSeconds(tti);
900 sendNotification(CACHE_CHANGED, getCacheAttributes(), getImmutableCacheName());
901 } catch (RuntimeException e) {
902 throw newPlainException(e);
903 }
904 }
905 }
906
907 /***
908 * {@inheritDoc}
909 *
910 * @see net.sf.ehcache.management.sampled.SampledCacheMBean#getConfigTimeToLiveSeconds()
911 */
912 public long getConfigTimeToLiveSeconds() {
913 return cache.getCacheConfiguration().getTimeToLiveSeconds();
914 }
915
916 /***
917 * {@inheritDoc}
918 *
919 * @see net.sf.ehcache.management.sampled.SampledCacheMBean#setConfigTimeToLiveSeconds(long)
920 */
921 public void setConfigTimeToLiveSeconds(long ttl) {
922 if (getConfigTimeToLiveSeconds() != ttl) {
923 try {
924 cache.getCacheConfiguration().setTimeToLiveSeconds(ttl);
925 sendNotification(CACHE_CHANGED, getCacheAttributes(), getImmutableCacheName());
926 } catch (RuntimeException e) {
927 throw newPlainException(e);
928 }
929 }
930 }
931
932 /***
933 * {@inheritDoc}
934 *
935 * @see net.sf.ehcache.management.sampled.SampledCacheMBean#isConfigDiskPersistent()
936 */
937 public boolean isConfigDiskPersistent() {
938 return cache.getCacheConfiguration().isDiskPersistent();
939 }
940
941 /***
942 * {@inheritDoc}
943 *
944 * @see net.sf.ehcache.management.sampled.SampledCacheMBean#setConfigDiskPersistent(boolean)
945 */
946 public void setConfigDiskPersistent(boolean diskPersistent) {
947 if (isConfigDiskPersistent() != diskPersistent) {
948 try {
949 cache.getCacheConfiguration().setDiskPersistent(diskPersistent);
950 sendNotification(CACHE_CHANGED, getCacheAttributes(), getImmutableCacheName());
951 } catch (RuntimeException e) {
952 throw newPlainException(e);
953 }
954 }
955 }
956
957 /***
958 * {@inheritDoc}
959 *
960 * @see net.sf.ehcache.management.sampled.SampledCacheMBean#isConfigEternal()
961 */
962 public boolean isConfigEternal() {
963 return cache.getCacheConfiguration().isEternal();
964 }
965
966 /***
967 * {@inheritDoc}
968 *
969 * @see net.sf.ehcache.management.sampled.SampledCacheMBean#setConfigEternal(boolean)
970 */
971 public void setConfigEternal(boolean eternal) {
972 if (isConfigEternal() != eternal) {
973 try {
974 cache.getCacheConfiguration().setEternal(eternal);
975 sendNotification(CACHE_CHANGED, getCacheAttributes(), getImmutableCacheName());
976 } catch (RuntimeException e) {
977 throw newPlainException(e);
978 }
979 }
980 }
981
982 /***
983 * {@inheritDoc}
984 *
985 * @see net.sf.ehcache.management.sampled.SampledCacheMBean#isConfigOverflowToDisk()
986 */
987 public boolean isConfigOverflowToDisk() {
988 return cache.getCacheConfiguration().isOverflowToDisk();
989 }
990
991 /***
992 * {@inheritDoc}
993 *
994 * @see net.sf.ehcache.management.sampled.SampledCacheMBean#setConfigOverflowToDisk(boolean)
995 */
996 public void setConfigOverflowToDisk(boolean overflowToDisk) {
997 if (isConfigOverflowToDisk() != overflowToDisk) {
998 try {
999 cache.getCacheConfiguration().setOverflowToDisk(overflowToDisk);
1000 sendNotification(CACHE_CHANGED, getCacheAttributes(), getImmutableCacheName());
1001 } catch (RuntimeException e) {
1002 throw newPlainException(e);
1003 }
1004 }
1005 }
1006
1007 /***
1008 * {@inheritDoc}
1009 *
1010 * @see net.sf.ehcache.management.sampled.SampledCacheMBean#isConfigLoggingEnabled()
1011 */
1012 public boolean isConfigLoggingEnabled() {
1013 return cache.getCacheConfiguration().getLogging();
1014 }
1015
1016 /***
1017 * {@inheritDoc}
1018 *
1019 * @see net.sf.ehcache.management.sampled.SampledCacheMBean#setConfigLoggingEnabled(boolean)
1020 */
1021 public void setConfigLoggingEnabled(boolean enabled) {
1022 if (isConfigLoggingEnabled() != enabled) {
1023 try {
1024 cache.getCacheConfiguration().setLogging(enabled);
1025 sendNotification(CACHE_CHANGED, getCacheAttributes(), getImmutableCacheName());
1026 } catch (RuntimeException e) {
1027 throw newPlainException(e);
1028 }
1029 }
1030 }
1031
1032 /***
1033 * {@inheritDoc}
1034 *
1035 * @see net.sf.ehcache.management.sampled.SampledCacheMBean#getEvictedCount()
1036 */
1037 public long getEvictedCount() {
1038 try {
1039 return cache.getLiveCacheStatistics().getEvictedCount();
1040 } catch (RuntimeException e) {
1041 throw newPlainException(e);
1042 }
1043 }
1044
1045 /***
1046 * {@inheritDoc}
1047 *
1048 * @see net.sf.ehcache.management.sampled.SampledCacheMBean#getExpiredCount()
1049 */
1050 public long getExpiredCount() {
1051 try {
1052 return cache.getLiveCacheStatistics().getExpiredCount();
1053 } catch (RuntimeException e) {
1054 throw newPlainException(e);
1055 }
1056 }
1057
1058 /***
1059 * {@inheritDoc}
1060 *
1061 * @see net.sf.ehcache.management.sampled.SampledCacheMBean#getInMemoryHitCount()
1062 */
1063 public long getInMemoryHitCount() {
1064 try {
1065 return cache.getLiveCacheStatistics().getInMemoryHitCount();
1066 } catch (RuntimeException e) {
1067 throw newPlainException(e);
1068 }
1069 }
1070
1071 /***
1072 * {@inheritDoc}
1073 *
1074 * @see net.sf.ehcache.management.sampled.SampledCacheMBean#getInMemorySize()
1075 * @deprecated use {@link #getLocalHeapSize()}
1076 */
1077 @Deprecated
1078 public long getInMemorySize() {
1079 return getLocalHeapSize();
1080 }
1081
1082 /***
1083 * {@inheritDoc}
1084 *
1085 * @see net.sf.ehcache.management.sampled.SampledCacheMBean#getOffHeapHitCount()
1086 */
1087 public long getOffHeapHitCount() {
1088 try {
1089 return cache.getLiveCacheStatistics().getOffHeapHitCount();
1090 } catch (RuntimeException e) {
1091 throw newPlainException(e);
1092 }
1093 }
1094
1095 /***
1096 * {@inheritDoc}
1097 *
1098 * @see net.sf.ehcache.management.sampled.SampledCacheMBean#getOffHeapSize()
1099 * @deprecated use {@link #getLocalOffHeapSize()}
1100 */
1101 @Deprecated
1102 public long getOffHeapSize() {
1103 return getLocalOffHeapSize();
1104 }
1105
1106 /***
1107 * {@inheritDoc}
1108 *
1109 * @see net.sf.ehcache.management.sampled.SampledCacheMBean#getOnDiskHitCount()
1110 */
1111 public long getOnDiskHitCount() {
1112 try {
1113 return cache.getLiveCacheStatistics().getOnDiskHitCount();
1114 } catch (RuntimeException e) {
1115 throw newPlainException(e);
1116 }
1117 }
1118
1119 /***
1120 * {@inheritDoc}
1121 *
1122 * @see net.sf.ehcache.management.sampled.SampledCacheMBean#getOnDiskSize()
1123 * @deprecated use {@link #getLocalDiskSize()}
1124 */
1125 @Deprecated
1126 public long getOnDiskSize() {
1127 return getLocalDiskSize();
1128 }
1129
1130 /***
1131 * {@inheritDoc}
1132 *
1133 * @see net.sf.ehcache.management.sampled.SampledCacheMBean#getLocalDiskSize()
1134 */
1135 public long getLocalDiskSize() {
1136 try {
1137 return cache.getLiveCacheStatistics().getLocalDiskSize();
1138 } catch (RuntimeException e) {
1139 throw newPlainException(e);
1140 }
1141 }
1142
1143 /***
1144 * {@inheritDoc}
1145 *
1146 * @see net.sf.ehcache.management.sampled.SampledCacheMBean#getLocalHeapSize()
1147 */
1148 public long getLocalHeapSize() {
1149 try {
1150 return cache.getLiveCacheStatistics().getLocalHeapSize();
1151 } catch (RuntimeException e) {
1152 throw newPlainException(e);
1153 }
1154 }
1155
1156 /***
1157 * {@inheritDoc}
1158 *
1159 * @see net.sf.ehcache.management.sampled.SampledCacheMBean#getLocalOffHeapSize()
1160 */
1161 public long getLocalOffHeapSize() {
1162 try {
1163 return cache.getLiveCacheStatistics().getLocalOffHeapSize();
1164 } catch (RuntimeException e) {
1165 throw newPlainException(e);
1166 }
1167 }
1168
1169 /***
1170 * {@inheritDoc}
1171 *
1172 * @see net.sf.ehcache.management.sampled.SampledCacheMBean#getLocalDiskSizeInBytes()
1173 */
1174 public long getLocalDiskSizeInBytes() {
1175 try {
1176 return cache.getLiveCacheStatistics().getLocalDiskSizeInBytes();
1177 } catch (RuntimeException e) {
1178 throw newPlainException(e);
1179 }
1180 }
1181
1182 /***
1183 * {@inheritDoc}
1184 *
1185 * @see net.sf.ehcache.management.sampled.SampledCacheMBean#getLocalHeapSizeInBytes()
1186 */
1187 public long getLocalHeapSizeInBytes() {
1188 try {
1189 return cache.getLiveCacheStatistics().getLocalHeapSizeInBytes();
1190 } catch (RuntimeException e) {
1191 throw newPlainException(e);
1192 }
1193 }
1194
1195 /***
1196 * {@inheritDoc}
1197 *
1198 * @see net.sf.ehcache.management.sampled.SampledCacheMBean#getLocalOffHeapSizeInBytes()
1199 */
1200 public long getLocalOffHeapSizeInBytes() {
1201 try {
1202 return cache.getLiveCacheStatistics().getLocalOffHeapSizeInBytes();
1203 } catch (RuntimeException e) {
1204 throw newPlainException(e);
1205 }
1206 }
1207
1208 /***
1209 * {@inheritDoc}
1210 *
1211 * @see net.sf.ehcache.management.sampled.SampledCacheMBean#getPutCount()
1212 */
1213 public long getPutCount() {
1214 try {
1215 return cache.getLiveCacheStatistics().getPutCount();
1216 } catch (RuntimeException e) {
1217 throw newPlainException(e);
1218 }
1219 }
1220
1221 /***
1222 * {@inheritDoc}
1223 *
1224 * @see net.sf.ehcache.management.sampled.SampledCacheMBean#getRemovedCount()
1225 */
1226 public long getRemovedCount() {
1227 try {
1228 return cache.getLiveCacheStatistics().getRemovedCount();
1229 } catch (RuntimeException e) {
1230 throw newPlainException(e);
1231 }
1232 }
1233
1234 /***
1235 * {@inheritDoc}
1236 *
1237 * @see net.sf.ehcache.management.sampled.SampledCacheMBean#getSize()
1238 */
1239 public long getSize() {
1240 try {
1241 return cache.getLiveCacheStatistics().getSize();
1242 } catch (RuntimeException e) {
1243 throw newPlainException(e);
1244 }
1245 }
1246
1247 /***
1248 * {@inheritDoc}
1249 *
1250 * @see net.sf.ehcache.management.sampled.SampledCacheMBean#getUpdateCount()
1251 */
1252 public long getUpdateCount() {
1253 try {
1254 return cache.getLiveCacheStatistics().getUpdateCount();
1255 } catch (RuntimeException e) {
1256 throw newPlainException(e);
1257 }
1258 }
1259
1260 /***
1261 * getCacheAttributes
1262 *
1263 * @return map of attribute name -> value
1264 */
1265 public Map<String, Object> getCacheAttributes() {
1266 Map<String, Object> result = new HashMap<String, Object>();
1267 result.put("Enabled", isEnabled());
1268 result.put("TerracottaClustered", isTerracottaClustered());
1269 result.put("LoggingEnabled", isConfigLoggingEnabled());
1270 result.put("TimeToIdleSeconds", getConfigTimeToIdleSeconds());
1271 result.put("TimeToLiveSeconds", getConfigTimeToLiveSeconds());
1272 result.put("MaxElementsInMemory", getConfigMaxElementsInMemory());
1273 result.put("MaxElementsOnDisk", getConfigMaxElementsOnDisk());
1274 result.put("DiskPersistent", isConfigDiskPersistent());
1275 result.put("Eternal", isConfigEternal());
1276 result.put("OverflowToDisk", isConfigOverflowToDisk());
1277 result.put("DiskExpiryThreadIntervalSeconds", getConfigDiskExpiryThreadIntervalSeconds());
1278 result.put("MemoryStoreEvictionPolicy", getConfigMemoryStoreEvictionPolicy());
1279 result.put("TerracottaConsistency", getTerracottaConsistency());
1280 if (isTerracottaClustered()) {
1281 result.put("NodeBulkLoadEnabled", isNodeBulkLoadEnabled());
1282 result.put("NodeCoherent", isNodeCoherent());
1283 result.put("ClusterBulkLoadEnabled", isClusterBulkLoadEnabled());
1284 result.put("ClusterCoherent", isClusterCoherent());
1285 }
1286 result.put("StatisticsEnabled", isStatisticsEnabled());
1287 result.put("WriterConcurrency", getWriterConcurrency());
1288 result.put("Transactional", getTransactional());
1289 return result;
1290 }
1291
1292 /***
1293 * @see BaseEmitterBean#getNotificationInfo()
1294 */
1295 @Override
1296 public MBeanNotificationInfo[] getNotificationInfo() {
1297 return NOTIFICATION_INFO;
1298 }
1299
1300 /***
1301 * {@inheritDoc}
1302 */
1303 public void deregistered(CacheConfiguration config) {
1304 /***/
1305 }
1306
1307 /***
1308 * {@inheritDoc}
1309 */
1310 public void diskCapacityChanged(int oldCapacity, int newCapacity) {
1311 if (oldCapacity != newCapacity) {
1312 setConfigMaxElementsOnDisk(newCapacity);
1313 }
1314 }
1315
1316 /***
1317 * {@inheritDoc}
1318 */
1319 public void loggingChanged(boolean oldValue, boolean newValue) {
1320 if (oldValue != newValue) {
1321 setConfigLoggingEnabled(newValue);
1322 }
1323 }
1324
1325 /***
1326 * {@inheritDoc}
1327 */
1328 public void memoryCapacityChanged(int oldCapacity, int newCapacity) {
1329 if (oldCapacity != newCapacity) {
1330 setConfigMaxElementsInMemory(newCapacity);
1331 }
1332 }
1333
1334 /***
1335 * {@inheritDoc}
1336 */
1337 public void registered(CacheConfiguration config) {
1338 /***/
1339 }
1340
1341 /***
1342 * {@inheritDoc}
1343 */
1344 public void timeToIdleChanged(long oldTimeToIdle, long newTimeToIdle) {
1345 if (oldTimeToIdle != newTimeToIdle) {
1346 setConfigTimeToIdleSeconds(newTimeToIdle);
1347 }
1348 }
1349
1350 /***
1351 * {@inheritDoc}
1352 */
1353 public void timeToLiveChanged(long oldTimeToLive, long newTimeToLive) {
1354 if (oldTimeToLive != newTimeToLive) {
1355 setConfigTimeToLiveSeconds(newTimeToLive);
1356 }
1357 }
1358
1359 /***
1360 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
1361 */
1362 public void propertyChange(PropertyChangeEvent evt) {
1363 sendNotification(CACHE_CHANGED, getCacheAttributes(), getImmutableCacheName());
1364 }
1365
1366 /***
1367 * {@inheritDoc}
1368 */
1369 @Override
1370 protected void doDispose() {
1371 cache.getCacheConfiguration().removeConfigurationListener(this);
1372 }
1373
1374 /***
1375 * {@inheritDoc}
1376 */
1377 public long getAverageSearchTime() {
1378 return cache.getAverageSearchTime();
1379 }
1380
1381 /***
1382 * {@inheritDoc}
1383 */
1384 public long getSearchesPerSecond() {
1385 return cache.getSearchesPerSecond();
1386 }
1387
1388 /***
1389 * {@inheritDoc}
1390 */
1391 public boolean getTransactional() {
1392 return cache.getCacheConfiguration().getTransactionalMode().isTransactional();
1393 }
1394
1395 /***
1396 * {@inheritDoc}
1397 */
1398 public boolean getSearchable() {
1399 return cache.getCacheConfiguration().getSearchable() != null;
1400 }
1401
1402 /***
1403 * {@inheritDoc}
1404 */
1405 public long getCacheSearchRate() {
1406 return cache.getSampledCacheStatistics().getSearchesPerSecond();
1407 }
1408
1409 /***
1410 * {@inheritDoc}
1411 */
1412 public long getCacheAverageSearchTime() {
1413 return cache.getSampledCacheStatistics().getAverageSearchTime();
1414 }
1415
1416 /***
1417 * {@inheritDoc}
1418 */
1419 public long getTransactionCommitRate() {
1420 return getCacheXaCommitsMostRecentSample();
1421 }
1422
1423 /***
1424 * {@inheritDoc}
1425 */
1426 public long getCacheXaCommitsMostRecentSample() {
1427 return cache.getSampledCacheStatistics().getCacheXaCommitsMostRecentSample();
1428 }
1429
1430 /***
1431 * {@inheritDoc}
1432 */
1433 public long getTransactionRollbackRate() {
1434 return getCacheXaRollbacksMostRecentSample();
1435 }
1436
1437 /***
1438 * {@inheritDoc}
1439 */
1440 public long getCacheXaRollbacksMostRecentSample() {
1441 return cache.getSampledCacheStatistics().getCacheXaRollbacksMostRecentSample();
1442 }
1443 }