[[q2_shutdown]]

== Shutting down Q2

If we recall <<writing_first_script>>, we have a +QFactory.properties+ file
with some mappings, including a +shutdown+ mapping:

----
shutdown=org.jpos.q2.qbean.Shutdown
----

So shutting down Q2 is as easy as deploying a QBean -- let's call it
+shutdown.xml+ -- with content like this:

[source,xml]
----

    <shutdown />
   
----

[NOTE]
====
The name +shutdown.xml+ can of course be any other name you want.
====

The shutdown QBean is implemented like this: 

[source,java]
----

    package org.jpos.q2.qbean;
    import org.jpos.q2.QBeanSupport;
    public class Shutdown extends QBeanSupport {
        public void startService() {
            getServer().shutdown ();                                 <1>
        }
    }
----
<1> This +getServer()+ method gives us a reference to the Q2 server. 
    It works because +Shutdown+ extends +QBeanSupport+ which in turn
    implements the method +setServer(Q2)+ called by +Q2+ via 
    reflection as described in <<qbean_support>>.

By deploying the +shutdown+ QBean you have a clean way to stop a given 
Q2 instance without knowing its process ID.

jPOS provides a +bin/stop+ script implemented like this:

----
#!/bin/sh

echo Stopping Q2
echo '<shutdown/>' > `dirname $0`/../deploy/shutdown.xml
----

[TIP]
=====
+bin/start+ which in turn calls +bin/q2+ removes +deploy/shutdown.xml+
before starting. If you use this shutdown technique using a shutdown name
other than +shutdown.xml+ and your find yourself in a situation where Q2 
starts and then immediately stops, check the +deploy+ directory for services
deploying the Shutdown service.
=====

