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.eobjects.datacleaner.monitor.alertnotification.AlertNotificationServiceImplTest.java

public void testNotify() throws Exception {
    final File targetDir = new File("target/example_repo");
    FileUtils.deleteDirectory(targetDir);
    FileUtils.copyDirectory(new File("src/test/resources/example_repo"), targetDir);

    final AtomicInteger counter = new AtomicInteger();

    final ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(
            "context/application-context.xml");
    final AlertNotificationServiceImpl alertNotificationService = (AlertNotificationServiceImpl) applicationContext
            .getBean(AlertNotificationService.class);
    alertNotificationService.getAlertNotifiers().add(new AlertNotifier() {
        @Override//from w ww . j a va2s. c  om
        public void onExecutionFinished(ExecutionLog execution, Ref<Map<AlertDefinition, Number>> activeAlerts,
                ResultContext result) {
            counter.incrementAndGet();

            assertTrue(activeAlerts.get().isEmpty());
        }
    });

    TenantIdentifier tenant = new TenantIdentifier("tenant1");
    JobIdentifier job = new JobIdentifier("product_profiling");
    ScheduleDefinition schedule = new ScheduleDefinition(tenant, job, "orderdb");
    ExecutionLog execution = new ExecutionLog(schedule, TriggerType.MANUAL);
    execution.setResultId("product_profiling-3.analysis.result.dat");
    alertNotificationService.notifySubscribers(execution);

    assertEquals(1, counter.get());

    applicationContext.close();
}

From source file:org.openspaces.itest.archive.ArchiveContainerTest.java

private void xmlTest(String relativeXmlName, int expectedBatchSize) throws InterruptedException {

    final ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(relativeXmlName);
    try {/*from   w w w .  j a  v  a2s.co  m*/
        final MockArchiveOperationsHandler archiveHandler = context.getBean(MockArchiveOperationsHandler.class);
        final GigaSpace gigaSpace = context.getBean(org.openspaces.core.GigaSpace.class);
        ArchivePollingContainer container = getArchivePollingContainer(context);
        Assert.assertTrue("Expected archive container to be configured with use-fifo-grouping=true",
                container.isUseFifoGrouping());
        test(archiveHandler, gigaSpace, container, expectedBatchSize);
    } finally {
        context.close();
    }
}

From source file:org.springframework.batch.admin.BootstrapTests.java

@Test
public void testBootstrapConfiguration() throws Exception {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            new String[] { ClassUtils.addResourcePathToPackagePath(getClass(), "dummy-context.xml"),
                    "classpath:/META-INF/spring/batch/bootstrap/**/*.xml" });
    assertTrue(context.containsBean("jobRepository"));
    context.close();
}

From source file:org.springframework.batch.admin.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);//from  w ww  .  j  a  v  a 2s. c om

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

    MenuManager menuManager = context.getBean(MenuManager.class);
    assertEquals(4, menuManager.getMenus().size());

    context.refresh();

    ClassPathXmlApplicationContext child = new ClassPathXmlApplicationContext(
            new String[] { "classpath:/test-job-context.xml" }, parent);
    Job job = child.getBean(Job.class);
    final JobExecution jobExecution = parent.getBean(JobLauncher.class).run(job, new JobParameters());

    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);

    HomeController metaData = new HomeController();
    metaData.setApplicationContext(context);
    metaData.afterPropertiesSet();
    MockHttpServletRequest request = new MockHttpServletRequest();
    ModelMap model = new ModelMap();
    metaData.getResources(request, model);
    @SuppressWarnings("unchecked")
    List<ResourceInfo> resources = (List<ResourceInfo>) model.get("resources");
    StringBuilder content = new StringBuilder();
    for (ResourceInfo resourceInfo : resources) {
        content.append(resourceInfo.getMethod() + resourceInfo.getUrl() + "=\n");
    }
    FileUtils.writeStringToFile(new File("target/resources.properties"), content.toString());

    HomeController home = context.getBean(HomeController.class);
    // System.err.println(home.getUrlPatterns());
    assertTrue(home.getUrlPatterns().contains("/jobs/{jobName}"));

    String message = context.getMessage("GET/jobs/{jobName}", new Object[0], Locale.getDefault());
    assertTrue("No message for /jobs/{jobName}", StringUtils.hasText(message));

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

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

}

From source file:org.springframework.batch.core.test.step.SplitJobMapRepositoryIntegrationTests.java

@SuppressWarnings("resource")
@Test/*  w  w  w  .  java2 s . c  o m*/
public void testMultithreadedSplit() throws Throwable {

    JobLauncher jobLauncher = null;
    Job job = null;

    ClassPathXmlApplicationContext context = null;

    for (int i = 0; i < MAX_COUNT; i++) {

        if (i % 100 == 0) {
            if (context != null) {
                context.close();
            }
            logger.info("Starting job: " + i);
            context = new ClassPathXmlApplicationContext(getClass().getSimpleName() + "-context.xml",
                    getClass());
            jobLauncher = context.getBean("jobLauncher", JobLauncher.class);
            job = context.getBean("job", Job.class);
        }

        try {
            JobExecution execution = jobLauncher.run(job,
                    new JobParametersBuilder().addLong("count", new Long(i)).toJobParameters());
            assertEquals(BatchStatus.COMPLETED, execution.getStatus());
        } catch (Throwable e) {
            logger.info("Failed on iteration " + i + " of " + MAX_COUNT);
            throw e;
        }

    }

}

From source file:org.springframework.data.neo4j.repository.ReadWriteTests.java

public static void main(String[] args) throws IOException {
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
            "classpath:ReadWriteTests-context.xml");
    boolean delete = false;
    try {/*from   w ww . jav  a 2 s  . c o  m*/
        Neo4jTemplate template = ctx.getBean(Neo4jTemplate.class);
        Car car = findOne(template);
        if (car != null) {
            delete = true;
            assertEquals(Volvo.class, car.getClass());
        } else {
            Transaction tx = template.getGraphDatabase().beginTx();
            Volvo volvo = template.save(new Volvo());
            assertEquals(1, volvo.id.intValue());
            tx.success();
            tx.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
        delete = true;
        throw new RuntimeException(e);
    } finally {
        ctx.close();
        if (delete) {
            FileSystemUtils.deleteRecursively(new File("target/read-write.db"));
        }
    }
}

From source file:org.springframework.integration.core.MessageIdGenerationTests.java

@Test
public void testCustomIdGenerationWithParentRegistrar() throws Exception {
    ClassPathXmlApplicationContext parent = new ClassPathXmlApplicationContext(
            "MessageIdGenerationTests-context-withGenerator.xml", this.getClass());
    ClassPathXmlApplicationContext child = new ClassPathXmlApplicationContext(
            new String[] { "MessageIdGenerationTests-context.xml" }, this.getClass(), parent);

    IdGenerator idGenerator = child.getBean("idGenerator", IdGenerator.class);
    MessageChannel inputChannel = child.getBean("input", MessageChannel.class);
    inputChannel.send(new GenericMessage<Integer>(0));
    verify(idGenerator, atLeastOnce()).generateId();
    child.close();
    parent.close();/* w w  w  .j  a v a 2s  .  co m*/
    this.assertDestroy();
}

From source file:org.springframework.integration.core.MessageIdGenerationTests.java

@Test
public void testCustomIdGenerationWithParentChildIndependentCreation() throws Exception {
    ClassPathXmlApplicationContext parent = new ClassPathXmlApplicationContext(
            "MessageIdGenerationTests-context-withGenerator.xml", this.getClass());
    GenericXmlApplicationContext child = new GenericXmlApplicationContext();
    child.load("classpath:/org/springframework/integration/core/MessageIdGenerationTests-context.xml");
    child.setParent(parent);/*from ww w. ja v a  2  s. c o  m*/
    child.refresh();

    IdGenerator idGenerator = child.getBean("idGenerator", IdGenerator.class);
    MessageChannel inputChannel = child.getBean("input", MessageChannel.class);
    inputChannel.send(new GenericMessage<Integer>(0));
    verify(idGenerator, atLeastOnce()).generateId();
    child.close();
    parent.close();
    this.assertDestroy();
}

From source file:org.springframework.integration.core.MessageIdGenerationTests.java

@Test
public void testCustomIdGenerationWithParentRegistrarClosed() throws Exception {
    ClassPathXmlApplicationContext parent = new ClassPathXmlApplicationContext(
            "MessageIdGenerationTests-context-withGenerator.xml", this.getClass());
    ClassPathXmlApplicationContext child = new ClassPathXmlApplicationContext(
            new String[] { "MessageIdGenerationTests-context.xml" }, this.getClass(), parent);

    IdGenerator idGenerator = child.getBean("idGenerator", IdGenerator.class);
    MessageChannel inputChannel = child.getBean("input", MessageChannel.class);
    inputChannel.send(new GenericMessage<Integer>(0));
    verify(idGenerator, atLeastOnce()).generateId();
    parent.close();
    child.close();//from w  w  w. j  a  v  a  2  s  .  c o m
    this.assertDestroy();
}

From source file:org.springframework.integration.core.MessageIdGenerationTests.java

@Test
public void testCustomIdGenerationWithChildRegistrar() throws Exception {
    ClassPathXmlApplicationContext parent = new ClassPathXmlApplicationContext(
            "MessageIdGenerationTests-context.xml", this.getClass());
    ClassPathXmlApplicationContext child = new ClassPathXmlApplicationContext(
            new String[] { "MessageIdGenerationTests-context-withGenerator.xml" }, this.getClass(), parent);

    IdGenerator idGenerator = child.getBean("idGenerator", IdGenerator.class);
    Mockito.reset(idGenerator);/*from  ww w  . j  av  a 2  s.c  o m*/
    MessageChannel inputChannel = child.getBean("input", MessageChannel.class);
    inputChannel.send(new GenericMessage<Integer>(0));
    verify(idGenerator, atLeastOnce()).generateId();
    child.close();
    parent.close();
    this.assertDestroy();
}