{"_id":"568a404f050eb50d00c0799b","__v":21,"category":{"_id":"568a404e050eb50d00c07999","__v":7,"pages":["568a404f050eb50d00c0799b","5698f130cb127f0d003cc06a","5698f1483da4370d009d2079","569e6013d233620d00705550","569f3e578f6d4b0d00f13bd5","56a9d3d43b04f20d00eccaa5","56cc2b9e94c8f00b00b83d76"],"project":"568a404d050eb50d00c07995","version":"568a404e050eb50d00c07998","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2016-01-04T09:50:06.816Z","from_sync":false,"order":9999,"slug":"documentation","title":"Documentation"},"version":{"_id":"568a404e050eb50d00c07998","project":"568a404d050eb50d00c07995","__v":2,"createdAt":"2016-01-04T09:50:06.218Z","releaseDate":"2016-01-04T09:50:06.218Z","categories":["568a404e050eb50d00c07999","56cc2b81272aa4130002cce9"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"project":"568a404d050eb50d00c07995","user":"55e94db887e942230032e40d","updates":["56bd9173911a962100455a23"],"next":{"pages":[],"description":""},"createdAt":"2016-01-04T09:50:07.714Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"settings":"","results":{"codes":[]},"auth":"required","params":[],"url":""},"isReference":false,"order":0,"body":"In order to use BlazingCache in a simple application you have to build up a client and put data in it.\nIf you are using Maven add the following dependency:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<dependency>\\n   <groupId>org.blazingcache</groupId>\\n   <artifactId>blazingcache-jcache</artifactId>\\n   <version>VERSION</version>\\n</dependency>\",\n      \"language\": \"xml\"\n    }\n  ]\n}\n[/block]\nThen you have to get a Cache object:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"import java.util.Properties;\\nimport javax.cache.Cache;\\nimport javax.cache.CacheManager;\\nimport javax.cache.Caching;\\nimport javax.cache.configuration.MutableConfiguration;\\nimport javax.cache.spi.CachingProvider;\\n\\n public static void main(String ... args) {\\n        CachingProvider provider = Caching.getCachingProvider();\\n        Properties properties = new Properties();\\n        try (CacheManager cacheManager = provider.getCacheManager(provider.getDefaultURI(), provider.getDefaultClassLoader(), properties)) {\\n            MutableConfiguration<String, String> cacheConfiguration = new MutableConfiguration<>();\\n            Cache<String, String> cache = cacheManager.createCache(\\\"example\\\", cacheConfiguration);\\n            cache.put(\\\"foo\\\", \\\"bar\\\");\\n            String result = cache.get(\\\"foo\\\");\\n            System.out.println(\\\"result:\\\" + result);\\n        }\\n    }\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]\nThis example will start the full stack of BlazingCache on your JVM (a client and a server) and it will let you \"play\" with all the JSR107 functions.\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Distributed setup\"\n}\n[/block]\nA more interesting case is when you want to run a distributed setup: in this case you will need a standalone CacheServer running outside of you JVM.\nThe simplest way of achieving this i to download the BlazingCache package and start a server, this is  quite simple, just follow these steps:\n  1. Download the latest binary package\n  2. Ensure to have a JDK 8 installed on your machine and that JAVA_HOME is configured\n  3. Create a directory:\n           mkdir blazingcache\n  4. Unzip the package:\n           cd blazingcache\n           unzip blazingcache-*.zip\n  5. Start the server:\n           bin/service start\n\nThe server will listen on localhost:1025 in plain mode (not SSL)\n\nNow you need to change the example code in order to configure mode=static:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"import java.util.Properties;\\nimport javax.cache.Cache;\\nimport javax.cache.CacheManager;\\nimport javax.cache.Caching;\\nimport javax.cache.configuration.MutableConfiguration;\\nimport javax.cache.spi.CachingProvider;\\n\\n public static void main(String ... args) {\\n        CachingProvider provider = Caching.getCachingProvider();\\n        Properties properties = new Properties();\\n        properties.put(\\\"blazingcache.mode\\\",\\\"static\\\");\\n        properties.put(\\\"blazingcache.jmx\\\",\\\"false\\\"); //default value\\n        properties.put(\\\"blazingcache.server.host\\\",\\\"localhost\\\"); // default value\\n        properties.put(\\\"blazingcache.server.port\\\",\\\"1025\\\"); // default value\\n        properties.put(\\\"blazingcache.server.ssl\\\",\\\"false\\\"); // default value\\n        try (CacheManager cacheManager = provider.getCacheManager(provider.getDefaultURI(), provider.getDefaultClassLoader(), properties)) {\\n            MutableConfiguration<String, String> cacheConfiguration = new MutableConfiguration<>();\\n            Cache<String, String> cache = cacheManager.createCache(\\\"example\\\", cacheConfiguration);\\n            cache.put(\\\"foo\\\", \\\"bar\\\");\\n            String result = cache.get(\\\"foo\\\");\\n            System.out.println(\\\"result:\\\" + result);\\n        }\\n    }\\n\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]\nThis example will work as before but you will see in server.log the connection from your client.\n\nUsing a separate server will let you share your data between clients.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"High Availability\"\n}\n[/block]\nAnother step is to replicate the server in order to achieve high availability. This is very simple, but you will need an Apache Zookeeper server running (in a real environment you will probably run at least three zookeeper servers).\n\nIn order to boot more that one server unzip the package in two directories and perform these steps:\n  1. Change the \"mode\" parameter from \"singleserver\" to \"clustered\" in conf/server.properties\n  2. In one of the two servers (if running on the same machine) change the \"port\" from 1025 to another (higher) value\n  3. Start the Zookeeper server on one machine. You can use for **demo/testing** purpose the provided script, but remember to use an official Zookeeper distribution on production:\n          bin/service startzookeeper\n\nBy default the provided Zookeeper script will run ZK in standalone mode on localhost:1281\n\nYour client will now look like this:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"import java.util.Properties;\\nimport javax.cache.Cache;\\nimport javax.cache.CacheManager;\\nimport javax.cache.Caching;\\nimport javax.cache.configuration.MutableConfiguration;\\nimport javax.cache.spi.CachingProvider;\\n\\n public static void main(String ... args) {\\n        CachingProvider provider = Caching.getCachingProvider();\\n        Properties properties = new Properties();\\n        properties.put(\\\"blazingcache.mode\\\",\\\"clustered\\\");        \\n        properties.put(\\\"blazingcache.zookeeper.connectstring\\\",\\\"localhost:1281\\\");\\n        properties.put(\\\"blazingcache.zookeeper.sessiontimeout\\\",\\\"40000\\\");        \\n        properties.put(\\\"blazingcache.zookeeper.path\\\",\\\"/blazingcache\\\");           \\n        try (CacheManager cacheManager = provider.getCacheManager(provider.getDefaultURI(), provider.getDefaultClassLoader(), properties)) {\\n            MutableConfiguration<String, String> cacheConfiguration = new MutableConfiguration<>();\\n            Cache<String, String> cache = cacheManager.createCache(\\\"example\\\", cacheConfiguration);\\n            cache.put(\\\"foo\\\", \\\"bar\\\");\\n            String result = cache.get(\\\"foo\\\");\\n            System.out.println(\\\"result:\\\" + result);\\n        }\\n    }\\n\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]\nYour client will now use Zookeeper for server discovery, then it will connect to the LEADER server.\nIf you shutdown the LEADER the client will reconnect automatically to the other server, which in turn will became the new LEADER.\n\nSince version 1.12.0 you can start a CacheServer embedded into every client but just using blazingcache.mode=\"server\".\nThis way you will be able to run any number of client without the need of running a CacheServer.\nThis setup is very useful for little clusters (2-3 machine at max).\nBeware that in this mode every client will run a copy of the server and in turn will act as the cache coordinator.\nIn this modo every client must be able to connect to other peers and every client will open a TCP listening endpoint.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"import java.util.Properties;\\nimport javax.cache.Cache;\\nimport javax.cache.CacheManager;\\nimport javax.cache.Caching;\\nimport javax.cache.configuration.MutableConfiguration;\\nimport javax.cache.spi.CachingProvider;\\n\\n public static void main(String ... args) {\\n        CachingProvider provider = Caching.getCachingProvider();\\n        Properties properties = new Properties();\\n        properties.put(\\\"blazingcache.mode\\\",\\\"server\\\");        \\n        properties.put(\\\"blazingcache.zookeeper.connectstring\\\",\\\"localhost:1281\\\");\\n        properties.put(\\\"blazingcache.zookeeper.sessiontimeout\\\",\\\"40000\\\");        \\n        properties.put(\\\"blazingcache.zookeeper.path\\\",\\\"/blazingcache\\\"); \\n        properties.put(\\\"blazingcache.zookeeper.writeacls\\\",\\\"false\\\"); \\n        properties.put(\\\"blazingcache.zookeeper.host\\\",\\\"my-public-hostname\\\"); \\n        properties.put(\\\"blazingcache.zookeeper.port\\\",\\\"7000\\\"); \\n        properties.put(\\\"blazingcache.zookeeper.ssl\\\",\\\"false\\\"); \\n    \\n        try (CacheManager cacheManager = provider.getCacheManager(provider.getDefaultURI(), provider.getDefaultClassLoader(), properties)) {\\n            MutableConfiguration<String, String> cacheConfiguration = new MutableConfiguration<>();\\n            Cache<String, String> cache = cacheManager.createCache(\\\"example\\\", cacheConfiguration);\\n            cache.put(\\\"foo\\\", \\\"bar\\\");\\n            String result = cache.get(\\\"foo\\\");\\n            System.out.println(\\\"result:\\\" + result);\\n        }\\n    }\\n\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]","excerpt":"This page will help you get started with BlazingCache.","slug":"getting-started","type":"basic","title":"Getting Started"}

Getting Started

This page will help you get started with BlazingCache.

In order to use BlazingCache in a simple application you have to build up a client and put data in it. If you are using Maven add the following dependency: [block:code] { "codes": [ { "code": "<dependency>\n <groupId>org.blazingcache</groupId>\n <artifactId>blazingcache-jcache</artifactId>\n <version>VERSION</version>\n</dependency>", "language": "xml" } ] } [/block] Then you have to get a Cache object: [block:code] { "codes": [ { "code": "import java.util.Properties;\nimport javax.cache.Cache;\nimport javax.cache.CacheManager;\nimport javax.cache.Caching;\nimport javax.cache.configuration.MutableConfiguration;\nimport javax.cache.spi.CachingProvider;\n\n public static void main(String ... args) {\n CachingProvider provider = Caching.getCachingProvider();\n Properties properties = new Properties();\n try (CacheManager cacheManager = provider.getCacheManager(provider.getDefaultURI(), provider.getDefaultClassLoader(), properties)) {\n MutableConfiguration<String, String> cacheConfiguration = new MutableConfiguration<>();\n Cache<String, String> cache = cacheManager.createCache(\"example\", cacheConfiguration);\n cache.put(\"foo\", \"bar\");\n String result = cache.get(\"foo\");\n System.out.println(\"result:\" + result);\n }\n }", "language": "java" } ] } [/block] This example will start the full stack of BlazingCache on your JVM (a client and a server) and it will let you "play" with all the JSR107 functions. [block:api-header] { "type": "basic", "title": "Distributed setup" } [/block] A more interesting case is when you want to run a distributed setup: in this case you will need a standalone CacheServer running outside of you JVM. The simplest way of achieving this i to download the BlazingCache package and start a server, this is quite simple, just follow these steps: 1. Download the latest binary package 2. Ensure to have a JDK 8 installed on your machine and that JAVA_HOME is configured 3. Create a directory: mkdir blazingcache 4. Unzip the package: cd blazingcache unzip blazingcache-*.zip 5. Start the server: bin/service start The server will listen on localhost:1025 in plain mode (not SSL) Now you need to change the example code in order to configure mode=static: [block:code] { "codes": [ { "code": "import java.util.Properties;\nimport javax.cache.Cache;\nimport javax.cache.CacheManager;\nimport javax.cache.Caching;\nimport javax.cache.configuration.MutableConfiguration;\nimport javax.cache.spi.CachingProvider;\n\n public static void main(String ... args) {\n CachingProvider provider = Caching.getCachingProvider();\n Properties properties = new Properties();\n properties.put(\"blazingcache.mode\",\"static\");\n properties.put(\"blazingcache.jmx\",\"false\"); //default value\n properties.put(\"blazingcache.server.host\",\"localhost\"); // default value\n properties.put(\"blazingcache.server.port\",\"1025\"); // default value\n properties.put(\"blazingcache.server.ssl\",\"false\"); // default value\n try (CacheManager cacheManager = provider.getCacheManager(provider.getDefaultURI(), provider.getDefaultClassLoader(), properties)) {\n MutableConfiguration<String, String> cacheConfiguration = new MutableConfiguration<>();\n Cache<String, String> cache = cacheManager.createCache(\"example\", cacheConfiguration);\n cache.put(\"foo\", \"bar\");\n String result = cache.get(\"foo\");\n System.out.println(\"result:\" + result);\n }\n }\n", "language": "java" } ] } [/block] This example will work as before but you will see in server.log the connection from your client. Using a separate server will let you share your data between clients. [block:api-header] { "type": "basic", "title": "High Availability" } [/block] Another step is to replicate the server in order to achieve high availability. This is very simple, but you will need an Apache Zookeeper server running (in a real environment you will probably run at least three zookeeper servers). In order to boot more that one server unzip the package in two directories and perform these steps: 1. Change the "mode" parameter from "singleserver" to "clustered" in conf/server.properties 2. In one of the two servers (if running on the same machine) change the "port" from 1025 to another (higher) value 3. Start the Zookeeper server on one machine. You can use for **demo/testing** purpose the provided script, but remember to use an official Zookeeper distribution on production: bin/service startzookeeper By default the provided Zookeeper script will run ZK in standalone mode on localhost:1281 Your client will now look like this: [block:code] { "codes": [ { "code": "import java.util.Properties;\nimport javax.cache.Cache;\nimport javax.cache.CacheManager;\nimport javax.cache.Caching;\nimport javax.cache.configuration.MutableConfiguration;\nimport javax.cache.spi.CachingProvider;\n\n public static void main(String ... args) {\n CachingProvider provider = Caching.getCachingProvider();\n Properties properties = new Properties();\n properties.put(\"blazingcache.mode\",\"clustered\"); \n properties.put(\"blazingcache.zookeeper.connectstring\",\"localhost:1281\");\n properties.put(\"blazingcache.zookeeper.sessiontimeout\",\"40000\"); \n properties.put(\"blazingcache.zookeeper.path\",\"/blazingcache\"); \n try (CacheManager cacheManager = provider.getCacheManager(provider.getDefaultURI(), provider.getDefaultClassLoader(), properties)) {\n MutableConfiguration<String, String> cacheConfiguration = new MutableConfiguration<>();\n Cache<String, String> cache = cacheManager.createCache(\"example\", cacheConfiguration);\n cache.put(\"foo\", \"bar\");\n String result = cache.get(\"foo\");\n System.out.println(\"result:\" + result);\n }\n }\n", "language": "java" } ] } [/block] Your client will now use Zookeeper for server discovery, then it will connect to the LEADER server. If you shutdown the LEADER the client will reconnect automatically to the other server, which in turn will became the new LEADER. Since version 1.12.0 you can start a CacheServer embedded into every client but just using blazingcache.mode="server". This way you will be able to run any number of client without the need of running a CacheServer. This setup is very useful for little clusters (2-3 machine at max). Beware that in this mode every client will run a copy of the server and in turn will act as the cache coordinator. In this modo every client must be able to connect to other peers and every client will open a TCP listening endpoint. [block:code] { "codes": [ { "code": "import java.util.Properties;\nimport javax.cache.Cache;\nimport javax.cache.CacheManager;\nimport javax.cache.Caching;\nimport javax.cache.configuration.MutableConfiguration;\nimport javax.cache.spi.CachingProvider;\n\n public static void main(String ... args) {\n CachingProvider provider = Caching.getCachingProvider();\n Properties properties = new Properties();\n properties.put(\"blazingcache.mode\",\"server\"); \n properties.put(\"blazingcache.zookeeper.connectstring\",\"localhost:1281\");\n properties.put(\"blazingcache.zookeeper.sessiontimeout\",\"40000\"); \n properties.put(\"blazingcache.zookeeper.path\",\"/blazingcache\"); \n properties.put(\"blazingcache.zookeeper.writeacls\",\"false\"); \n properties.put(\"blazingcache.zookeeper.host\",\"my-public-hostname\"); \n properties.put(\"blazingcache.zookeeper.port\",\"7000\"); \n properties.put(\"blazingcache.zookeeper.ssl\",\"false\"); \n \n try (CacheManager cacheManager = provider.getCacheManager(provider.getDefaultURI(), provider.getDefaultClassLoader(), properties)) {\n MutableConfiguration<String, String> cacheConfiguration = new MutableConfiguration<>();\n Cache<String, String> cache = cacheManager.createCache(\"example\", cacheConfiguration);\n cache.put(\"foo\", \"bar\");\n String result = cache.get(\"foo\");\n System.out.println(\"result:\" + result);\n }\n }\n", "language": "java" } ] } [/block]