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:lcn.module.batch.core.launch.support.SchedulerRunner.java

/**
 * Scheduler ./*from   w w w  .ja  v  a 2 s  . c o m*/
 * 
 * Thread.sleep()? ?, Scheduler schedulerJob?   
 * ApplicationContext  ?? delayTime ? .
 * ApplicationContext ?  ? Scheduler   Batch Job? .
 * :  10 Batch Job (Cron ?: 0/10 * * * * ?)
 * ?? xml? : /framework/batch/batch-scheduler-runner-context.xml
 */
public void start() {
    List<String> paths = new ArrayList<String>();

    paths.add(contextPath);
    paths.add(schedulerJobPath);

    //  XML ?? ContextPath? ?.
    for (int index = 0; index < jobPaths.size(); index++) {
        paths.add(jobPaths.get(index));
    }

    String[] locations = paths.toArray(new String[paths.size()]);

    // ApplicationContext ?.
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(locations, false);
    context.refresh();

    logger.warn("ApplicationContext Running Time: " + delayTime / 1000 + " seconds");
    logger.warn("CronTrigger is loaded on Spring ApplicationContext");

    try {
        //  ? ,  ?? Scheduler ?.
        Thread.sleep(delayTime);
    } catch (InterruptedException e) {
        /* interrupted by other thread */
    }

    context.close();
}

From source file:net.sf.oval.test.integration.spring.SpringInjectorTest.java

public void testWithSpringInjector() {
    final ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("SpringInjectorTest.xml",
            SpringInjectorTest.class);
    try {//from w w  w. j  av  a  2 s. c om
        final AnnotationsConfigurer myConfigurer = new AnnotationsConfigurer();
        myConfigurer.addCheckInitializationListener(SpringCheckInitializationListener.INSTANCE);
        final Validator v = new Validator(myConfigurer);

        final Entity e = new Entity();
        assertEquals(1, v.validate(e).size());
        e.field = "whatever";
        assertEquals(0, v.validate(e).size());
    } finally {
        ctx.close();
    }
}

From source file:pl.touk.hades.sql.timemonitoring.HadesSpringContextIT.java

@Test
public void shouldLoadSpringContextForHost1() throws SchedulerException, InterruptedException {

    ClassPathXmlApplicationContext ctx;

    ctx = new ClassPathXmlApplicationContext("/integration/context.xml");
    ctx.getBean("scheduler", Scheduler.class).start();

    String cronAlfa = "1 2 3 * * ?";
    String cronBeta = "2 3 4 * * ?";
    StringFactory.beansByName.put("cronAlfa", cronAlfa);
    StringFactory.beansByName.put("cronBeta", cronBeta);

    assertEquals(cronAlfa, ctx.getBean("monitorAlfa", SqlTimeBasedQuartzMonitor.class).getCron());
    assertEquals(cronBeta, ctx.getBean("monitorBeta", SqlTimeBasedQuartzMonitor.class).getCron());

    ctx.getBean("scheduler", Scheduler.class).shutdown();
    ctx.close();
}

From source file:org.alfresco.util.exec.RuntimeExecBeansTest.java

public void testSplitArguments() throws Exception {
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(APP_CONTEXT_XML);
    try {//from  w w w  .  j  a  va2s  .  c  om
        RuntimeExec splitExec = (RuntimeExec) ctx.getBean("commandSplitArguments");
        assertNotNull(splitExec);
        String[] splitCommand = splitExec.getCommand();
        assertTrue("Command arguments not split into 'dir', '.' and '..' :" + Arrays.deepToString(splitCommand),
                Arrays.deepEquals(new String[] { "dir", ".", ".." }, splitCommand));
    } finally {
        ctx.close();
    }
}

From source file:com.ryantenney.metrics.spring.ReporterTest.java

@Test
public void realReporters() throws Throwable {
    ClassPathXmlApplicationContext ctx = null;
    try {//from   w  w w  . java 2 s  . c om
        ctx = new ClassPathXmlApplicationContext("classpath:reporter-test.xml");
        // intentionally avoids calling ctx.start()

        Assert.assertNotNull(ctx.getBean(ConsoleReporter.class));
        Assert.assertNotNull(ctx.getBean(JmxReporter.class));
        Assert.assertNotNull(ctx.getBean(Slf4jReporter.class));
        Assert.assertNotNull(ctx.getBean(GraphiteReporter.class));
        Assert.assertNotNull(ctx.getBean(GangliaReporter.class));
    } finally {
        if (ctx != null) {
            ctx.close();
        }
    }
}

From source file:com.chevres.rss.restapi.controller.FeedController.java

@CrossOrigin
@RequestMapping(path = "/feed/as_read/{feedId}", method = RequestMethod.POST)
@ResponseBody/*from www.  j a  v  a 2 s.  c om*/
public ResponseEntity<String> markFeedAsRead(@RequestHeader(value = "User-token") String userToken,
        @PathVariable int feedId) {

    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
    FeedDAO feedDAO = context.getBean(FeedDAO.class);
    UserAuthDAO userAuthDAO = context.getBean(UserAuthDAO.class);
    ArticleDAO articleDAO = context.getBean(ArticleDAO.class);
    ArticleStateDAO articleStateDAO = context.getBean(ArticleStateDAO.class);

    UserAuth userAuth = userAuthDAO.findByToken(userToken);
    if (userAuth == null) {
        context.close();
        return new ResponseEntity(new ErrorMessageResponse("invalid_token"), HttpStatus.BAD_REQUEST);
    }

    Feed feed = feedDAO.findById(userAuth, feedId);
    if (feed == null) {
        context.close();
        return new ResponseEntity(HttpStatus.NOT_FOUND);
    }

    ArticleState articleState = articleStateDAO.findByLabel(ArticleState.READ_LABEL);
    articleDAO.markAllArticlesInFeedAsRead(feed, articleState);
    context.close();

    return new ResponseEntity(new SuccessMessageResponse("success"), HttpStatus.OK);
}

From source file:org.lottery.common.message.ProducerTest.java

@Test
public void testSend() {
    final ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(CONFIG);
    ctx.start();//from   w  ww  .j a  v a 2  s .  co  m

    final MessageChannel channel = ctx.getBean("common-message.producer", MessageChannel.class);

    channel.send(MessageBuilder.withPayload("from messageChannel" + System.currentTimeMillis())
            .setHeader("messageKey", "key").setHeader("topic", "test").build());

    MessageProducer messageProducer = ctx.getBean(MessageProducer.class);
    messageProducer.send("test", "from messageProducer" + System.currentTimeMillis());
    try {
        Thread.sleep(10000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    ctx.close();
}

From source file:tools.datasync.core.sampleapp.CamelSeedIntegrationTests.java

private void loadTestObjs(String beansFile) {

    ClassPathXmlApplicationContext appContext = null;
    try {//w w  w. j a v  a 2 s  . c  o m
        appContext = new ClassPathXmlApplicationContext(beansFile);

        syncOrchMgr = appContext.getBean("syncOrchMgr", SyncOrchestrationManager.class);

        sourceDao = appContext.getBean("sourceDao", GenericDao.class);
        targetDao = appContext.getBean("targetDao", GenericDao.class);

    } catch (Exception e) {
        LOG.error("Cannot load app enviornment", e);
        if (appContext != null) {
            appContext.close();
        }
    }

}

From source file:org.alfresco.util.exec.RuntimeExecBeansTest.java

public void testSplitArgumentsAsSingleValue() throws Exception {
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(APP_CONTEXT_XML);
    try {//w  w w  .j  ava2  s . c om
        RuntimeExec splitExec = (RuntimeExec) ctx.getBean("commandSplitArgumentsAsSingleValue");
        assertNotNull(splitExec);
        String[] splitCommand = splitExec.getCommand();
        assertTrue(
                "Command arguments not split into 'dir', '.' and '..' : " + Arrays.deepToString(splitCommand),
                Arrays.deepEquals(new String[] { "dir", ".", ".." }, splitCommand));
    } finally {
        ctx.close();
    }
}

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

private static void generateLiveDB() throws InterruptedException {

    RepositoryService repositoryService;
    IdentityService identityService;/*from   ww  w  .  j  a  va2 s .  c  o  m*/
    ProcessEngine processEngine;
    String LIVE_DB = TEMP_DIR + "/live-SimulateBottleneckTest";

    String previousLiveDB = System.getProperty("liveDB");
    System.setProperty("liveDB", LIVE_DB);

    // delete previous DB instalation and set property to point to the current file
    File prevDB = new File(LIVE_DB + ".h2.db");

    if (prevDB.exists())
        prevDB.delete();
    prevDB = null;

    ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
            "org/activiti/crystalball/simulator/LiveEngine-context.xml");

    repositoryService = appContext.getBean(RepositoryService.class);

    identityService = appContext.getBean(IdentityService.class);
    processEngine = appContext.getBean(ProcessEngine.class);

    // deploy processes
    repositoryService.createDeployment()
            .addClasspathResource("org/activiti/crystalball/simulator/ParallelUserTasksProcess.bpmn").deploy();

    // init identity service
    identityService.saveGroup(identityService.newGroup("Group1"));
    identityService.saveGroup(identityService.newGroup("Group2"));
    identityService.saveGroup(identityService.newGroup("Group3"));
    identityService.saveGroup(identityService.newGroup("Group4"));
    identityService.saveUser(identityService.newUser("user1"));
    identityService.saveUser(identityService.newUser("user2"));
    identityService.saveUser(identityService.newUser("user3"));
    identityService.saveUser(identityService.newUser("user4"));

    identityService.createMembership("user1", "Group1");
    identityService.createMembership("user2", "Group2");
    identityService.createMembership("user3", "Group3");
    identityService.createMembership("user4", "Group4");
    identityService.createUserQuery().list();

    processEngine.close();

    appContext.close();

    if (previousLiveDB != null)
        System.setProperty("liveDB", previousLiveDB);
    else
        System.setProperty("liveDB", "");
}