Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

spring

neo4j

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.

like image 706
chrisjleu Avatar asked Jan 16 '13 06:01

chrisjleu


1 Answers

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>
like image 169
Michael Hunger Avatar answered Oct 12 '22 11:10

Michael Hunger