|
20 | 20 | import org.eclipse.store.storage.embedded.types.EmbeddedStorageFoundation; |
21 | 21 | import org.springframework.beans.factory.ObjectProvider; |
22 | 22 | import org.springframework.beans.factory.annotation.Autowired; |
| 23 | +import org.springframework.beans.factory.annotation.Value; |
23 | 24 | import org.springframework.boot.autoconfigure.transaction.TransactionManagerCustomizers; |
24 | 25 | import org.springframework.context.annotation.ComponentScan; |
25 | 26 | import org.springframework.context.annotation.Configuration; |
| 27 | +import org.springframework.context.event.ContextClosedEvent; |
| 28 | +import org.springframework.context.event.EventListener; |
26 | 29 | import org.springframework.transaction.PlatformTransactionManager; |
27 | 30 | import org.springframework.transaction.TransactionManager; |
28 | 31 |
|
@@ -112,4 +115,71 @@ public EclipseStoreTransactionManager getTransactionManagerInstance() |
112 | 115 | } |
113 | 116 | return this.transactionManager; |
114 | 117 | } |
| 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 |
115 | 185 | } |
0 commit comments