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

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

Introduction

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

Prototype

@Override
public void registerShutdownHook() 

Source Link

Document

Register a shutdown hook with the JVM runtime, closing this context on JVM shutdown unless it has already been closed at that time.

Usage

From source file:com.nokia.dempsy.router.TestRouterInstantiation.java

@Test
public void testSpringConfig() throws Throwable {
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("RouterConfigTest.xml");
    ctx.registerShutdownHook();
}

From source file:com.github.khannedy.simplepos.manager.SpringManager.java

private SpringManager() {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
    context.registerShutdownHook();

    this.applicationContext = context;
}

From source file:com.nokia.dempsy.mpcluster.TestAllMpClusterImpls.java

private <T, N> void runAllCombinations(Checker<T, N> checker) throws Throwable {
    for (String clusterManager : clusterManagers) {
        ClassPathXmlApplicationContext actx = new ClassPathXmlApplicationContext(clusterManager);
        actx.registerShutdownHook();

        @SuppressWarnings("unchecked")
        MpClusterSessionFactory<T, N> factory = (MpClusterSessionFactory<T, N>) actx
                .getBean("clusterSessionFactory");

        if (checker != null)
            checker.check("pass for:" + clusterManager, factory);

        actx.stop();/*ww  w  . j a  va2s.co m*/
        actx.destroy();
    }
}

From source file:com.nokia.dempsy.TestDempsy.java

public void runAllCombinations(String applicationContext, Checker checker) throws Throwable {
    int runCount = 0;
    for (String clusterManager : clusterManagers) {
        for (String[] alternatingTransports : transports) {
            // select one of the alternatingTransports
            String transport = alternatingTransports[runCount % alternatingTransports.length];

            // alternate the dempsy configs
            String dempsyConfig = dempsyConfigs[runCount % dempsyConfigs.length];

            if (!badCombos.contains(new ClusterId(clusterManager, transport))) {
                try {
                    logger.debug("*****************************************************************");
                    logger.debug(" test: " + (checker == null ? "none" : checker) + " using " + dempsyConfig
                            + "," + clusterManager + "," + transport);
                    logger.debug("*****************************************************************");

                    DefaultRoutingStrategy.resetOutboundsChecking();

                    String[] ctx = new String[4];
                    ctx[0] = dempsyConfig;
                    ctx[1] = clusterManager;
                    ctx[2] = transport;/*from www . java2  s. c  o  m*/
                    ctx[3] = "testDempsy/" + applicationContext;

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

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

                    assertTrue(TestUtils.waitForClustersToBeInitialized(baseTimeoutMillis, 20, dempsy));

                    WaitForShutdown waitingForShutdown = new WaitForShutdown(dempsy);
                    Thread waitingForShutdownThread = new Thread(waitingForShutdown, "Waiting For Shutdown");
                    waitingForShutdownThread.start();
                    Thread.yield();

                    logger.debug("Running test ...");
                    if (checker != null)
                        checker.check(actx);
                    logger.debug("Done with test, stopping the application context ...");

                    actx.stop();
                    actx.destroy();

                    assertTrue(waitingForShutdown.waitForShutdownDoneLatch.await(baseTimeoutMillis,
                            TimeUnit.MILLISECONDS));
                    assertTrue(waitingForShutdown.shutdown);

                    logger.debug("Finished this pass.");
                } catch (AssertionError re) {
                    logger.error("***************** FAILED ON: " + clusterManager + ", " + transport);
                    throw re;
                }

                runCount++;
            }
        }
    }
}

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

@Test
public void testStartStop() throws Throwable {
    ClassPathXmlApplicationContext actx = null;
    Dempsy dempsy = null;/*  ww w .jav  a 2 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:com.nokia.dempsy.mpcluster.zookeeper.TestFullApp.java

@Test
public void testStartForceMpDisconnectStop() throws Throwable {
    ClassPathXmlApplicationContext actx = null;
    Dempsy dempsy = null;//from   w w  w. j  ava  2 s. c  om

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

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

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

        // Override the cluster session factory to keep track of the sessions asked for.
        // This is so that I can grab the ZookeeperSession that's being instantiated by
        // the MyMp cluster.
        zookeeperCluster = null;
        dempsy.setClusterSessionFactory(new ZookeeperSessionFactory<ClusterInformation, SlotInformation>(
                System.getProperty("zk_connect"), 5000) {
            int sessionCount = 0;

            @Override
            public synchronized MpClusterSession<ClusterInformation, SlotInformation> createSession()
                    throws MpClusterException {
                sessionCount++;
                MpClusterSession<ClusterInformation, SlotInformation> ret = super.createSession();

                if (sessionCount == 2)
                    zookeeperCluster = (ZookeeperSession<ClusterInformation, SlotInformation>) ret;
                return ret;
            }
        });

        dempsy.start();

        Dempsy.Application.Cluster cluster = dempsy.getCluster(
                new ClusterId(FullApplication.class.getSimpleName(), MyAdaptor.class.getSimpleName()));
        Dempsy.Application.Cluster.Node node = cluster.getNodes().get(0);
        final StatsCollector collector = node.getStatsCollector();

        // this checks that the throughput works.
        assertTrue(poll(baseTimeoutMillis * 5, app, new Condition<Object>() {
            @Override
            public boolean conditionMet(Object o) {
                return app.finalMessageCount.get() > 10;
            }
        }));

        assertNotNull(zookeeperCluster);

        assertEquals(0, collector.getDiscardedMessageCount());
        assertEquals(0, collector.getMessageFailedCount());

        // ok ... so now we have stuff going all the way through. let's kick
        // the middle Mp's zookeeper cluster and see what happens.
        ZooKeeper origZk = zookeeperCluster.zkref.get();
        long sessionid = origZk.getSessionId();
        ZooKeeper killer = new ZooKeeper(System.getProperty("zk_connect"), 5000, new Watcher() {
            @Override
            public void process(WatchedEvent arg0) {
            }
        }, sessionid, null);
        killer.close(); // tricks the server into expiring the other session

        //         // we should be getting failures now ... 
        //         // but it's possible that it can reconnect prior to actually seeing an error so if this 
        //         //   fails frequently we need to remove this test.
        //         assertTrue(poll(baseTimeoutMillis, app, new Condition()
        //         {
        //            @Override
        //            public boolean conditionMet(Object o)
        //            {
        //               return collector.getMessageFailedCount() > 1;
        //            }
        //         }));

        //... and then recover.

        // get the MyMp prototype
        cluster = dempsy
                .getCluster(new ClusterId(FullApplication.class.getSimpleName(), MyMp.class.getSimpleName()));
        node = cluster.getNodes().get(0);
        final MyMp prototype = (MyMp) node.getMpContainer().getPrototype();

        // so let's see where we are
        final long interimMessageCount = prototype.myMpReceived.get();

        // and now we should eventually get more as the session recovers.
        assertTrue(poll(baseTimeoutMillis * 5, app, new Condition<Object>() {
            @Override
            public boolean conditionMet(Object o) {
                return prototype.myMpReceived.get() > interimMessageCount + 100;
            }
        }));
    } finally {
        if (dempsy != null)
            dempsy.stop();

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

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

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

@Test
public void testStartForceMpDisconnectWithStandby() throws Throwable {
    ClassPathXmlApplicationContext actx = null;
    Dempsy dempsy = null;/*  ww w .j  av a  2s .c  o  m*/

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

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

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

        // Override the cluster session factory to keep track of the sessions asked for.
        // This is so that I can grab the ZookeeperSession that's being instantiated by
        // the MyMp cluster.
        zookeeperCluster = null;
        dempsy.setClusterSessionFactory(new ZookeeperSessionFactory<ClusterInformation, SlotInformation>(
                System.getProperty("zk_connect"), 5000) {
            int sessionCount = 0;

            @Override
            public synchronized MpClusterSession<ClusterInformation, SlotInformation> createSession()
                    throws MpClusterException {
                sessionCount++;
                MpClusterSession<ClusterInformation, SlotInformation> ret = super.createSession();

                if (sessionCount == 2)
                    zookeeperCluster = (ZookeeperSession<ClusterInformation, SlotInformation>) ret;
                return ret;
            }
        });

        dempsy.start();

        Dempsy.Application.Cluster cluster = dempsy.getCluster(
                new ClusterId(FullApplication.class.getSimpleName(), MyAdaptor.class.getSimpleName()));
        Dempsy.Application.Cluster.Node node = cluster.getNodes().get(0);
        final StatsCollector collector = node.getStatsCollector();

        // we are going to create another node of the MyMp via a test hack
        cluster = dempsy
                .getCluster(new ClusterId(FullApplication.class.getSimpleName(), MyMp.class.getSimpleName()));
        Dempsy.Application.Cluster.Node mpnode = cluster.getNodes().get(0);
        cluster.instantiateAndStartAnotherNodeForTesting(); // the code for start instantiates a new node

        // this checks that the throughput works.
        assertTrue(poll(baseTimeoutMillis * 5, app, new Condition<Object>() {
            @Override
            public boolean conditionMet(Object o) {
                return app.finalMessageCount.get() > 10;
            }
        }));

        assertNotNull(zookeeperCluster);

        assertEquals(0, collector.getDiscardedMessageCount());
        assertEquals(0, collector.getMessageFailedCount());

        // ok ... so now we have stuff going all the way through. let's kick
        // the middle Mp's zookeeper cluster and see what happens.
        ZooKeeper origZk = zookeeperCluster.zkref.get();
        origZk.close(); // this should kill it.

        // but just to be sure actually stop the node.
        mpnode.stop();

        //         // we should be getting failures now ... 
        //         // but it's possible that it can reconnect prior to actually seeing an error so if this 
        //         //   fails frequently we need to remove this test.
        //         assertTrue(poll(baseTimeoutMillis, app, new Condition()
        //         {
        //            @Override
        //            public boolean conditionMet(Object o)
        //            {
        //               return collector.getMessageFailedCount() > 1;
        //            }
        //         }));

        //... and then recover.

        // get the MyMp prototype
        cluster = dempsy
                .getCluster(new ClusterId(FullApplication.class.getSimpleName(), MyMp.class.getSimpleName()));
        node = cluster.getNodes().get(1); // notice, we're getting the SECOND node.
        final MyMp prototype = (MyMp) node.getMpContainer().getPrototype();

        // so let's see where we are
        final long interimMessageCount = prototype.myMpReceived.get();

        // and now we should eventually get more as the session recovers.
        assertTrue(poll(baseTimeoutMillis * 5, app, new Condition<Object>() {
            @Override
            public boolean conditionMet(Object o) {
                return prototype.myMpReceived.get() > interimMessageCount + 100;
            }
        }));

    } finally {
        if (dempsy != null)
            dempsy.stop();

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

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

}

From source file:com.nokia.dempsy.TestDempsy.java

@Test
public void testIndividualClusterStart() throws Throwable {
    ClassPathXmlApplicationContext actx = new ClassPathXmlApplicationContext(
            "testDempsy/Dempsy-IndividualClusterStart.xml", "testDempsy/Transport-PassthroughActx.xml",
            "testDempsy/ClusterManager-LocalVmActx.xml", "testDempsy/SimpleMultistageApplicationActx.xml");
    actx.registerShutdownHook();

    Dempsy dempsy = (Dempsy) actx.getBean("dempsy");
    assertNotNull(dempsy);//from  ww  w  .java  2s  .co m

    Dempsy.Application.Cluster cluster = dempsy.getCluster(new ClusterId("test-app", "test-cluster0"));
    assertNull(cluster);

    cluster = dempsy.getCluster(new ClusterId("test-app", "test-cluster1"));
    assertNull(cluster);

    cluster = dempsy.getCluster(new ClusterId("test-app", "test-cluster2"));
    assertNotNull(cluster);
    assertEquals(1, cluster.getNodes().size());

    cluster = dempsy.getCluster(new ClusterId("test-app", "test-cluster3"));
    assertNull(cluster);

    cluster = dempsy.getCluster(new ClusterId("test-app", "test-cluster4"));
    assertNull(cluster);

    actx.stop();
    actx.destroy();
}

From source file:com.nokia.dempsy.TestDempsy.java

@Test
public void testAdaptorThrowsRuntimeOnSetDispatcher() throws Throwable {
    TestAdaptor.throwExceptionOnSetDispatcher = true;
    ClassPathXmlApplicationContext actx = null;
    boolean gotException = false;

    try {// www.  j  a  v  a  2 s.  c  om
        actx = new ClassPathXmlApplicationContext("testDempsy/Dempsy.xml",
                "testDempsy/Transport-PassthroughActx.xml", "testDempsy/ClusterManager-LocalVmActx.xml",
                "testDempsy/SimpleMultistageApplicationActx.xml");
        actx.registerShutdownHook();
    } catch (Throwable th) {
        assertEquals("Forced RuntimeException", th.getCause().getLocalizedMessage());
        gotException = true;
    } finally {
        TestAdaptor.throwExceptionOnSetDispatcher = false;
        if (actx != null) {
            actx.stop();
            actx.destroy();
        }

    }

    assertTrue(gotException);
}