/** * Gets the IElementAttributes attribute of the CacheElement object * <p> * @return The IElementAttributes value, never null */ @Override public IElementAttributes getElementAttributes() { // create default attributes if they are null // this shouldn't happen, but could if a corrupt // object was sent over the wire. if ( this.attr == null ) { this.attr = new ElementAttributes(); } return this.attr; }
/** * Sets the eventHandlers of the IElementAttributes object. * <p> * This add the references to the local list. Subsequent changes in the caller's list will not * be reflected. * <p> * @param eventHandlers List of IElementEventHandler objects */ @Override public void addElementEventHandlers( List<IElementEventHandler> eventHandlers ) { if ( eventHandlers == null ) { return; } for (IElementEventHandler handler : eventHandlers) { addElementEventHandler(handler); } }
/** * For logging and debugging the element IElementAttributes. * <p> * @return String info about the values. */ @Override public String toString() { StringBuilder dump = new StringBuilder(); dump.append( "[ IS_LATERAL = " ).append( IS_LATERAL ); dump.append( ", IS_SPOOL = " ).append( IS_SPOOL ); dump.append( ", IS_REMOTE = " ).append( IS_REMOTE ); dump.append( ", IS_ETERNAL = " ).append( IS_ETERNAL ); dump.append( ", MaxLifeSeconds = " ).append( this.getMaxLife() ); dump.append( ", IdleTime = " ).append( this.getIdleTime() ); dump.append( ", CreateTime = " ).append( this.getCreateTime() ); dump.append( ", LastAccessTime = " ).append( this.getLastAccessTime() ); dump.append( ", getTimeToLiveSeconds() = " ).append( String.valueOf( getTimeToLiveSeconds() ) ); dump.append( ", createTime = " ).append( String.valueOf( createTime ) ).append( " ]" ); return dump.toString(); } }
/** * Gets the time left to live of the IAttributes object. * <p> * This is the (max life + create time) - current time. * @return The TimeToLiveSeconds value */ @Override public long getTimeToLiveSeconds() { final long now = System.currentTimeMillis(); final long timeFactorForMilliseconds = getTimeFactorForMilliseconds(); return ( this.getCreateTime() + this.getMaxLife() * timeFactorForMilliseconds - now ) / 1000; }
/** * Copies the attributes, including references to event handlers. * <p> * @return a copy of the Attributes */ @Override public IElementAttributes copy() { try { // need to make this more efficient. Just want to insure // a proper copy ElementAttributes attr = new ElementAttributes(); attr.setIdleTime( this.getIdleTime() ); attr.setIsEternal( this.getIsEternal() ); attr.setIsLateral( this.getIsLateral() ); attr.setIsRemote( this.getIsRemote() ); attr.setIsSpool( this.getIsSpool() ); attr.setMaxLife(this.getMaxLife()); attr.addElementEventHandlers( this.eventHandlers ); return attr; } catch ( Exception e ) { return new ElementAttributes(); } }
/** verify the check for removal * <p> * @throws IOException */ public void testCheckForRemoval_NotExpired() throws IOException { // SETUP CompositeCacheAttributes cacheAttr = new CompositeCacheAttributes(); cacheAttr.setCacheName("testRegion"); cacheAttr.setMaxMemoryIdleTimeSeconds( 10 ); cacheAttr.setMaxSpoolPerRun( 10 ); CompositeCache<String, String> cache = new CompositeCache<String, String>(cacheAttr, new ElementAttributes()); String key = "key"; String value = "value"; ICacheElement<String, String> element = new CacheElement<String, String>( "testRegion", key, value ); ElementAttributes elementAttr = new ElementAttributes(); elementAttr.setIsEternal( false ); element.setElementAttributes( elementAttr ); element.getElementAttributes().setMaxLife(1); long now = System.currentTimeMillis(); // subtract two seconds now -= 2000; // DO WORK boolean result = cache.isExpired( element, now, ElementEventType.EXCEEDED_MAXLIFE_BACKGROUND, ElementEventType.EXCEEDED_IDLETIME_BACKGROUND ); // VERIFY assertFalse( "Item should not have expired.", result ); }
cacheAttr.setMaxSpoolPerRun( 3 ); CompositeCache<String, String> cache = new CompositeCache<String, String>(cacheAttr, new ElementAttributes()); MockMemoryCache<String, String> memory = (MockMemoryCache<String, String>)cache.getMemoryCache(); ElementAttributes elementAttr = new ElementAttributes(); elementAttr.addElementEventHandler( handler ); elementAttr.setIsEternal( false ); element.setElementAttributes( elementAttr ); element.getElementAttributes().setMaxLife(1);
/** * Test that cloned ElementAttributes have different creation times. * @throws Exception */ public void testElementAttributesCreationTime() throws Exception { ElementAttributes elem1 = new ElementAttributes(); long ctime1 = elem1.getCreateTime(); Thread.sleep(10); IElementAttributes elem2 = elem1.clone(); long ctime2 = elem2.getCreateTime(); assertFalse("Creation times should be different", ctime1 == ctime2); }
/** verify the check for removal * <p> * @throws IOException */ public void testCheckForRemoval_NotExpired() throws IOException { // SETUP CompositeCacheAttributes cacheAttr = new CompositeCacheAttributes(); cacheAttr.setCacheName("testRegion"); cacheAttr.setMaxMemoryIdleTimeSeconds( 10 ); cacheAttr.setMaxSpoolPerRun( 10 ); CompositeCache<String, String> cache = new CompositeCache<String, String>(cacheAttr, new ElementAttributes()); String key = "key"; String value = "value"; ICacheElement<String, String> element = new CacheElement<String, String>( "testRegion", key, value ); ElementAttributes elementAttr = new ElementAttributes(); elementAttr.setIsEternal( false ); element.setElementAttributes( elementAttr ); element.getElementAttributes().setMaxLife(1); long now = System.currentTimeMillis(); // subtract two seconds now -= 2000; // DO WORK boolean result = cache.isExpired( element, now, ElementEventType.EXCEEDED_MAXLIFE_BACKGROUND, ElementEventType.EXCEEDED_IDLETIME_BACKGROUND ); // VERIFY assertFalse( "Item should not have expired.", result ); }
cacheAttr.setMaxSpoolPerRun( 3 ); CompositeCache<String, String> cache = new CompositeCache<String, String>(cacheAttr, new ElementAttributes()); MockMemoryCache<String, String> memory = (MockMemoryCache<String, String>)cache.getMemoryCache(); ElementAttributes elementAttr = new ElementAttributes(); elementAttr.addElementEventHandler( handler ); elementAttr.setIsEternal( false ); element.setElementAttributes( elementAttr ); element.getElementAttributes().setMaxLife(1);
/** * Gets the time left to live of the IAttributes object. * <p> * This is the (max life + create time) - current time. * @return The TimeToLiveSeconds value */ @Override public long getTimeToLiveSeconds() { final long now = System.currentTimeMillis(); final long timeFactorForMilliseconds = getTimeFactorForMilliseconds(); return ( this.getCreateTime() + this.getMaxLife() * timeFactorForMilliseconds - now ) / 1000; }
/** * Gets the IElementAttributes attribute of the CacheElement object * <p> * @return The IElementAttributes value, never null */ @Override public IElementAttributes getElementAttributes() { // create default attributes if they are null // this shouldn't happen, but could if a corrupt // object was sent over the wire. if ( this.attr == null ) { this.attr = new ElementAttributes(); } return this.attr; }
/** verify the check for removal * <p> * @throws IOException */ public void testCheckForRemoval_Expired() throws IOException { // SETUP CompositeCacheAttributes cacheAttr = new CompositeCacheAttributes(); cacheAttr.setCacheName("testRegion"); cacheAttr.setMaxMemoryIdleTimeSeconds( 10 ); cacheAttr.setMaxSpoolPerRun( 10 ); CompositeCache<String, String> cache = new CompositeCache<String, String>(cacheAttr, new ElementAttributes()); String key = "key"; String value = "value"; ICacheElement<String, String> element = new CacheElement<String, String>( "testRegion", key, value ); ElementAttributes elementAttr = new ElementAttributes(); elementAttr.setIsEternal( false ); element.setElementAttributes( elementAttr ); element.getElementAttributes().setMaxLife(1); long now = System.currentTimeMillis(); // add two seconds now += 2000; // DO WORK boolean result = cache.isExpired( element, now, ElementEventType.EXCEEDED_MAXLIFE_BACKGROUND, ElementEventType.EXCEEDED_IDLETIME_BACKGROUND ); // VERIFY assertTrue( "Item should have expired.", result ); }
/** * For logging and debugging the element IElementAttributes. * <p> * @return String info about the values. */ @Override public String toString() { StringBuilder dump = new StringBuilder(); dump.append( "[ IS_LATERAL = " ).append( IS_LATERAL ); dump.append( ", IS_SPOOL = " ).append( IS_SPOOL ); dump.append( ", IS_REMOTE = " ).append( IS_REMOTE ); dump.append( ", IS_ETERNAL = " ).append( IS_ETERNAL ); dump.append( ", MaxLifeSeconds = " ).append( this.getMaxLife() ); dump.append( ", IdleTime = " ).append( this.getIdleTime() ); dump.append( ", CreateTime = " ).append( this.getCreateTime() ); dump.append( ", LastAccessTime = " ).append( this.getLastAccessTime() ); dump.append( ", getTimeToLiveSeconds() = " ).append( String.valueOf( getTimeToLiveSeconds() ) ); dump.append( ", createTime = " ).append( String.valueOf( createTime ) ).append( " ]" ); return dump.toString(); }
/** * Sets the eventHandlers of the IElementAttributes object. * <p> * This add the references to the local list. Subsequent changes in the caller's list will not * be reflected. * <p> * @param eventHandlers List of IElementEventHandler objects */ @Override public void addElementEventHandlers( List<IElementEventHandler> eventHandlers ) { if ( eventHandlers == null ) { return; } for (IElementEventHandler handler : eventHandlers) { addElementEventHandler(handler); } }
new ElementAttributes(), DEFAULT_REGION );
/** verify the check for removal * <p> * @throws IOException */ public void testCheckForRemoval_Expired() throws IOException { // SETUP CompositeCacheAttributes cacheAttr = new CompositeCacheAttributes(); cacheAttr.setCacheName("testRegion"); cacheAttr.setMaxMemoryIdleTimeSeconds( 10 ); cacheAttr.setMaxSpoolPerRun( 10 ); CompositeCache<String, String> cache = new CompositeCache<String, String>(cacheAttr, new ElementAttributes()); String key = "key"; String value = "value"; ICacheElement<String, String> element = new CacheElement<String, String>( "testRegion", key, value ); ElementAttributes elementAttr = new ElementAttributes(); elementAttr.setIsEternal( false ); element.setElementAttributes( elementAttr ); element.getElementAttributes().setMaxLife(1); long now = System.currentTimeMillis(); // add two seconds now += 2000; // DO WORK boolean result = cache.isExpired( element, now, ElementEventType.EXCEEDED_MAXLIFE_BACKGROUND, ElementEventType.EXCEEDED_IDLETIME_BACKGROUND ); // VERIFY assertTrue( "Item should have expired.", result ); }
/** * @param cacheName * @return Returns a CompositeCache */ @Override @SuppressWarnings("unchecked") public <K, V> CompositeCache<K, V> getCache( String cacheName ) { if ( cache == null ) { // System.out.println( "Creating mock cache" ); CompositeCache<K, V> newCache = new CompositeCache<K, V>( new CompositeCacheAttributes(), new ElementAttributes() ); this.setCache( newCache ); } return (CompositeCache<K, V>)cache; }
/** verify the check for removal * <p> * @throws IOException */ public void testCheckForRemoval_IdleTooLong() throws IOException { // SETUP CompositeCacheAttributes cacheAttr = new CompositeCacheAttributes(); cacheAttr.setCacheName("testRegion"); cacheAttr.setMaxMemoryIdleTimeSeconds( 10 ); cacheAttr.setMaxSpoolPerRun( 10 ); CompositeCache<String, String> cache = new CompositeCache<String, String>(cacheAttr, new ElementAttributes()); String key = "key"; String value = "value"; ICacheElement<String, String> element = new CacheElement<String, String>( "testRegion", key, value ); ElementAttributes elementAttr = new ElementAttributes(); elementAttr.setIsEternal( false ); element.setElementAttributes( elementAttr ); element.getElementAttributes().setMaxLife(100); element.getElementAttributes().setIdleTime( 1 ); long now = System.currentTimeMillis(); // add two seconds now += 2000; // DO WORK boolean result = cache.isExpired( element, now, ElementEventType.EXCEEDED_MAXLIFE_BACKGROUND, ElementEventType.EXCEEDED_IDLETIME_BACKGROUND ); // VERIFY assertTrue( "Item should have expired.", result ); }
/** * @param cacheName * @return Returns a CompositeCache */ @Override @SuppressWarnings("unchecked") public <K, V> CompositeCache<K, V> getCache( String cacheName ) { if ( cache == null ) { // System.out.println( "Creating mock cache" ); CompositeCache<K, V> newCache = new CompositeCache<K, V>( new CompositeCacheAttributes(), new ElementAttributes() ); this.setCache( newCache ); } return (CompositeCache<K, V>)cache; }