@Override public void destroy() throws Exception { if (this.expireOnDestroy) { if (this.isRunning()) { logger.info("Expiring all messages from message group store: " + this.messageGroupStore); this.messageGroupStore.expireMessageGroups(0); } else { logger.debug("'expireOnDestroy' is set to 'true' but the reaper is not currently running"); } } }
@Override public void stop(Runnable callback) { this.lifecycleLock.lock(); try { this.stop(); callback.run(); } finally { this.lifecycleLock.unlock(); } }
@Override public void stop() { this.lifecycleLock.lock(); try { if (this.running) { this.destroy(); if (logger.isInfoEnabled()) { logger.info("stopped " + this); } } this.running = false; } catch (Exception e) { logger.error("failed to stop bean", e); } finally { this.lifecycleLock.unlock(); } }
@Test public void testSmartLifecycle() throws Exception { GenericMessage<String> testMessage = new GenericMessage<String>("foo"); messageStore2.addMessageToGroup("FOO", testMessage); assertEquals(1, messageStore2.getMessageGroup("FOO").size()); reaper2.setExpireOnDestroy(true); reaper2.setTimeout(0); if (!reaper2.isAutoStartup()) { reaper2.start(); } assertTrue(reaper2.isRunning()); //reaper timeout is set to 0, but need to ensure positive elapsed time Thread.sleep(1L); reaper2.stop(); assertTrue(!reaper2.isRunning()); assertEquals(0, messageStore2.getMessageGroup("FOO").size()); assertEquals(1, expiryCallback2.groups.size()); messageStore2.addMessageToGroup("FOO", testMessage); assertEquals(1, messageStore2.getMessageGroup("FOO").size()); reaper2.run(); assertEquals(1, messageStore2.getMessageGroup("FOO").size()); reaper2.stop(); assertEquals(1, messageStore2.getMessageGroup("FOO").size()); reaper2.start(); reaper2.run(); assertEquals(0, messageStore2.getMessageGroup("FOO").size()); assertEquals(2, expiryCallback2.groups.size()); }
@Bean MessageGroupStoreReaper messageGroupStoreReaper(MessageGroupStore messageStore, InputBindingLifecycle inputBindingLifecycle) { MessageGroupStoreReaper messageGroupStoreReaper = new MessageGroupStoreReaper(messageStore); messageGroupStoreReaper.setPhase(inputBindingLifecycle.getPhase() - 1); messageGroupStoreReaper.setTimeout(properties.getIdleTimeout()); messageGroupStoreReaper.setAutoStartup(true); messageGroupStoreReaper.setExpireOnDestroy(true); return messageGroupStoreReaper; }
@Scheduled(fixedRate=1000) public void reap() { messageGroupStoreReaper.run(); }
@Override protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException { final ApplicationContext applicationContext; try { applicationContext = (ApplicationContext) jobExecutionContext.getScheduler().getContext().get("applicationContext"); ((MessageGroupStoreReaper) applicationContext.getBean("reaper")).run(); } catch (SchedulerException e) { logger.error("Encountered exception while executing reaper job", e); } }
/** * Expire all message groups older than the {@link #setTimeout(long) timeout} provided. Normally this method would * be executed by a scheduled task. */ @Override public void run() { if (this.timeout >= 0 && this.isRunning()) { if (logger.isDebugEnabled()) { logger.debug("Expiring all messages older than timeout=" + this.timeout + " from message group store: " + this.messageGroupStore); } this.messageGroupStore.expireMessageGroups(this.timeout); } }
@Override public void stop(Runnable callback) { this.lifecycleLock.lock(); try { this.stop(); callback.run(); } finally { this.lifecycleLock.unlock(); } }
@Override public void stop() { this.lifecycleLock.lock(); try { if (this.running) { this.destroy(); if (logger.isInfoEnabled()) { logger.info("stopped " + this); } } this.running = false; } catch (Exception e) { logger.error("failed to stop bean", e); } finally { this.lifecycleLock.unlock(); } }
@Override public void destroy() throws Exception { if (this.expireOnDestroy) { if (this.isRunning()) { logger.info("Expiring all messages from message group store: " + this.messageGroupStore); this.messageGroupStore.expireMessageGroups(0); } else { logger.debug("'expireOnDestroy' is set to 'true' but the reaper is not currently running"); } } }
/** * Expire all message groups older than the {@link #setTimeout(long) timeout} provided. Normally this method would * be executed by a scheduled task. */ @Override public void run() { if (this.timeout >= 0 && this.isRunning()) { if (logger.isDebugEnabled()) { logger.debug("Expiring all messages older than timeout=" + this.timeout + " from message group store: " + this.messageGroupStore); } this.messageGroupStore.expireMessageGroups(this.timeout); } }