# Pub-Sub Example
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
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.
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
zookeeper Java api - curator 5.6.0 (757个子文件)
mvnw.cmd 8KB
DEPENDENCIES 29B
.gitignore 825B
gradle-enterprise-workspace-id 26B
gradle-enterprise-custom-user-data.groovy 2KB
maven-wrapper.jar 61KB
TestFramework.java 59KB
CreateBuilderImpl.java 52KB
TestLeaderLatch.java 49KB
TestPathChildrenCache.java 43KB
CuratorFrameworkImpl.java 40KB
TestPersistentEphemeralNode.java 32KB
TestFrameworkEdges.java 32KB
TreeCache.java 31KB
TestReconfiguration.java 31KB
TestLeaderSelector.java 31KB
PathChildrenCache.java 30KB
TestDistributedQueue.java 28KB
TestInterProcessSemaphore.java 28KB
TestFramework.java 27KB
TestWatchesBuilder.java 27KB
TestTreeCache.java 25KB
LeaderLatch.java 25KB
CuratorFrameworkFactory.java 24KB
DistributedQueue.java 23KB
TestCreate.java 21KB
TestSharedCount.java 21KB
LeaderSelector.java 19KB
PersistentNode.java 19KB
TestDelete.java 19KB
TestInterProcessMutexBase.java 18KB
ZKPaths.java 17KB
TestSetData.java 17KB
TestWatcherRemovalManager.java 17KB
ServiceDiscoveryImpl.java 17KB
TestServiceDiscovery.java 17KB
InterProcessSemaphoreV2.java 17KB
SetDataBuilderImpl.java 16KB
ModeledFrameworkImpl.java 16KB
TestSimpleDistributedQueue.java 15KB
TestMigrationManager.java 15KB
TestInterProcessReadWriteLock.java 14KB
TestModeledFramework.java 14KB
CuratorZookeeperClient.java 14KB
GzipCompressionProvider.java 13KB
AsyncWrappers.java 13KB
TestServiceCache.java 13KB
TestCuratorCacheConsistency.java 13KB
ConnectionStateManager.java 13KB
TestFrameworkBackground.java 13KB
ModeledFramework.java 13KB
DeleteBuilderImpl.java 13KB
TestTransactionsNew.java 12KB
TestFailedDeleteManager.java 12KB
TestCachedModeledFramework.java 12KB
InstanceSpec.java 12KB
GetDataBuilderImpl.java 12KB
RemoveWatchesBuilderImpl.java 11KB
TestDistributedAtomicLong.java 11KB
SharedValue.java 11KB
TestCircuitBreakingConnectionStateListener.java 11KB
TestInterProcessSemaphoreCluster.java 11KB
DistributedAtomicValue.java 11KB
CuratorFramework.java 11KB
NodeCache.java 11KB
LockInternals.java 11KB
TestSchema.java 11KB
TestFrameworkBackground.java 11KB
DistributedDoubleBarrier.java 11KB
TestingZooKeeperMain.java 11KB
GetConfigBuilderImpl.java 10KB
TestCuratorCache.java 10KB
Schema.java 10KB
DiscoveryExample.java 10KB
InterProcessSemaphore.java 10KB
TestCuratorCacheWrappers.java 10KB
UriSpec.java 10KB
TestDistributedPriorityQueue.java 10KB
ExistsBuilderImpl.java 10KB
PersistentTtlNode.java 10KB
ConnectionState.java 10KB
ReconfigBuilderImpl.java 10KB
TestBoundedDistributedQueue.java 10KB
ServiceInstance.java 10KB
TestQueueSharder.java 9KB
TestSessionFailRetryLoop.java 9KB
CuratorCacheImpl.java 9KB
GetChildrenBuilderImpl.java 9KB
ZPath.java 9KB
TestPersistentNode.java 9KB
QueueSharder.java 9KB
TestLeaderSelectorEdges.java 9KB
QueueBuilder.java 9KB
SubPubTest.java 9KB
TestNodeCache.java 9KB
CuratorMultiTransactionImpl.java 9KB
CachedModeledFrameworkImpl.java 9KB
TestNamespaceFacade.java 9KB
TestTransactionsOld.java 9KB
CircuitBreakingConnectionStateListener.java 9KB
共 757 条
- 1
- 2
- 3
- 4
- 5
- 6
- 8
资源评论
发家致富¥
- 粉丝: 229
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功