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

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

Introduction

In this page you can find the example usage for org.springframework.context.support GenericXmlApplicationContext 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.rcaspar.fitbitbat.App.java

/**
 * Program entry point.// w ww  .j  a v a2 s  .  c  o  m
 *
 * @param args CLI args
 */
public static void main(final String[] args) {
    log.debug("main() - args={}", Arrays.asList(args));
    final Args arguments = new Args();
    try {
        new JCommander(arguments, args);

        final GenericXmlApplicationContext applicationContext = new GenericXmlApplicationContext(
                "classpath:application-context.xml");
        applicationContext.registerShutdownHook();
        applicationContext.start();

        final Package appPackage = App.class.getPackage();
        log.info("=== {} v.{} started ===", appPackage.getImplementationTitle(),
                appPackage.getImplementationVersion());

        final FitBitBat fitBitBat = applicationContext.getBean(FitBitBat.class);
        if (fitBitBat.checkCredentials(arguments.getPin())) {
            fitBitBat.startAsync();
        } else {
            log.error("Bad pin: {}", arguments.getPin());
        }
    } catch (final OAuthException ex) {
        System.err.println(ex.getMessage());
        System.exit(1);
    } catch (final ParameterException ex) {
        new JCommander(arguments).usage();
        System.exit(-1);
    } catch (final Exception ex) {
        log.error("Cannot startup", ex);
        System.exit(-2);
    }
}

From source file:org.bitcoinrt.Main.java

/**
 * Load the Spring Integration Application Context
 *
 * @param args - command line arguments// w ww.  j  a v  a2s  .  c o m
 */
public static void main(final String... args) {

    final Scanner scanner = new Scanner(System.in);

    System.out.println("\n========================================================="
            + "\n                                                         "
            + "\n    Welcome to the Spring Integration Bitcoin-rt Sample! "
            + "\n                                                         "
            + "\n=========================================================");

    System.out.println("Which WebSocket Client would you like to use? <enter>: ");
    System.out.println("\t1. Use Sonatype's Async HTTP Client implementation");
    System.out.println("\t2. Use Jetty's WebSocket client implementation");
    System.out.println("\t3. Use a Dummy client");

    System.out.println("\tq. Quit the application");
    System.out.print("Enter you choice: ");

    final GenericXmlApplicationContext context = new GenericXmlApplicationContext();

    while (true) {
        final String input = scanner.nextLine();

        if ("1".equals(input.trim())) {
            context.getEnvironment().setActiveProfiles("default");
            break;
        } else if ("2".equals(input.trim())) {
            context.getEnvironment().setActiveProfiles("jetty");
            break;
        } else if ("3".equals(input.trim())) {
            context.getEnvironment().setActiveProfiles("dummy");
            break;
        } else if ("q".equals(input.trim())) {
            System.out.println("Exiting application...bye.");
            System.exit(0);
        } else {
            System.out.println("Invalid choice\n\n");
            System.out.print("Enter you choice: ");
        }
    }

    context.load("classpath:META-INF/spring/integration/*-context.xml");
    context.registerShutdownHook();
    context.refresh();

    final ConnectionBroker connectionBroker = context.getBean(ConnectionBroker.class);

    if (LOGGER.isInfoEnabled()) {
        LOGGER.info("\n========================================================="
                + "\n                                                         "
                + "\n    Please press 'q + Enter' to quit the application.    "
                + "\n    For statistical information press 'i + Enter'.       "
                + "\n                                                         "
                + "\n    In your browser open:                                "
                + "\n    file:///.../src/main/webapp/index.html               "
                + "\n=========================================================");
    }

    while (true) {

        final String input = scanner.nextLine();

        if ("q".equals(input.trim())) {
            break;
        } else if ("i".equals(input.trim())) {
            LOGGER.info("\n========================================================="
                    + "\n                                                         "
                    + "\n Number of connected clients: " + connectionBroker.connectedClients()
                    + "\n                                                         "
                    + "\n=========================================================");
        }

    }

    if (LOGGER.isInfoEnabled()) {
        LOGGER.info("Exiting application...bye.");
    }

    context.close();
    System.exit(0);

}

From source file:com.richard.memorystore.udp.UDPServer.java

public static GenericXmlApplicationContext setupContext() {
    final GenericXmlApplicationContext context = new GenericXmlApplicationContext();

    System.out.print("Detect open server socket...");
    int availableServerSocket = SocketUtils.findAvailableServerSocket(11111);

    final Map<String, Object> sockets = new HashMap<String, Object>();
    sockets.put("availableServerSocket", availableServerSocket);

    final MapPropertySource propertySource = new MapPropertySource("sockets", sockets);

    context.getEnvironment().getPropertySources().addLast(propertySource);

    System.out.println("using port " + context.getEnvironment().getProperty("availableServerSocket"));

    context.load("classpath:udpServer.xml");
    context.registerShutdownHook();
    context.refresh();/* w ww  . j a v  a 2  s  .  co m*/

    return context;
}

From source file:com.richard.memorystore.tcp.TcpServer.java

public static GenericXmlApplicationContext setupContext() {
    final GenericXmlApplicationContext context = new GenericXmlApplicationContext();

    System.out.print("Detect open server socket...");
    int availableServerSocket = SocketUtils.findAvailableServerSocket(5678);

    final Map<String, Object> sockets = new HashMap<String, Object>();
    sockets.put("availableServerSocket", availableServerSocket);

    final MapPropertySource propertySource = new MapPropertySource("sockets", sockets);

    context.getEnvironment().getPropertySources().addLast(propertySource);

    System.out.println("using port " + context.getEnvironment().getProperty("availableServerSocket"));

    context.load("classpath:tcpClientServerDemo-context.xml");
    context.registerShutdownHook();
    context.refresh();//  w w w  .j  a v a2s . co m

    return context;
}

From source file:org.musa.tcpserver.Main.java

public static GenericXmlApplicationContext setupContext() {
    final GenericXmlApplicationContext context = new GenericXmlApplicationContext();

    System.out.print("Detect open server socket...");
    int availableServerSocket = SocketUtils.findAvailableServerSocket(5678);

    final Map<String, Object> sockets = new HashMap<String, Object>();
    sockets.put("availableServerSocket", availableServerSocket);

    final MapPropertySource propertySource = new MapPropertySource("sockets", sockets);

    context.getEnvironment().getPropertySources().addLast(propertySource);

    System.out.println("using port " + context.getEnvironment().getProperty("availableServerSocket"));

    context.load("classpath:META-INF/spring/integration/serverContext.xml");

    context.registerShutdownHook();
    context.refresh();/*  w w  w  . j a  va2 s. co  m*/

    return context;
}

From source file:com.haythem.integration.Main.java

public static GenericXmlApplicationContext setupContext() {
    final GenericXmlApplicationContext context = new GenericXmlApplicationContext();

    System.out.print("Detect open server socket...");
    int availableServerSocket = SocketUtils.findAvailableServerSocket(5678);

    final Map<String, Object> sockets = new HashMap<String, Object>();
    sockets.put("availableServerSocket", availableServerSocket);

    final MapPropertySource propertySource = new MapPropertySource("sockets", sockets);

    context.getEnvironment().getPropertySources().addLast(propertySource);

    System.out.println("using port " + context.getEnvironment().getProperty("availableServerSocket"));

    context.load("classpath:META-INF/spring/integration/tcpClientServerDemo-context.xml");
    context.registerShutdownHook();
    context.refresh();//from  w  w w. j  a  v  a 2s .c  om

    return context;
}

From source file:com.tcp.client.Main.java

public static GenericXmlApplicationContext setupContext() throws InterruptedException, IOException {

    int availableServerSocket = 5682;
    final GenericXmlApplicationContext context = new GenericXmlApplicationContext();

    System.out.println("===== Player 2 =====");

    while (true) {
        try {//from  ww w .  ja va2s  .c o m
            ServerSocket s = new ServerSocket(availableServerSocket);
            s.close();
            System.out.println("Player 1 is unavailable.. trying again!!");
            Thread.sleep(2000);

        } catch (IOException e) {
            System.out.println("Found player 1!!");
            break;
        }
    }

    final Map<String, Object> sockets = new HashMap<String, Object>();
    sockets.put("availableServerSocket", availableServerSocket);

    final MapPropertySource propertySource = new MapPropertySource("sockets", sockets);

    context.getEnvironment().getPropertySources().addLast(propertySource);
    context.load("classpath:META-INF/context.xml");
    context.registerShutdownHook();
    context.refresh();

    return context;
}

From source file:org.objectrepository.MessageConsumerDaemon.java

public void init() {

    log.info("Startup service...");
    GenericXmlApplicationContext context = new GenericXmlApplicationContext();
    context.setValidating(false);//from  ww w . ja  va 2s .  c  o m
    context.load("/META-INF/spring/application-context.xml", "META-INF/spring/dispatcher-servlet.xml");
    context.refresh();
    setContext(context);
    context.registerShutdownHook();

    final RejectedExecutionHandler rejectedExecutionHandler = context.getBean(RejectedExecutionHandler.class);
    for (Queue taskExecutor : taskExecutors) {
        taskExecutor.setRejectedExecutionHandler(rejectedExecutionHandler);
        taskExecutor.initialize();
        log.info("Initialized " + taskExecutor.getQueueName());
    }
}

From source file:nz.co.jsrsolutions.tideservice.scraper.TideScraper.java

public static void main(String[] args) {

    logger.info("Starting application [ tide scraper ] ...");

    GenericXmlApplicationContext context = null;

    try {/* w  w  w.  j av  a  2  s  .c  o m*/

        CommandLineParser parser = new GnuParser();

        CommandLine commandLine = parser.parse(CommandLineOptions.Options, args);

        if (commandLine.getOptions().length > 0 && !commandLine.hasOption(CommandLineOptions.HELP)) {

            context = new GenericXmlApplicationContext();

            //ConfigurableEnvironment env = ctx.getEnvironment();

            //env.setActiveProfiles("test1");

            context.load(SPRING_CONFIG);

            context.refresh();

            context.registerShutdownHook();

            if (commandLine.hasOption(CommandLineOptions.SCHEDULED)) {

                Scheduler scheduler = context.getBean(SCHEDULER_BEAN_ID, Scheduler.class);
                scheduler.start();

                Object lock = new Object();
                synchronized (lock) {
                    lock.wait();
                }

            } else {
                TideScraperController controller = context.getBean(CONTROLLER_BEAN_ID,
                        TideScraperController.class);
                controller.executeCommandLine(commandLine);
            }

        } else {

            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("ts", CommandLineOptions.Options);

        }

    } catch (TideScraperException tse) {
        logger.error("Failed to execute command", tse);
    } catch (ParseException pe) {

        logger.error("Failed to parse command line", pe);

    } catch (Exception e) {

        logger.error("Failed to execute command", e);

    } finally {
        if (context != null) {
            context.close();
        }
    }

    logger.info("Exiting application [ tide scraper ] ...");

}

From source file:org.springframework.integration.samples.smb.Main.java

/**
 * Load the Spring Integration Application Context
 *
 * @param args - command line arguments/*  w  w w .java2  s . co m*/
 */
public static void main(final String... args) {

    final Scanner scanner = new Scanner(System.in);

    if (LOGGER.isInfoEnabled()) {
        LOGGER.info("\n========================================================="
                + "\n                                                         "
                + "\n     Welcome to the Spring Integration Smb Sample        "
                + "\n                                                         "
                + "\n    For more information please visit:                   "
                + "\nhttps://github.com/SpringSource/spring-integration-extensions"
                + "\n                                                         "
                + "\n=========================================================");
    }

    final GenericXmlApplicationContext context = new GenericXmlApplicationContext();

    System.out.println("Please enter the: ");
    System.out.println("\t- SMB Host");
    System.out.println("\t- SMB Share and Directory");
    System.out.println("\t- SMB Username");
    System.out.println("\t- SMB Password");

    System.out.print("Host: ");
    final String host = scanner.nextLine();

    System.out.print("Share and Directory (e.g. myFile/path/to/): ");
    final String shareAndDir = scanner.nextLine();

    System.out.print("Username (e.g. guest): ");
    final String username = scanner.nextLine();

    System.out.print("Password (can be empty): ");
    final String password = scanner.nextLine();

    context.getEnvironment().getSystemProperties().put("host", host);
    context.getEnvironment().getSystemProperties().put("shareAndDir", shareAndDir);
    context.getEnvironment().getSystemProperties().put("username", username);
    context.getEnvironment().getSystemProperties().put("password", password);

    context.load("classpath:META-INF/spring/integration/*-context.xml");
    context.registerShutdownHook();
    context.refresh();

    context.registerShutdownHook();

    if (LOGGER.isInfoEnabled()) {
        LOGGER.info("\n========================================================="
                + "\n                                                         "
                + "\n    Please press 'q + Enter' to quit the application.    "
                + "\n                                                         "
                + "\n=========================================================");
    }

    SmbSessionFactory smbSessionFactory = context.getBean("smbSession", SmbSessionFactory.class);

    System.out.println("Polling from Share: " + smbSessionFactory.getUrl());

    while (true) {

        final String input = scanner.nextLine();

        if ("q".equals(input.trim())) {
            break;
        }

    }

    if (LOGGER.isInfoEnabled()) {
        LOGGER.info("Exiting application...bye.");
    }

    System.exit(0);

}