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

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

Introduction

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

Prototype

public ContextStoppedEvent(ApplicationContext source) 

Source Link

Document

Create a new ContextStoppedEvent.

Usage

From source file:org.alfresco.bm.server.EventController.java

/**
 * Do the actual run but without concern for exceptions, which will be logged.
 *///  www .j  a  va 2s . c o m
private void runImpl() {
    Set<String> staleDrivers = new HashSet<String>(3); // Keep track of any stale drivers

    int eventsPerSecond = (threadCount * eventsPerSecondPerThread);
    String msgStarted = "Event processing started: " + testRunFqn + " (" + eventsPerSecond
            + " events per second using " + threadCount + " threads)";
    logger.info("\t" + msgStarted);
    logService.log(LogLevel.INFO, msgStarted);

    // Keep details on when we started looking for events
    long eventProcessStartTime = System.currentTimeMillis();
    int eventSearchesPerformed = 0;

    runStateChanged: while (isRunning()) {
        long eventProcessSearchTime = System.currentTimeMillis();
        // Make sure we don't look for events too frequently
        while (true) {
            if (!isRunning()) {
                break runStateChanged;
            }
            // Calculate how many searches we are allowed to have performed
            long eventProcessElapsedTime = eventProcessSearchTime - eventProcessStartTime;
            int eventSearchesAllowed = (int) Math.floor((eventProcessElapsedTime / 1000.0) * eventsPerSecond);
            if (eventSearchesPerformed < eventSearchesAllowed) {
                // Yield to other threads
                Thread.yield();
                // We are allowed to do more
                break;
            }
            // We need to wait and allow enough time to elapse.
            // We cut the mean time between checks in half
            long toSleep = (long) (1000L / eventsPerSecond) / 2;
            toSleep = (toSleep < 10L) ? 10L : toSleep;
            synchronized (this) {
                try {
                    this.wait(toSleep);
                } catch (InterruptedException e) {
                }
            }
            // Now go back around the see if we are allowed to proceed
            eventProcessSearchTime = System.currentTimeMillis();
        }
        // We record the event search regardless of missing or hit in the queue
        eventSearchesPerformed++;
        // Grab an event
        // First look for events specific to this driver
        Event event = eventService.nextEvent(driverId, eventProcessSearchTime);
        if (event == null) {
            // Nothing found for the driver.
            // Look for events from other drivers, giving them a grace period
            event = eventService.nextEvent(null, eventProcessSearchTime - assignedEventGracePeriod);
            if (event != null) {
                String driver = event.getDriver();
                if (staleDrivers.add(driver)) {
                    logger.error("Driver " + driver + " is leaving stale events.  Check server load.");
                }
            }
        }
        // Do we have an event to process?
        if (event == null) {
            long count = eventService.count();
            if (count == 0) {
                // Look in the results to see if the run was started at some point
                List<EventRecord> startRecords = resultService.getResults(Event.EVENT_NAME_START, 0, 1);
                if (startRecords.size() == 0) {
                    // The test has not *ever* been started.
                    // We do that now; note that the event name will enforce a unique ID
                    event = new Event(Event.EVENT_NAME_START, 0L, null);
                    try {
                        eventService.putEvent(event);
                        // There is no guarantee that it actually went in
                    } catch (RuntimeException e) {
                        // We were unable to start the whole process.
                        // We assume that someone else has.
                    }
                } else {
                    // The test was started but there are no more events remaining.
                    // Quit
                    if (ctx != null) // The controller might have been run manually
                    {
                        ctx.publishEvent(new ContextStoppedEvent(ctx));
                    }
                }
            }
            // Go back to the queue
            continue;
        }
        // Find the processor for the event
        EventProcessor processor = getProcessor(event);

        // Schedule it
        EventWork work = new EventWork(driverId, testRunFqn, event, driverIds, processor, eventProducers,
                eventService, resultService, sessionService, logService);
        try {
            // Grabbing an event automatically applies a short-lived lock to prevent
            // any other drivers from grabbing the same event before the event is locked
            // for execution.
            executor.execute(work);
        } catch (RejectedExecutionException e) {
            // Should not occur as the caller executes
            eventSearchesPerformed += threadCount;
            // Log it
            logService.log(LogLevel.WARN, "EventController's execution of an event was rejected.  "
                    + "Are there enough drivers to handle the event load?");
        } catch (RuntimeException e) {
            // Put here in case a CallerRunsPolicy is used
            logger.error("execute failed (pool or CallerRunsPolicy)", e);
        }
    }

    String msgStopped = "Event processing stopped: " + testRunFqn;
    logger.info("\t" + msgStopped);
    logService.log(LogLevel.INFO, msgStopped);
}

From source file:org.springframework.context.support.AbstractApplicationContext.java

@Override
public void stop() {
    getLifecycleProcessor().stop();
    publishEvent(new ContextStoppedEvent(this));
}

From source file:org.springframework.data.gemfire.support.SpringContextBootstrappingInitializerTest.java

@Test
public void testOnApplicationEventAndNotifyOnExistingContextRefreshedEvent() {
    ConfigurableApplicationContext mockApplicationContext = mock(ConfigurableApplicationContext.class,
            "testOnApplicationEventAndNotifyOnExistingContextRefreshedEvent");

    SpringContextBootstrappingInitializer initializer = new SpringContextBootstrappingInitializer();

    TestApplicationListener testApplicationListenerOne = new TestApplicationListener(
            "testOnApplicationEventAndNotifyOnExistingContextRefreshedEvent.1");

    TestApplicationListener testApplicationListenerTwo = new TestApplicationListener(
            "testOnApplicationEventAndNotifyOnExistingContextRefreshedEvent.2");

    TestApplicationListener testApplicationListenerThree = new TestApplicationListener(
            "testOnApplicationEventAndNotifyOnExistingContextRefreshedEvent.3");

    try {/*from w  w w.  java2s  .  com*/
        testApplicationListenerOne = SpringContextBootstrappingInitializer.register(testApplicationListenerOne);

        assertUnnotified(testApplicationListenerOne);
        assertUnnotified(testApplicationListenerTwo);
        assertUnnotified(testApplicationListenerThree);

        ContextRefreshedEvent testContextRefreshedEvent = new ContextRefreshedEvent(mockApplicationContext);

        initializer.onApplicationEvent(testContextRefreshedEvent);

        assertNotified(testApplicationListenerOne, testContextRefreshedEvent);
        assertUnnotified(testApplicationListenerTwo);
        assertUnnotified(testApplicationListenerThree);

        testApplicationListenerTwo = SpringContextBootstrappingInitializer.register(testApplicationListenerTwo);

        assertNotified(testApplicationListenerTwo, testContextRefreshedEvent);
        assertUnnotified(testApplicationListenerOne);
        assertUnnotified(testApplicationListenerThree);

        ContextStoppedEvent testContextStoppedEvent = new ContextStoppedEvent(mockApplicationContext);

        initializer.onApplicationEvent(testContextStoppedEvent);

        assertUnnotified(testApplicationListenerOne);
        assertUnnotified(testApplicationListenerTwo);
        assertUnnotified(testApplicationListenerThree);

        initializer.onApplicationEvent(testContextRefreshedEvent);

        assertNotified(testApplicationListenerOne, testContextRefreshedEvent);
        assertNotified(testApplicationListenerTwo, testContextRefreshedEvent);
        assertUnnotified(testApplicationListenerThree);

        ContextClosedEvent testContextClosedEvent = new ContextClosedEvent(mockApplicationContext);

        initializer.onApplicationEvent(testContextClosedEvent);

        assertUnnotified(testApplicationListenerOne);
        assertUnnotified(testApplicationListenerTwo);
        assertUnnotified(testApplicationListenerThree);

        SpringContextBootstrappingInitializer.register(testApplicationListenerThree);

        assertUnnotified(testApplicationListenerOne);
        assertUnnotified(testApplicationListenerTwo);
        assertUnnotified(testApplicationListenerThree);
    } finally {
        SpringContextBootstrappingInitializer.unregister(testApplicationListenerOne);
        SpringContextBootstrappingInitializer.unregister(testApplicationListenerTwo);
        SpringContextBootstrappingInitializer.unregister(testApplicationListenerThree);
    }
}