What is the correct way to register a shutdown hook in Spring 3?

neo4j, spring

Solution

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>

Problem

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.

Original source

Related problems