public static void startEmbeddedCassandra ( File file , String tmpDir , long timeout ) throws IOException , ConfigurationException { if ( cassandraDaemon != null ) { /* nothing to do Cassandra is already started */ return ; } checkConfigNameForRestart ( file . getAbsolutePath ( ) ) ; log . debug ( "Starting cassandra..." ) ; log . debug ( "Initialization needed" ) ; System . setProperty ( "cassandra.config" , "file:" + file . getAbsolutePath ( ) ) ; System . setProperty ( "cassandra-foreground" , "true" ) ; System . setProperty ( "cassandra.native.epoll.enabled" , "false" ) ; // JNA doesnt cope with relocated netty System . setProperty ( "cassandra.unsafesystem" , "true" ) ; // disable fsync for a massive speedup on old platters // If there is no log4j config set already, set the default config if ( System . getProperty ( "log4j.configuration" ) == null ) { copy ( DEFAULT_LOG4J_CONFIG_FILE , tmpDir ) ; System . setProperty ( "log4j.configuration" , "file:" + tmpDir + DEFAULT_LOG4J_CONFIG_FILE ) ; } DatabaseDescriptor . daemonInitialization ( ) ; cleanupAndLeaveDirs ( ) ; final CountDownLatch startupLatch = new CountDownLatch ( 1 ) ; ExecutorService executor = Executors . newSingleThreadExecutor ( ) ; executor . execute ( ( ) -> { cassandraDaemon = new CassandraDaemon ( ) ; cassandraDaemon . activate ( ) ; startupLatch . countDown ( ) ; } ) ; try { if ( ! startupLatch . await ( timeout , MILLISECONDS ) ) { log . error ( "Cassandra daemon did not start after " + timeout + " ms. Consider increasing the timeout" ) ; throw new AssertionError ( "Cassandra daemon did not start within timeout" ) ; } Runtime . getRuntime ( ) . addShutdownHook ( new Thread ( ( ) -> { if ( session != null ) session . close ( ) ; if ( cluster != null ) cluster . close ( ) ; } ) ) ; } catch ( InterruptedException e ) { log . error ( "Interrupted waiting for Cassandra daemon to start:" , e ) ; throw new AssertionError ( e ) ; } finally { executor . shutdown ( ) ; } }
Set embedded cassandra up and spawn it in a new thread .