Example usage for org.springframework.context.support AbstractXmlApplicationContext registerShutdownHook

List of usage examples for org.springframework.context.support AbstractXmlApplicationContext registerShutdownHook

Introduction

In this page you can find the example usage for org.springframework.context.support AbstractXmlApplicationContext registerShutdownHook.

Prototype

@Override
public void registerShutdownHook() 

Source Link

Document

Register a shutdown hook with the JVM runtime, closing this context on JVM shutdown unless it has already been closed at that time.

Usage

From source file:com.turbospaces.api.EmbeddedJSpaceRunner.java

/**
 * launcher method/*w  w w .jav  a  2  s.  co  m*/
 * 
 * @param args
 *            [1 argument = application context path]
 * @throws Exception
 *             re-throw execution errors if any
 */
public static void main(final String... args) throws Exception {
    JVMUtil.gcOnExit();
    String appContextPath = args[0];
    if (System.getProperty(Global.IPv4) == null && System.getProperty(Global.IPv6) == null)
        System.setProperty(Global.IPv4, Boolean.TRUE.toString());
    System.setProperty(Global.CUSTOM_LOG_FACTORY, JGroupsCustomLoggerFactory.class.getName());

    LOGGER.info("Welcome to turbospaces:version = {}, build date = {}", SpaceUtility.projectVersion(),
            SpaceUtility.projecBuildTimestamp());
    LOGGER.info("{}: launching configuration {}", EmbeddedJSpaceRunner.class.getSimpleName(), appContextPath);
    AbstractXmlApplicationContext c = appContextPath.startsWith("file")
            ? new FileSystemXmlApplicationContext(appContextPath)
            : new ClassPathXmlApplicationContext(appContextPath);
    c.getBeansOfType(JSpace.class);
    c.registerShutdownHook();
    Collection<SpaceConfiguration> configurations = c.getBeansOfType(SpaceConfiguration.class).values();
    for (SpaceConfiguration spaceConfiguration : configurations)
        spaceConfiguration.joinNetwork();
    LOGGER.info("all jspaces joined network, notifying waiting threads...");
    synchronized (joinNetworkMonitor) {
        joinNetworkMonitor.notifyAll();
    }

    while (!Thread.currentThread().isInterrupted())
        synchronized (c) {
            try {
                c.wait(TimeUnit.SECONDS.toMillis(1));
            } catch (InterruptedException e) {
                LOGGER.info("got interruption signal, terminating jspaces... stack_trace = {}",
                        Throwables.getStackTraceAsString(e));
                Thread.currentThread().interrupt();
                Util.printThreads();
            }
        }

    c.destroy();
}

From source file:il.ac.tau.yoavram.pes.SpringRunner.java

public static void run(String[] args) throws IOException {
    System.out.println("Starting " + SpringRunner.class.getSimpleName());
    SimulationConfigurer configurer = new SimulationConfigurer(args);
    if (configurer.getSpringXmlConfig() == null) {
        System.err.println("Spring XML config file not defined");
        System.err.println();//from w  w w . j a v  a 2 s  .com
        System.exit(1);
    }
    if (configurer.getProperties() == null) {
        System.err.println("Properties not defined");
        System.err.println();
        System.exit(1);
    }

    // get the properties
    Properties properties = configurer.getProperties();
    String jobName = properties.getProperty(SimulationConfigurer.JOB_NAME_KEY);

    // create context
    AbstractXmlApplicationContext context = new ClassPathXmlApplicationContext();

    // add properties to context
    logger.info("Adding properties to context: " + properties.toString());
    PropertyPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertyPlaceholderConfigurer();
    propertyPlaceholderConfigurer.setProperties(properties);
    context.addBeanFactoryPostProcessor(propertyPlaceholderConfigurer);

    // set config location
    String configLocation = configurer.getSpringXmlConfig().toString();
    logger.info("Loading context from file " + configLocation);
    context.setConfigLocation(configLocation);

    // make sure destroy methods will be called and refresh the context
    context.registerShutdownHook();
    context.refresh();

    // persist properties
    try {
        PropertiesPersister persister = context.getBean(PropertiesPersister.class);
        if (persister != null) {
            persister.persist(properties);
        }
    } catch (NoSuchBeanDefinitionException e) {
        // nothing to do
    }

    // get the simulation bean and run it
    Simulation simulation = context.getBean("simulation", Simulation.class);

    logger.debug("Starting simulation " + jobName);
    simulation.start();
}