Example usage for org.springframework.context.event ContextClosedEvent ContextClosedEvent

List of usage examples for org.springframework.context.event ContextClosedEvent ContextClosedEvent

Introduction

In this page you can find the example usage for org.springframework.context.event ContextClosedEvent ContextClosedEvent.

Prototype

public ContextClosedEvent(ApplicationContext source) 

Source Link

Document

Creates a new ContextClosedEvent.

Usage

From source file:org.dspace.app.cris.batch.ScriptDeleteRP.java

/**
 * Batch script to delete a RP. See the technical documentation for further
 * details./*from   ww  w. j  a  v a 2s. co  m*/
 */
public static void main(String[] args) {
    log.info("#### START DELETE: -----" + new Date() + " ----- ####");
    Context dspaceContext = null;
    ApplicationContext context = null;
    try {
        dspaceContext = new Context();
        dspaceContext.turnOffAuthorisationSystem();

        DSpace dspace = new DSpace();
        ApplicationService applicationService = dspace.getServiceManager()
                .getServiceByName("applicationService", ApplicationService.class);

        CommandLineParser parser = new PosixParser();

        Options options = new Options();
        options.addOption("h", "help", false, "help");

        options.addOption("r", "researcher", true, "RP id to delete");

        options.addOption("s", "silent", false, "no interactive mode");

        CommandLine line = parser.parse(options, args);

        if (line.hasOption('h')) {
            HelpFormatter myhelp = new HelpFormatter();
            myhelp.printHelp("ScriptHKURPDelete \n", options);
            System.out.println("\n\nUSAGE:\n ScriptHKURPDelete -r <id> \n");
            System.out.println("Please note: add -s for no interactive mode");
            System.exit(0);
        }

        Integer rpId = null;
        boolean delete = false;
        boolean silent = line.hasOption('s');
        Item[] items = null;
        if (line.hasOption('r')) {
            rpId = ResearcherPageUtils.getRealPersistentIdentifier(line.getOptionValue("r"),
                    ResearcherPage.class);
            ResearcherPage rp = applicationService.get(ResearcherPage.class, rpId);

            if (rp == null) {
                if (!silent) {
                    System.out.println("RP not exist...exit");
                }
                log.info("RP not exist...exit");
                System.exit(0);
            }

            log.info("Use browse indexing");

            BrowseIndex bi = BrowseIndex.getBrowseIndex(plugInBrowserIndex);
            // now start up a browse engine and get it to do the work for us
            BrowseEngine be = new BrowseEngine(dspaceContext);

            String authKey = ResearcherPageUtils.getPersistentIdentifier(rp);

            // set up a BrowseScope and start loading the values into it
            BrowserScope scope = new BrowserScope(dspaceContext);
            scope.setBrowseIndex(bi);
            // scope.setOrder(order);
            scope.setFilterValue(authKey);
            scope.setAuthorityValue(authKey);
            scope.setResultsPerPage(Integer.MAX_VALUE);
            scope.setBrowseLevel(1);

            BrowseInfo binfo = be.browse(scope);
            log.debug("Find " + binfo.getResultCount() + "item(s) for the reseracher " + authKey);
            items = binfo.getItemResults(dspaceContext);

            if (!silent && rp != null) {
                System.out.println(MESSAGE_ONE);

                // interactive mode
                System.out.println("Attempting to remove Researcher Page:");
                System.out.println("StaffNo:" + rp.getSourceID());
                System.out.println("FullName:" + rp.getFullName());
                System.out
                        .println("the researcher has " + items.length + " relation(s) with item(s) in the HUB");
                System.out.println();

                System.out.println(QUESTION_ONE);
                InputStreamReader isr = new InputStreamReader(System.in);
                BufferedReader reader = new BufferedReader(isr);
                String answer = reader.readLine();
                if (answer.equals("yes")) {
                    delete = true;
                } else {
                    System.out.println("Exit without delete");
                    log.info("Exit without delete");
                    System.exit(0);
                }

            } else {
                delete = true;
            }
        } else {
            System.out.println("\n\nUSAGE:\n ScriptHKURPDelete <-v> -r <RPid> \n");
            System.out.println("-r option is mandatory");
            log.error("-r option is mandatory");
            System.exit(1);
        }

        if (delete) {
            if (!silent) {
                System.out.println("Deleting...");
            }
            log.info("Deleting...");
            cleanAuthority(dspaceContext, items, rpId);
            applicationService.delete(ResearcherPage.class, rpId);
            dspaceContext.complete();
        }

        if (!silent) {
            System.out.println("Ok...Bye");
        }
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw new RuntimeException(e.getMessage(), e);
    } finally {
        if (dspaceContext != null && dspaceContext.isValid()) {
            dspaceContext.abort();
        }
        if (context != null) {
            context.publishEvent(new ContextClosedEvent(context));
        }
    }
    log.info("#### END: -----" + new Date() + " ----- ####");
    System.exit(0);
}

From source file:at.ac.univie.isc.asio.nest.ContainerCleanUpTest.java

@Test
public void should_invoke_actions_on_context_closed() throws Exception {
    subject.onApplicationEvent(new ContextClosedEvent(context));
    verify(action).cleanUp(config);/*from   ww w .  ja  v a2 s  .c o  m*/
}

From source file:at.ac.univie.isc.asio.nest.ContainerCleanUpTest.java

@Test
public void should_ignore_close_events_of_other_contexts() throws Exception {
    subject.onApplicationEvent(new ContextClosedEvent(new StaticApplicationContext()));
    verifyZeroInteractions(action);/*from  w  w  w . j ava 2s  .  c o m*/
}

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

public void testBootstrapAndShutdown() throws Exception {
    // now bring up the bootstrap
    ApplicationContext ctx = new ClassPathXmlApplicationContext(APP_CONTEXT_XML);

    // the folder should be gone
    assertFalse("Folder was not deleted by bootstrap", dir.exists());

    // now create the folder again
    dir.mkdir();/*  w w  w . j av  a 2 s. co m*/
    assertTrue("Directory not created", dir.exists());

    // announce that the context is closing
    ctx.publishEvent(new ContextClosedEvent(ctx));

    // the folder should be gone
    assertFalse("Folder was not deleted by shutdown", dir.exists());
}

From source file:org.springframework.amqp.rabbit.listener.DirectMessageListenerContainerIntegrationTests.java

@Test
public void testNonManagedContainerDoesntStartWhenConnectionFactoryDestroyed() throws Exception {
    CachingConnectionFactory cf = new CachingConnectionFactory("localhost");
    ApplicationContext context = mock(ApplicationContext.class);
    cf.setApplicationContext(context);//from w  ww  . j ava2s  . c  o  m

    cf.addConnectionListener(connection -> {
        cf.onApplicationEvent(new ContextClosedEvent(context));
        cf.stop();
        cf.destroy();
    });

    DirectMessageListenerContainer container = new DirectMessageListenerContainer(cf);
    container.setMessageListener(m -> {
    });
    container.setQueueNames(Q1);
    container.setBeanName("stopAfterDestroyBeforeStart");
    container.afterPropertiesSet();
    container.start();

    int n = 0;
    while (n++ < 100 && container.isRunning()) {
        Thread.sleep(100);
    }
    assertFalse(container.isRunning());
}

From source file:org.springframework.amqp.rabbit.listener.DirectMessageListenerContainerIntegrationTests.java

@Test
public void testNonManagedContainerStopsWhenConnectionFactoryDestroyed() throws Exception {
    CachingConnectionFactory cf = new CachingConnectionFactory("localhost");
    ApplicationContext context = mock(ApplicationContext.class);
    cf.setApplicationContext(context);/*from  www .j  av  a  2  s . c o m*/
    DirectMessageListenerContainer container = new DirectMessageListenerContainer(cf);
    final CountDownLatch latch = new CountDownLatch(1);
    container.setMessageListener(m -> {
        latch.countDown();
    });
    container.setQueueNames(Q1);
    container.setBeanName("stopAfterDestroy");
    container.setIdleEventInterval(500);
    container.setFailedDeclarationRetryInterval(500);
    container.afterPropertiesSet();
    container.start();
    new RabbitTemplate(cf).convertAndSend(Q1, "foo");
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    cf.onApplicationEvent(new ContextClosedEvent(context));
    cf.stop();
    cf.destroy();
    int n = 0;
    while (n++ < 100 && container.isRunning()) {
        Thread.sleep(100);
    }
    assertFalse(container.isRunning());
}

From source file:org.springframework.boot.context.logging.LoggingApplicationListenerTests.java

@Test
public void bridgeHandlerLifecycle() {
    assertThat(bridgeHandlerInstalled()).isTrue();
    multicastEvent(new ContextClosedEvent(this.context));
    assertThat(bridgeHandlerInstalled()).isFalse();
}

From source file:org.springframework.boot.context.logging.LoggingApplicationListenerTests.java

@Test
public void closingContextCleansUpLoggingSystem() {
    System.setProperty(LoggingSystem.SYSTEM_PROPERTY, TestCleanupLoggingSystem.class.getName());
    multicastEvent(new ApplicationStartingEvent(this.springApplication, new String[0]));
    TestCleanupLoggingSystem loggingSystem = (TestCleanupLoggingSystem) ReflectionTestUtils
            .getField(this.initializer, "loggingSystem");
    assertThat(loggingSystem.cleanedUp).isFalse();
    multicastEvent(new ContextClosedEvent(this.context));
    assertThat(loggingSystem.cleanedUp).isTrue();
}

From source file:org.springframework.boot.context.logging.LoggingApplicationListenerTests.java

@Test
public void closingChildContextDoesNotCleanUpLoggingSystem() {
    System.setProperty(LoggingSystem.SYSTEM_PROPERTY, TestCleanupLoggingSystem.class.getName());
    multicastEvent(new ApplicationStartingEvent(this.springApplication, new String[0]));
    TestCleanupLoggingSystem loggingSystem = (TestCleanupLoggingSystem) ReflectionTestUtils
            .getField(this.initializer, "loggingSystem");
    assertThat(loggingSystem.cleanedUp).isFalse();
    GenericApplicationContext childContext = new GenericApplicationContext();
    childContext.setParent(this.context);
    multicastEvent(new ContextClosedEvent(childContext));
    assertThat(loggingSystem.cleanedUp).isFalse();
    multicastEvent(new ContextClosedEvent(this.context));
    assertThat(loggingSystem.cleanedUp).isTrue();
    childContext.close();//from  w  w w . j a  va  2s . co m
}

From source file:org.springframework.boot.logging.LoggingApplicationListenerTests.java

@Test
public void bridgeHandlerLifecycle() throws Exception {
    assertTrue(bridgeHandlerInstalled());
    this.initializer.onApplicationEvent(new ContextClosedEvent(this.context));
    assertFalse(bridgeHandlerInstalled());
}