Search code examples
cachingignitedistributed-cachingdistributed-cache

How to run multiple Apache Ignite nodes on same JVM?


This is my Java implementation of Ignite as a caching layer.

public static void main(String[] args) throws IOException {

    Properties conf = getConfiguration();
    IgniteConfiguration cfg = new IgniteConfiguration();

    CacheConfiguration configuration = new CacheConfiguration();
    configuration.setName("ignt");

    DataSource dataSource = new DataSource();
    dataSource.setContactPoints(conf.getProperty("cass.contactPoints"));
    RoundRobinPolicy robinPolicy = new RoundRobinPolicy();
    dataSource.setLoadBalancingPolicy(robinPolicy);
    dataSource.setReadConsistency("ONE");
    dataSource.setWriteConsistency("ONE");
    dataSource.setProtocolVersion(4);
    dataSource.setPort(9042);
    configuration.setWriteThrough(true);
    configuration.setReadThrough(true);
    configuration.setWriteBehindEnabled(true);
    configuration.setWriteBehindFlushFrequency(30000);


    String persistenceSettingsXml = FileUtils.readFileToString(new File(conf.getProperty("ignite.persistenceSettings")), "utf-8");
    KeyValuePersistenceSettings persistenceSettings = new KeyValuePersistenceSettings(persistenceSettingsXml);
    System.out.println(persistenceSettings.getKeyspace());
    CassandraCacheStoreFactory cacheStoreFactory = new CassandraCacheStoreFactory();
    cacheStoreFactory.setDataSource(dataSource);
    cacheStoreFactory.setPersistenceSettings(persistenceSettings);
    configuration.setCacheStoreFactory(cacheStoreFactory);


    cfg.setCacheConfiguration(configuration);

    cfg.setGridName("g1");
    Ignite ignite=Ignition.getOrStart(cfg);

    System.out.println(cfg.getNodeId());
    cfg.setGridName("g2");
    Ignite igTwo = Ignition.getOrStart(cfg);

}

Is there a way to run multiple nodes (on localhost) from the same JVM program? If it is not possible to run multiple nodes from the same Java program, is there a way to run all the nodes from the command prompt separately and then connect to them from the Java application?


Solution

  • Yes, you can, and in process of running Java tests we run dozens of Ignite instances in the same VM. They are lightweight and they start up pretty fast.

    You should just make sure to set a different igniteInstanceName on IgniteConfiguration. Please also note that you can't reuse IgniteConfiguration when starting both instances. Create a factory method to build two IgniteConfiguration copies, one for every instance.