Switch branches/tags
next-release curator-2.0.0 curator-0.22-SNAPSHOT curator-0.21-SNAPSHOT curator-0.20-SNAPSHOT curator-0.19-SNAPSHOT curator-0.18-SNAPSHOT curator-0.17-SNAPSHOT curator-0.16-SNAPSHOT curator-0.15-SNAPSHOT curator-0.14-SNAPSHOT curator-0.13-SNAPSHOT curator-0.12-SNAPSHOT curator-0.11-SNAPSHOT curator-0.10-SNAPSHOT curator-0.9-SNAPSHOT curator-0.8-SNAPSHOT curator-0.7-SNAPSHOT curator-0.6-SNAPSHOT curator-0.5-SNAPSHOT curator-0.4-SNAPSHOT curator-0.3-SNAPSHOT curator-0.2-SNAPSHOT curator-0.1-SNAPSHOT apache-curator-4.0.1 apache-curator-4.0.0 apache-curator-3.3.0 apache-curator-3.2.1 apache-curator-3.2.0 apache-curator-3.2.0-SNAPSHOT apache-curator-3.1.0 apache-curator-3.0.0 apache-curator-3.0.0-SNAPSHOT apache-curator-2.12.0 apache-curator-2.11.1 apache-curator-2.11.0 apache-curator-2.11.0-SNAPSHOT apache-curator-2.10.0 apache-curator-2.9.1 apache-curator-2.9.0 apache-curator-2.9.0-rc1 apache-curator-2.8.0 apache-curator-2.7.0 apache-curator-2.6.0 apache-curator-2.5.0 apache-curator-2.4.2 apache-curator-2.4.1 apache-curator-2.3.1 apache-curator-2.3.0 apache-curator-2.2.0-incubating apache-curator-2.1.0-incubating apache-curator-2.0.1 apache-curator-2.0.1-incubating apache-curator-2.0.0-incubating TestLockCleanlinessWithFaults 2.4.0 1.1.17 1.1.16 1.1.15 1.1.14 1.1.13 1.1.12 1.1.11 1.1.10 1.1.9 1.1.3 1.1.2 1.1.1 1.1.0 1.0.17 1.0.16 1.0.16-2 1.0.15 1.0.15-2 1.0.14 1.0.13 1.0.11 1.0.10 1.0.3 1.0.3a 1.0.3-2 1.0.2 1.0.1 1.0.0 0.100.0 0.32 0.31 0.30 0.29 0.28 0.27 0.26 0.25 0.24 0.23 0.6.4 0.6.3 0.6.2 0.6.1 0.5.2
Nothing to show
Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
..
Failed to load latest commit information.
messages
models
Clients.java Squashed commit of the following: Jul 8, 2017
Publisher.java Squashed commit of the following: Jul 8, 2017
README.md Squashed commit of the following: Jul 8, 2017
SubPubTest.java Squashed commit of the following: Jul 8, 2017
Subscriber.java Squashed commit of the following: Jul 8, 2017

README.md

Pub-Sub Example

This example models a publish and subscribe system (note: it is not meant for production) using the strongly typed modeled APIs in Apache Curator.

Design Notes

In this example, there are three models that can be published: Instance, LocationAvailable and UserCreated. Instances have an InstanceType; LocationAvailable and UserCreated both have a Priority and are associated with a Group. (Note: these names/objects are meant for illustrative purposes only and are completely contrived)

Each model is stored at a unique path in ZooKeeper:

  • Instance: /root/pubsub/instances/TYPE/ID
  • LocationAvailable: /root/pubsub/messages/locations/GROUP/PRIORITY/ID
  • UserCreated: /root/pubsub/messages/users/GROUP/PRIORITY/ID

All models are stored using a TTL so that they automatically get deleted after 10 minutes.

Clients

This example uses the "typed" models (TypedModelSpec, etc.). The typed paths, models and clients are meant to be created early in your application and re-used as needed. Thus, you can model your ZooKeeper usage and the rest of your application can use them without worrying about correct paths, types, etc.

TypedModeledFramework is a template that produces a ModeledFramework by applying parameters to the TypedZPath in the contained TypedModelSpec. Curator provides variants that accept from 1 to 10 parameters (TypedModeledFramework, TypedModeledFramework2, TypedModeledFramework3, etc.).

In this example, the TypedModeledFrameworks are defined in Clients.java. E.g.

public static final TypedModeledFramework2<LocationAvailable, Group, Priority> locationAvailableClient = 
    TypedModeledFramework2.from(
        ModeledFramework.builder(),
        builder(LocationAvailable.class),
        "/root/pubsub/messages/locations/{group}/{priority}"
    );

Publisher

Publisher.java shows how to use the ModeledFramework to write models. There are methods to write single instances and to write lists of instances in a transaction. Each publish method resolves the appropriate typed client and then calls its set() method with the given model.

Subscriber

Subscriber.java uses CachedModeledFrameworks to listen for changes on the parent nodes for all of the models in this example. Each of the methods resolves the appropriate typed client and then starts the cache (via cached()).

SubPubTest

SubPubTest.java is a class that exercises this example.

  • start() uses Subscriber to start a CachedModeledFramework for each combination of the Instance + InstanceType, LocationAvailable + Group + Priority, and UserCreated + Group + Priority. It then adds a simple listener to each cache that merely prints the class name and path whenever an update occurs (see generalListener()).
  • start() also starts a scheduled task that runs every second. This task calls publishSomething()
  • publishSomething() randomly publishes either a single Instance, LocationAvailable, UserCreated or a list of those.

SubPubTest.java has a main() method. When you run you should see something similar to this:

Publishing 9 instances
Subscribed Instance @ /root/pubsub/instances/proxy/1
Subscribed Instance @ /root/pubsub/instances/web/2
Subscribed Instance @ /root/pubsub/instances/cache/4
Subscribed Instance @ /root/pubsub/instances/proxy/9
Subscribed Instance @ /root/pubsub/instances/database/3
Subscribed Instance @ /root/pubsub/instances/cache/5
Subscribed Instance @ /root/pubsub/instances/database/6
Subscribed Instance @ /root/pubsub/instances/cache/7
Subscribed Instance @ /root/pubsub/instances/cache/8
Publishing 1 userCreated
Subscribed UserCreated @ /root/pubsub/messages/users/main/high/10
Publishing 9 locationsAvailable
Subscribed LocationAvailable @ /root/pubsub/messages/locations/admin/low/11
Subscribed LocationAvailable @ /root/pubsub/messages/locations/admin/medium/12
Subscribed LocationAvailable @ /root/pubsub/messages/locations/admin/medium/13
Subscribed LocationAvailable @ /root/pubsub/messages/locations/admin/medium/14
Subscribed LocationAvailable @ /root/pubsub/messages/locations/admin/medium/16
Subscribed LocationAvailable @ /root/pubsub/messages/locations/admin/high/15
Subscribed LocationAvailable @ /root/pubsub/messages/locations/admin/medium/17
...

It runs for 1 minute and then exits.