0

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?

awesomemypro
  • 531
  • 1
  • 11
  • 32

1 Answers1

3

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.

alamar
  • 18,729
  • 4
  • 64
  • 97
  • Using different `igniteInstanceName` then both instances **don't run in the same cluster**. – lucasvc Jun 10 '20 at 14:41
  • 1
    You probably have some other problem in your configuration, since `igniteInstanceName` should not affect cluster join-ability. – alamar Jun 10 '20 at 15:58
  • Yes, you are right :) In fact the cluster was well formed, I did not realized correctly. Anyway seems a little bit weird to me that there is no `grid` or `cluster` name, and everybody which is detected in discovery, is part of the cluster... but this is other question. Thanks. – lucasvc Jun 11 '20 at 14:40
  • 1
    Cluster name is planned but not implemented yet. – alamar Jun 15 '20 at 07:33
  • All Ignite instances seem to share the same data though. Even if I set different instanceName and even different workDir. Is there a way to make the data completely independent between the instances? – Renato Oct 07 '22 at 09:13
  • Yes, you need to have different discovery configuration between nodes so that they form more than one cluster. – alamar Oct 07 '22 at 09:15