Example usage for org.springframework.context.support ClassPathXmlApplicationContext close

List of usage examples for org.springframework.context.support ClassPathXmlApplicationContext close

Introduction

In this page you can find the example usage for org.springframework.context.support ClassPathXmlApplicationContext close.

Prototype

@Override
public void close() 

Source Link

Document

Close this application context, destroying all beans in its bean factory.

Usage

From source file:org.activiti.crystalball.simulator.OptimizeBottleneckTest.java

@Test
public void testUser2OverLoadedWithHelp3() throws Exception {
    System.setProperty("liveDB", LIVE_DB);
    System.setProperty("_SIM_DB_PATH",
            tempDir + "/simulationRunDB-SimulateBottleNeck-Overload3-" + Thread.currentThread().getId());

    FileUtils.copyFile(new File(LIVE_DB + ".h2.db"), new File(tempDir
            + "/simulationRunDB-SimulateBottleNeck-Overload3-" + Thread.currentThread().getId() + ".h2.db"));

    ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
            "/org/activiti/crystalball/simulator/SimEngine-simulateBottleneck-h2-context.xml");
    ProcessEngine processEngine = (ProcessEngine) appContext.getBean("simProcessEngine");

    // increase frequency of process starts to 45 mins (default is 1 hour)
    StartProcessEventHandler startProcessEventHandler = appContext.getBean(StartProcessEventHandler.class);
    startProcessEventHandler.setPeriod(2700000);
    // add User3 to the Group2 to help user2
    IdentityService identityService = (IdentityService) appContext.getBean("simIdentityService");
    identityService.createMembership("user3", "Group2");

    runSimulation(appContext, tempDir + "/OptimizeBottleneckTest3.png",
            tempDir + "/OptimizeBottleneckTest3-dueDate.png");

    processEngine.close();//  ww w.java 2  s  . co  m
    appContext.close();

    File expected = new File(System.getProperty("baseDir", ".")
            + "/src/test/resources/org/activiti/crystalball/simulator/OptimizeBottleneckTest3.png");
    File generated = new File(tempDir + "/OptimizeBottleneckTest3.png");
    assertTrue(FileUtils.contentEquals(expected, generated));

    expected = new File(System.getProperty("baseDir", ".")
            + "/src/test/resources/org/activiti/crystalball/simulator/OptimizeBottleneckTest3-dueDate.png");
    generated = new File(tempDir + "/OptimizeBottleneckTest3-dueDate.png");
    assertTrue(FileUtils.contentEquals(expected, generated));

    // delete database file
    File f = new File(System.getProperty("_SIM_DB_PATH") + ".h2.db");
    if (!f.delete())
        System.err.println("unable to delete file");
}

From source file:org.activiti.crystalball.simulator.OptimizeBottleneckTest.java

@Test
public void testUser2OverLoadedWithHelp4() throws Exception {
    System.setProperty("liveDB", LIVE_DB);
    System.setProperty("_SIM_DB_PATH",
            tempDir + "/simulationRunDB-SimulateBottleNeck-Overload4-" + Thread.currentThread().getId());

    FileUtils.copyFile(new File(LIVE_DB + ".h2.db"), new File(tempDir
            + "/simulationRunDB-SimulateBottleNeck-Overload4-" + Thread.currentThread().getId() + ".h2.db"));

    ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
            "/org/activiti/crystalball/simulator/SimEngine-simulateBottleneck-h2-context.xml");
    ProcessEngine processEngine = (ProcessEngine) appContext.getBean("simProcessEngine");

    // increase frequency of process starts to 45 mins (default is 1 hour)
    StartProcessEventHandler startProcessEventHandler = appContext.getBean(StartProcessEventHandler.class);
    startProcessEventHandler.setPeriod(2700000);
    // add user4 to the Group2 to help user2
    IdentityService identityService = (IdentityService) appContext.getBean("simIdentityService");

    identityService.createMembership("user4", "Group2");

    runSimulation(appContext, tempDir + "/OptimizeBottleneckTest4.png",
            tempDir + "/OptimizeBottleneckTest4-dueDate.png");

    processEngine.close();//from   w w w . j a va 2 s  .com
    appContext.close();

    File expected = new File(System.getProperty("baseDir", ".")
            + "/src/test/resources/org/activiti/crystalball/simulator/OptimizeBottleneckTest4.png");
    File generated = new File(tempDir + "/OptimizeBottleneckTest4.png");
    assertTrue(FileUtils.contentEquals(expected, generated));

    expected = new File(System.getProperty("baseDir", ".")
            + "/src/test/resources/org/activiti/crystalball/simulator/OptimizeBottleneckTest4-dueDate.png");
    generated = new File(tempDir + "/OptimizeBottleneckTest4-dueDate.png");
    assertTrue(FileUtils.contentEquals(expected, generated));

    // delete database file
    File f = new File(System.getProperty("_SIM_DB_PATH") + ".h2.db");
    if (!f.delete())
        System.err.println("unable to delete file");
}

From source file:org.activiti.crystalball.simulator.impl.simulationexecutor.SimulationRunExecuteJobHandler.java

public void execute(JobEntity job, String configuration, SimulationInstanceEntity simulationInstance,
        CommandContext commandContext) {
    log.log(Level.INFO, "Starting simulation experiment [" + simulationInstance + "] configuration ["
            + configuration + "]");

    SimulationRunEntity simulationRun = commandContext.getSimulationRunManager()
            .findSimulationRunWithReferencesById(configuration);
    SimulationContext.setSimulationRun(simulationRun);

    //initializeSimulationRun
    ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
            simulationRun.getSimulation().getSimulationConfigUrl());
    PropertyPlaceholderConfigurer propConfig = new PropertyPlaceholderConfigurer();
    Properties properties = new Properties();
    properties.put("simulationRunId", configuration);
    propConfig.setProperties(properties);
    appContext.addBeanFactoryPostProcessor(propConfig);
    appContext.refresh();/*  w w w  .java 2  s.  com*/

    SimulationRunHelper runHelper = new NoopSimulationRunHelper();
    if (appContext.containsBean("simulationRunHelper"))
        runHelper = (SimulationRunHelper) appContext.getBean("simulationRunHelper");

    try {
        runHelper.before(configuration);

        SimulationRun simRun = (SimulationRun) appContext.getBean("simulationRun");
        simRun.execute(simulationRun);
    } catch (Exception e) {
        log.log(Level.SEVERE, "SimulationRun handling error" + simulationRun, e);
    } finally {

        runHelper.after(configuration);
        appContext.close();
    }
    job.delete();

    SimulationContext.removeSimulationRun();

    // check whether all jobs for given simulationInstance were already executed.
    simulationInstance.checkActivity();

    log.log(Level.INFO, "finished simulation experiment [" + simulationInstance + "] configuration ["
            + configuration + "]");
}

From source file:org.echocat.jemoni.carbon.spring.Jmx2CarbonBridgeDefinitionParserUnitTest.java

@Test
public void test() throws Exception {
    final ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            "jmx2carbonBridgeTestBeans.xml", Jmx2CarbonBridgeDefinitionParserUnitTest.class);
    try {/*from   www. j a v a 2  s  . c  om*/
        final CarbonWriter carbonWriter = context.getBean("carbonWriter", CarbonWriter.class);
        final ClassLoader classLoader = context.getBean("classLoader", ClassLoader.class);

        final Jmx2CarbonBridge bridge1 = context.getBean("bridge1", Jmx2CarbonBridge.class);
        assertThat(bridge1.getCarbonWriter(), isSameAs(carbonWriter));
        assertThat(bridge1.getClassLoader(), isSameAs(classLoader));
        assertThat(bridge1.getPathPrefix(), is("foo."));
        assertThat(bridge1.getConfiguration(), is(createReferenceRules()));

        final Jmx2CarbonBridge bridge2 = context.getBean(Jmx2CarbonBridge.class.getName(),
                Jmx2CarbonBridge.class);
        assertThat(bridge2.getCarbonWriter(), isSameAs(carbonWriter));
        assertThat(bridge2.getClassLoader(), isSameAs(currentThread().getContextClassLoader()));
        assertThat(bridge2.getConfiguration(), is(null));
    } finally {
        context.close();
    }
}

From source file:com.enterra.batch.admin.sample.BootstrapTests.java

@Test
public void testServletConfiguration() throws Exception {
    ClassPathXmlApplicationContext parent = new ClassPathXmlApplicationContext(
            "classpath:/org/springframework/batch/admin/web/resources/webapp-config.xml");
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            new String[] { "classpath:/org/springframework/batch/admin/web/resources/servlet-config.xml" },
            parent);/*  ww w . j a  v  a  2  s.co m*/

    assertTrue(context.containsBean("jobRepository"));
    String[] beanNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(context.getBeanFactory(),
            JobController.class);
    assertEquals(1, beanNames.length);

    Job job = context.getBean(JobRegistry.class).getJob("job1");
    final JobExecution jobExecution = parent.getBean(JobLauncher.class).run(job,
            new JobParametersBuilder().addString("fail", "false").toJobParameters());

    new DirectPoller<BatchStatus>(100).poll(new Callable<BatchStatus>() {
        public BatchStatus call() throws Exception {
            BatchStatus status = jobExecution.getStatus();
            if (status.isLessThan(BatchStatus.STOPPED) && status != BatchStatus.COMPLETED) {
                return null;
            }
            return status;
        }
    }).get(2000, TimeUnit.MILLISECONDS);

    context.close();
    parent.close();

    assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());

}

From source file:org.activiti.crystalball.simulator.OptimizeBottleneckTest.java

@Test
public void testUser2OverLoadedWithHelp5() throws Exception {
    System.setProperty("liveDB", LIVE_DB);
    System.setProperty("_SIM_DB_PATH",
            tempDir + "/simulationRunDB-SimulateBottleNeck-Overload5-" + Thread.currentThread().getId());

    FileUtils.copyFile(new File(LIVE_DB + ".h2.db"), new File(tempDir
            + "/simulationRunDB-SimulateBottleNeck-Overload5-" + Thread.currentThread().getId() + ".h2.db"));

    ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
            "/org/activiti/crystalball/simulator/SimEngine-simulateBottleneck-h2-context.xml");
    ProcessEngine processEngine = (ProcessEngine) appContext.getBean("simProcessEngine");

    // increase frequency of process starts to 45 mins (default is 1 hour)
    StartProcessEventHandler startProcessEventHandler = appContext.getBean(StartProcessEventHandler.class);
    startProcessEventHandler.setPeriod(2700000);
    // add new user5 to the Group2 to help user2
    IdentityService identityService = (IdentityService) appContext.getBean("simIdentityService");
    identityService.saveUser(identityService.newUser("user5"));
    identityService.createMembership("user5", "Group2");

    runSimulation(appContext, tempDir + "/OptimizeBottleneckTest5.png",
            tempDir + "/OptimizeBottleneckTest5-dueDate.png");

    processEngine.close();/*from  w  ww. j  a  v  a2 s . c o m*/
    appContext.close();

    File expected = new File(System.getProperty("baseDir", ".")
            + "/src/test/resources/org/activiti/crystalball/simulator/OptimizeBottleneckTest5.png");
    File generated = new File(tempDir + "/OptimizeBottleneckTest5.png");
    assertTrue(FileUtils.contentEquals(expected, generated));

    expected = new File(System.getProperty("baseDir", ".")
            + "/src/test/resources/org/activiti/crystalball/simulator/OptimizeBottleneckTest5-dueDate.png");
    generated = new File(tempDir + "/OptimizeBottleneckTest5-dueDate.png");
    assertTrue(FileUtils.contentEquals(expected, generated));

    // delete database file
    File f = new File(System.getProperty("_SIM_DB_PATH") + ".h2.db");
    if (!f.delete())
        System.err.println("unable to delete file");
}

From source file:org.activiti.crystalball.simulator.OptimizeBottleneckTest.java

@Test
public void testUser2OverLoadedWithHelp45() throws Exception {
    System.setProperty("liveDB", LIVE_DB);
    System.setProperty("_SIM_DB_PATH",
            tempDir + "/simulationRunDB-SimulateBottleNeck-Overload45-" + Thread.currentThread().getId());

    FileUtils.copyFile(new File(LIVE_DB + ".h2.db"), new File(tempDir
            + "/simulationRunDB-SimulateBottleNeck-Overload45-" + Thread.currentThread().getId() + ".h2.db"));

    ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
            "/org/activiti/crystalball/simulator/SimEngine-simulateBottleneck-h2-context.xml");
    ProcessEngine processEngine = (ProcessEngine) appContext.getBean("simProcessEngine");

    // increase frequency of process starts to 45 mins (default is 1 hour)
    StartProcessEventHandler startProcessEventHandler = appContext.getBean(StartProcessEventHandler.class);
    startProcessEventHandler.setPeriod(2700000);
    // add new user5 to the Group2 to help user2 and to Group4 to help user4
    IdentityService identityService = (IdentityService) appContext.getBean("simIdentityService");
    identityService.saveUser(identityService.newUser("user5"));
    identityService.createMembership("user5", "Group2");
    identityService.createMembership("user5", "Group4");

    runSimulation(appContext, tempDir + "/OptimizeBottleneckTest45.png",
            tempDir + "/OptimizeBottleneckTest45-dueDate.png");

    processEngine.close();/*from  ww  w.java2s.c  om*/
    appContext.close();

    File expected = new File(System.getProperty("baseDir", ".")
            + "/src/test/resources/org/activiti/crystalball/simulator/OptimizeBottleneckTest45.png");
    File generated = new File(tempDir + "/OptimizeBottleneckTest45.png");
    assertTrue(FileUtils.contentEquals(expected, generated));

    expected = new File(System.getProperty("baseDir", ".")
            + "/src/test/resources/org/activiti/crystalball/simulator/OptimizeBottleneckTest45-dueDate.png");
    generated = new File(tempDir + "/OptimizeBottleneckTest45-dueDate.png");
    assertTrue(FileUtils.contentEquals(expected, generated));

    // delete database file
    File f = new File(System.getProperty("_SIM_DB_PATH") + ".h2.db");
    if (!f.delete())
        System.err.println("unable to delete file");
}

From source file:com.nokia.dempsy.mpcluster.zookeeper.TestFullApp.java

@Test
public void testStartStop() throws Throwable {
    ClassPathXmlApplicationContext actx = null;
    Dempsy dempsy = null;/*ww  w .  ja  v  a2 s . c  o m*/

    try {
        logger.debug("Starting up the appliction context ...");
        actx = new ClassPathXmlApplicationContext(ctx);
        actx.registerShutdownHook();

        dempsy = (Dempsy) actx.getBean("dempsy");
        dempsy.start();

        final FullApplication app = (FullApplication) actx.getBean("app");

        // this checks that the throughput works.
        assertTrue(poll(baseTimeoutMillis * 5, app, new Condition<Object>() {
            @Override
            public boolean conditionMet(Object o) {
                return app.finalMessageCount.get() > 100;
            }
        }));
    } finally {
        if (dempsy != null)
            dempsy.stop();

        if (actx != null)
            actx.close();

        if (dempsy != null)
            assertTrue(dempsy.waitToBeStopped(baseTimeoutMillis));
    }
}

From source file:org.openspaces.itest.persistency.cassandra.archive.CassandaraArchiveOperationHandlerTest.java

private void xmlTest(String relativeXmlName) {

    final boolean refreshNow = false;
    final ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            new String[] { relativeXmlName }, refreshNow);

    PropertyPlaceholderConfigurer propertyConfigurer = new PropertyPlaceholderConfigurer();
    Properties properties = new Properties();
    properties.put("cassandra.keyspace", server.getKeySpaceName());
    properties.put("cassandra.hosts", server.getHost());
    properties.put("cassandra.port", "" + server.getPort());
    properties.put("cassandra.write-consistency", "ALL");
    propertyConfigurer.setProperties(properties);
    context.addBeanFactoryPostProcessor(propertyConfigurer);
    context.refresh();//ww w.  j a va 2 s  . c  o  m

    try {
        final CassandraArchiveOperationHandler archiveHandler = context
                .getBean(CassandraArchiveOperationHandler.class);
        Assert.assertEquals(CassandraConsistencyLevel.ALL, archiveHandler.getWriteConsistency());
        final GigaSpace gigaSpace = context.getBean(org.openspaces.core.GigaSpace.class);
        test(archiveHandler, gigaSpace);
    } finally {
        context.close();
    }
}

From source file:com.aeg.ims.ftp.SftpOutboundTransferSample.java

@Test
public void testOutbound() throws Exception {

    final String sourceFileName = "README.md";
    final String destinationFileName = sourceFileName + "_foo";

    final ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext(
            "/META-INF/spring/integration/SftpOutboundTransferSample-context.xml",
            SftpOutboundTransferSample.class);
    @SuppressWarnings("unchecked")
    SessionFactory<LsEntry> sessionFactory = ac.getBean(CachingSessionFactory.class);
    RemoteFileTemplate<LsEntry> template = new RemoteFileTemplate<LsEntry>(sessionFactory);
    //SftpTestUtils.createTestFiles(template); // Just the directory

    try {/*from   ww  w.java 2s .c o  m*/
        final File file = new File(sourceFileName);

        Assert.isTrue(file.exists(), String.format("File '%s' does not exist.", sourceFileName));

        final Message<File> message = MessageBuilder.withPayload(file).build();
        final MessageChannel inputChannel = ac.getBean("inputChannel", MessageChannel.class);

        inputChannel.send(message);
        Thread.sleep(2000);

        Assert.isTrue(SftpTestUtils.fileExists(template, destinationFileName));

        System.out.println(String.format(
                "Successfully transferred '%s' file to a " + "remote location under the name '%s'",
                sourceFileName, destinationFileName));
    } finally {
        //SftpTestUtils.cleanUp(template, destinationFileName);
        ac.close();
    }
}