Java Code Examples for org.springframework.context.support.AbstractApplicationContext#registerShutdownHook()

The following examples show how to use org.springframework.context.support.AbstractApplicationContext#registerShutdownHook() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TxIntegrationConfig.java    From tutorials with MIT License 6 votes vote down vote up
public static void main(final String... args) {
    final AbstractApplicationContext context = new AnnotationConfigApplicationContext(TxIntegrationConfig.class);
    context.registerShutdownHook();

    final Scanner scanner = new Scanner(System.in);
    System.out.print("Integration flow is running. Type q + <enter> to quit ");
    while (true) {
        final String input = scanner.nextLine();
        if ("q".equals(input.trim())) {
            context.close();
            scanner.close();
            break;
        }
    }
    System.exit(0);
}
 
Example 2
Source File: TxIntegrationTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void whenFileDoesntStartWithFail_thenTxSuccessful() throws InterruptedException, IOException {
    final AbstractApplicationContext context =
            new ClassPathXmlApplicationContext(CONTEXT_CONFIG);

    String fileName = System.getProperty("java.io.tmpdir") + "/tx/test1.txt";
    FileWriter fw = new FileWriter(fileName);
    fw.write("PASSED!");
    fw.close();

    context.registerShutdownHook();
    Thread.sleep(5000);

    File file = new File(fileName + ".PASSED");
    Assert.assertTrue(file.exists());
}
 
Example 3
Source File: TxIntegrationTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void whenFileStartsWithFail_thenTxFailed() {

    String fileName = System.getProperty("java.io.tmpdir") + "/tx/test2.txt";

    try {
        final AbstractApplicationContext context =
                new ClassPathXmlApplicationContext(CONTEXT_CONFIG);

        FileWriter fw = new FileWriter(fileName);
        fw.write("FAILED!");
        fw.close();

        context.registerShutdownHook();
        Thread.sleep(5000);
    } catch (Exception e) {
        // Exception is expected, do nothing
    }

    File file = new File(fileName + ".FAILED");
    Assert.assertTrue(file.exists());
}
 
Example 4
Source File: JavaDSLFileCopyConfig.java    From tutorials with MIT License 5 votes vote down vote up
public static void main(final String... args) {
    final AbstractApplicationContext context = new AnnotationConfigApplicationContext(JavaDSLFileCopyConfig.class);
    context.registerShutdownHook();
    final Scanner scanner = new Scanner(System.in);
    System.out.print("Please enter a string and press <enter>: ");
    while (true) {
        final String input = scanner.nextLine();
        if ("q".equals(input.trim())) {
            context.close();
            scanner.close();
            break;
        }
    }
    System.exit(0);
}
 
Example 5
Source File: FileCopyConfig.java    From tutorials with MIT License 5 votes vote down vote up
public static void main(final String... args) {
    final AbstractApplicationContext context = new AnnotationConfigApplicationContext(FileCopyConfig.class);
    context.registerShutdownHook();
    final Scanner scanner = new Scanner(System.in);
    System.out.print("Please enter a string and press <enter>: ");
    while (true) {
        final String input = scanner.nextLine();
        if ("q".equals(input.trim())) {
            context.close();
            scanner.close();
            break;
        }
    }
    System.exit(0);
}
 
Example 6
Source File: App.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
  AbstractApplicationContext context = new ClassPathXmlApplicationContext(
      "/applicationContext.xml", App.class);
  log.info("Streaming Application Running");
  context.registerShutdownHook();
}
 
Example 7
Source File: App.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
  AbstractApplicationContext context = new ClassPathXmlApplicationContext(
      "/applicationContext.xml", App.class);
  log.info("Streaming Application Running");
  context.registerShutdownHook();
}
 
Example 8
Source File: FileCopyIntegrationTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test
public void whenFileCopyConfiguration_thanFileCopiedSuccessfully() throws InterruptedException {
    final AbstractApplicationContext context = new AnnotationConfigApplicationContext(FileCopyConfig.class.getCanonicalName());
    context.registerShutdownHook();
    Thread.sleep(5000);
}
 
Example 9
Source File: DrawingApp.java    From Spring with Apache License 2.0 3 votes vote down vote up
public static void main(String[] args) {

		final AbstractApplicationContext beanFactory = new ClassPathXmlApplicationContext("src/main/resources/jb/_2_scope_lifecycle/spring.xml");
		beanFactory.registerShutdownHook(); // enable destroying beans (@PreDestroy, implements DisposableBean)

		final Holder holder = beanFactory.getBean("holder", Holder.class);
		System.out.println(holder.getDependency().getName().equals("spring-oore"));  //true

		final Triangle triangleSingleton1 = (Triangle) beanFactory.getBean("triangleSingleton");
		final Triangle triangleSingleton2 = (Triangle) beanFactory.getBean("triangleSingleton");
		System.out.println((triangleSingleton1 == triangleSingleton2));  //true

		final Triangle trianglePrototype1 = (Triangle) beanFactory.getBean("trianglePrototype");
		final Triangle trianglePrototype2 = (Triangle) beanFactory.getBean("trianglePrototype");
		System.out.println((trianglePrototype1 == trianglePrototype2));  //false

		final TriangleAware triangleAware = (TriangleAware) beanFactory.getBean("triangleAware");
		triangleAware.draw();

		final TriangleInheritance triangleInheritance1 = (TriangleInheritance) beanFactory.getBean("triangle1");
		triangleInheritance1.draw();

		final TriangleInheritance triangleInheritance2 = (TriangleInheritance) beanFactory.getBean("triangle2");
		triangleInheritance2.draw();

		final TriangleLifecycle triangleLifecycle = (TriangleLifecycle) beanFactory.getBean("triangleLifecycle");

		final Point pointWithProperty = (Point) beanFactory.getBean("pointWithProperty");
		System.out.println(pointWithProperty);

	}