private void updateWatermark(Serializable value, Comparator comparator) { try { if (watermarkObjectStore.contains(WATERMARK_OS_KEY)) { Serializable currentValue = watermarkObjectStore.retrieve(WATERMARK_OS_KEY); if (compareWatermarks(currentValue, value, comparator) >= 0) { return; } watermarkObjectStore.remove(WATERMARK_OS_KEY); } updateRecentlyProcessedIds(); watermarkObjectStore.store(WATERMARK_OS_KEY, value); } catch (ObjectStoreException e) { throw new MuleRuntimeException( createStaticMessage(format("Failed to update watermark value for message source at location '%s'. %s", flowName, e.getMessage())), e); } }
public static <T extends Exception> T testException(T exception, MuleContext muleContext) { ObjectStore<T> os = getObjectStore(muleContext); try { os.store(key, exception); return os.retrieve(key); } catch (Exception e) { throw new RuntimeException(e); } finally { try { os.clear(); } catch (ObjectStoreException e) { throw new RuntimeException(e); } } }
private void updateRecentlyProcessedIds() throws ObjectStoreException { Lock osClearingLock = lockFactory.createLock(UPDATE_PROCESSED_LOCK); try { osClearingLock.lock(); List<String> strings = recentlyProcessedIds.allKeys(); idsOnUpdatedWatermark.clear(); strings.forEach(key -> { try { idsOnUpdatedWatermark.store(key, recentlyProcessedIds.retrieve(key)); } catch (ObjectStoreException e) { throw new MuleRuntimeException(createStaticMessage("An error occurred while updating the watermark Ids. Failed to update key '%s' in Watermark-IDs ObjectStore: %s", key, e.getMessage()), e); } }); recentlyProcessedIds.clear(); } finally { osClearingLock.unlock(); } }
@Test public void clear() throws ObjectStoreException { String key = this.createKey(); Serializable value = this.getStorableValue(); ObjectStore<Serializable> objectStore = this.getObjectStore(); objectStore.store(key, value); Assert.assertTrue(objectStore.contains(key)); objectStore.clear(); Assert.assertFalse(objectStore.contains(key)); // check it's still usable objectStore.store(key, value); Assert.assertTrue(objectStore.contains(key)); }
@Test public void retrieveCacheWhenExists() throws ObjectStoreException { when(objectStore.contains(SOME_KEY)).thenReturn(true); when(objectStore.retrieve(SOME_KEY)).thenReturn(mockCache); MetadataCache actual = cacheManager.getOrCreateCache(SOME_KEY); assertThat(actual, is(mockCache)); verify(objectStore).contains(SOME_KEY); verify(objectStore).retrieve(SOME_KEY); verify(objectStore, never()).store(anyString(), any(MetadataCache.class)); }
@Test public void expireWithoutMaxEntries() throws Exception { when(settings.getMaxEntries()).thenReturn(empty()); when(settings.getEntryTTL()).thenReturn(of(1L)); StoredObject value = new StoredObject("", 0L, KEY); when(objectStore.retrieve(KEY)).thenReturn(value); when(objectStore.allKeys()).thenReturn(asList(KEY)); when(objectStore.contains(KEY)).thenReturn(true); when(objectStore.remove(KEY)).thenReturn(value); wrapper = new MonitoredObjectStoreWrapper(objectStore, settings); wrapper.expire(); check(5000, 100, () -> { verify(objectStore).remove(KEY); return true; }); } }
@Test public void expirationIntervalWithHighTTLPersistentObjectStore() throws Exception { int maxEntries = 5; int entryTTL = 10000; ObjectStore os = objectStoreFactory.createObjectStore("myOs", maxEntries, entryTTL, 100); os.store("0", 0); ensureMilisecondChanged(); for (int i = 1; i < maxEntries; i++) { os.store(valueOf(i), i); } os.store(OBJECT_KEY, OBJECT_KEY_VALUE_1); Thread.sleep(entryTTL / 5); assertThat(os.allKeys().size(), is(maxEntries)); for (int i = 1; i < maxEntries; i++) { assertThat(os.contains(valueOf(i)), is(true)); } assertThat(os.contains(OBJECT_KEY), is(true)); }
private Serializable getCurrentWatermark() { try { if (watermarkObjectStore.contains(WATERMARK_OS_KEY)) { return watermarkObjectStore.retrieve(WATERMARK_OS_KEY); } else { return null; } } catch (ObjectStoreException e) { throw new MuleRuntimeException( createStaticMessage(format("Failed to fetch watermark for Message source at location '%s'. %s", flowName, e.getMessage())), e); } }
public void storeMoney(ObjectStore<Long> objectStore, Long money) throws Exception { objectStore.store("money", money); }
@Override public CoreEvent process(CoreEvent event) throws MuleException { ObjectStore<String> myPartition = objectStoreManager.getOrCreateObjectStore("myPartition", ObjectStoreSettings.builder().persistent(true).build()); String key = "value"; if (myPartition.contains(key)) { myPartition.remove(key); } myPartition.store(key, key); return event; } }
@Before public void setUp() throws Exception { this.values = new HashMap<>(); this.values.put("fruit", "banana"); this.values.put("icecream", "chocolate"); this.values.put("drink", "coke"); when(this.objectStore.retrieve(Mockito.anyString())).thenAnswer(invocation -> { Serializable value = values.get(invocation.getArguments()[0]); if (value == null) { throw new ObjectDoesNotExistException(); } return value; }); when(this.objectStore.allKeys()).thenReturn(new ArrayList<>(this.values.keySet())); this.producer = new ObjectStoreProducer<>(this.objectStore); }
@Test public void testRetrieveWithNullKey() { try { getObjectStore().retrieve(null); fail("retrieve() called with null key must throw ObjectStoreException"); } catch (ObjectStoreException ose) { // this one was expected } }
@Override public void run() { try { List<Serializable> ids = store.allKeys(); logger.debug("Found " + ids.size() + " objects in store"); for (Serializable id : ids) { boolean deleteEvent = false; String correlationId = (String) id; MultipleRequestReplierEvent multipleEvent = (MultipleRequestReplierEvent) store.retrieve(correlationId); store.remove(correlationId);
if (inflightIdsObjectStore.contains(id)) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Source at flow '{}' polled item '{}', but skipping it since it is already being processed in another " } else { try { inflightIdsObjectStore.store(id, id); callbackContext.addVariable(ITEM_RELEASER_CTX_VAR, new ItemReleaser(id, lock)); return true;
@Test public void testRemoveWithNullKey() { try { getObjectStore().remove(null); fail("remove() called with null key must throw ObjectStoreException"); } catch (ObjectStoreException ose) { // this one was expected } }