Skip to content

Commit 1af5523

Browse files
committed
Add support for shuting down the storage during application shutdown
1 parent c347c07 commit 1af5523

2 files changed

Lines changed: 76 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# 2.3.0
2+
3+
* Add support for shuting down the storage during application shutdown
4+
* By default only enabled when Spring DevTools are active
5+
* This should fix "StorageExceptionInitialization: Active storage for ... already exists" errors during DevTools restart
6+
17
# 2.2.2
28

39
* Fixed NPE in EclipseSerializerRegisteringCopier

spring-data-eclipse-store/src/main/java/software/xdev/spring/data/eclipse/store/repository/config/EclipseStoreClientConfiguration.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@
2020
import org.eclipse.store.storage.embedded.types.EmbeddedStorageFoundation;
2121
import org.springframework.beans.factory.ObjectProvider;
2222
import org.springframework.beans.factory.annotation.Autowired;
23+
import org.springframework.beans.factory.annotation.Value;
2324
import org.springframework.boot.autoconfigure.transaction.TransactionManagerCustomizers;
2425
import org.springframework.context.annotation.ComponentScan;
2526
import org.springframework.context.annotation.Configuration;
27+
import org.springframework.context.event.ContextClosedEvent;
28+
import org.springframework.context.event.EventListener;
2629
import org.springframework.transaction.PlatformTransactionManager;
2730
import org.springframework.transaction.TransactionManager;
2831

@@ -112,4 +115,71 @@ public EclipseStoreTransactionManager getTransactionManagerInstance()
112115
}
113116
return this.transactionManager;
114117
}
118+
119+
// region On context closed shutdown storage
120+
121+
@Value("${spring-data-eclipse-store.context-close-shutdown-storage.enabled:true}")
122+
protected boolean contextCloseShutdownStorageEnabled;
123+
124+
@Value("${spring-data-eclipse-store.context-close-shutdown-storage.only-when-dev-tools:true}")
125+
protected boolean contextCloseShutdownStorageOnlyWhenDevTools;
126+
127+
/**
128+
* Upstream value from Spring Boot DevTools.
129+
*
130+
* @see org.springframework.boot.devtools.autoconfigure.DevToolsProperties.Restart
131+
*/
132+
@Value("${spring.devtools.restart.enabled:true}")
133+
protected boolean springDevtoolsRestartEnabled;
134+
135+
protected boolean shouldShutdownStorageOnContextClosed()
136+
{
137+
// Did the user disable support for this?
138+
if(!this.contextCloseShutdownStorageEnabled)
139+
{
140+
return false;
141+
}
142+
143+
// Always or only for DevTools?
144+
if(!this.contextCloseShutdownStorageOnlyWhenDevTools)
145+
{
146+
return true;
147+
}
148+
149+
// Spring DevTools loaded?
150+
try
151+
{
152+
Class.forName("org.springframework.boot.devtools.autoconfigure.DevToolsProperties");
153+
}
154+
catch(final ClassNotFoundException e)
155+
{
156+
return false;
157+
}
158+
159+
// Spring Boot DevTools Restart enabled?
160+
return this.springDevtoolsRestartEnabled;
161+
}
162+
163+
/**
164+
* Invoked when the application is "shut down" - or parts of it during a DevTools restart.
165+
* <p>
166+
* Shuts down the storage when it's present and {@link #shouldShutdownStorageOnContextClosed()} is
167+
* <code>true</code>
168+
* </p>
169+
*
170+
* <p>
171+
* This is required for the DevTools restart as it otherwise crashes with <code>StorageExceptionInitialization:
172+
* Active storage for ... already exists</code>
173+
* </p>
174+
*/
175+
@EventListener
176+
public void shutdownStorageOnContextClosed(final ContextClosedEvent event)
177+
{
178+
if(this.storageInstance != null && this.shouldShutdownStorageOnContextClosed())
179+
{
180+
this.storageInstance.stop();
181+
}
182+
}
183+
184+
// endregion
115185
}

0 commit comments

Comments
 (0)