5

A tutorial on how to embed Neo4j in a java application recommends registering a shutdown hook like so:

Runtime.getRuntime().addShutdownHook( new Thread() {
    // do shutdown work here
});

I'm wondering where the best place to put this code - or in fact any code that needs to run once when Spring starts. Is it simply a case of registering a bean with an init method and putting the code in that?

I'd be interested to know this and more specifically how others have registered a shutdown hook when using an embedded Neo4j in their Spring application.

Community
  • 1
  • 1
chrisjleu
  • 4,329
  • 7
  • 42
  • 55

2 Answers2

8

Just declare your bean for the graphdatabase-service with the correct destroy-method:

<bean id="graphDatabaseService" class="org.neo4j.kernel.EmbeddedGraphDatabase"
        destroy-method="shutdown">
    <constructor-arg index="0" value="data/testdb.db"/>
    <constructor-arg index="1">
        <map>
            <entry key="allow_store_upgrade" value="true"/>
        </map>
    </constructor-arg>
</bean>
Michael Hunger
  • 41,339
  • 3
  • 57
  • 80
  • Have you got an annotation configuration equivalent of this? – chrisjleu Jan 16 '13 at 08:47
  • @Bean public GraphDatabaseService graphDatabaseService() { return new EmbeddedGraphDatabase(storeDir, config); } – Michael Hunger Feb 20 '13 at 09:04
  • 1
    @Bean(destroyMethod = "shutdown") public GraphDatabaseService graphDatabaseService() { return new EmbeddedGraphDatabase(storeDir, config); } – zebeurton Nov 19 '13 at 10:49
  • I found that this might not work in the case of a ctrl+c being issued. That is, the destroy-method isn't called. If you need it to be, you can create a bean that implements `org.springframework.context.ApplicationContextAware` and in the implemented method, do `((ConfigurableApplicationContext) applicationContext).registerShutdownHook();` which will get your context shutdown called when the runtime exits. Under the covers Spring probably calls the Runtime method you mentioned. – Tom Saleeba Mar 24 '15 at 05:38
-3

Using 1.8.1 Neo4j, and SDN 2.1 - the above does not work for my Eclipse/Jetty environment.

Nearly every time I start / stop with Eclipse "server" controls, I see this on startup:

INFO: Non clean shutdown detected on log ...

NOTE: I do not set the "allow-store-upgrade" property though. I thought that had nothing to do with clean shutdown?