public static void main()

in durableMessaging/src/main/java/org/apache/geode_examples/durableMessaging/Example.java [36:88]


  public static void main(String[] args) throws Exception {
    ClientCache clientCacheOne = createDurableClient();

    final String regionName = "example-region";

    // Create a local caching proxy region that matches the server region
    ClientRegionFactory<Integer, String> clientOneRegionFactory =
        clientCacheOne.createClientRegionFactory(ClientRegionShortcut.PROXY);
    Region<Integer, String> exampleClientRegionOne = clientOneRegionFactory.create(regionName);

    // Register interest to create the durable client message queue
    exampleClientRegionOne.registerInterestForAllKeys(InterestResultPolicy.DEFAULT, true);

    // Close the client cache with keepalive set to true so
    // the durable client messages are preserved
    // for the duration of the configured timeout. In practice,
    // it is more likely the client would disconnect
    // due to a temporary network issue, but for this example the cache is explicitly closed.
    clientCacheOne.close(true);

    // Create a second client to do puts with while the first client is disconnected
    ClientCache clientCacheTwo = new ClientCacheFactory().addPoolLocator("127.0.0.1", 10334)
        .set("log-level", "WARN").create();

    ClientRegionFactory<Integer, String> clientTwoRegionFactory =
        clientCacheTwo.createClientRegionFactory(ClientRegionShortcut.PROXY);
    Region<Integer, String> exampleClientRegionTwo = clientTwoRegionFactory.create(regionName);

    for (int i = 0; i < numEvents; ++i) {
      exampleClientRegionTwo.put(i, "testValue" + i);
    }

    // Close the second client and restart the durable client
    clientCacheTwo.close(false);

    clientCacheOne = createDurableClient();

    // Add an example cache listener so this client can react
    // when the server sends this client's events from the
    // durable message queue. This isn't required but helps
    // illustrate that the events are delivered successfully.
    clientOneRegionFactory = clientCacheOne.createClientRegionFactory(ClientRegionShortcut.PROXY);
    exampleClientRegionOne = clientOneRegionFactory
        .addCacheListener(new ExampleCacheListener<Integer, String>()).create(regionName);

    // Signal to the server that this client is ready to receive events.
    // Events in this client's durable message queue
    // will then be delivered and trigger our example cache listener.
    clientCacheOne.readyForEvents();

    // Use a count down latch to ensure that this client receives all queued events from the server
    waitForEventsLatch.await();
  }