public static void main(String[] args) throws Exception { Properties props = new Properties(); props.setProperty("name", "CqServer"); props.setProperty("log-level", "warning"); System.out.println("\nConnecting to the distributed system and creating the cache."); DistributedSystem ds = DistributedSystem.connect(props); Cache cache = CacheFactory.create(ds); // Create region. AttributesFactory factory = new AttributesFactory(); factory.setDataPolicy(DataPolicy.REPLICATE); factory.setScope(Scope.DISTRIBUTED_ACK); Region testRegion = cache.createRegion("test-cq", factory.create()); System.out.println("Test region, " + testRegion.getFullPath() + ", created in cache."); // Start Cache Server. CacheServer server = cache.addCacheServer(); server.setPort(40404); server.setNotifyBySubscription(true); server.start(); System.out.println("Waiting for signal"); // wait for signal BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); bufferedReader.readLine(); System.out.println("Received signal"); testRegion.put("one", 1); testRegion.put("two", 2); testRegion.put("three", 3); System.out.println("Waiting for shutdown"); bufferedReader.readLine(); }
/** Creates and starts the server instance */ private int createServer() { CacheServer server = null; try { Properties p = new Properties(); // make it a loner p.put("mcast-port", "0"); p.put("locators", ""); this.system = DistributedSystem.connect(p); this.cache = CacheFactory.create(system); server = this.cache.addCacheServer(); int port = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET); server.setMaximumTimeBetweenPings(TIME_BETWEEN_PINGS); server.setMaxThreads(getMaxThreads()); server.setPort(port); server.start(); } catch (Exception e) { e.printStackTrace(); fail("Failed to create server"); } return server.getPort(); }