| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
* or more contributor license agreements. See the NOTICE file |
| 4 |
* distributed with this work for additional information |
| 5 |
* regarding copyright ownership. The ASF licenses this file |
| 6 |
* to you under the Apache License, Version 2.0 (the |
| 7 |
* "License"); you may not use this file except in compliance |
| 8 |
* with the License. You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, |
| 13 |
* software distributed under the License is distributed on an |
| 14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
* KIND, either express or implied. See the License for the |
| 16 |
* specific language governing permissions and limitations |
| 17 |
* under the License. |
| 18 |
*/ |
| 19 |
|
| 20 |
package pubsub; |
| 21 |
|
| 22 |
import org.apache.curator.x.async.AsyncCuratorFramework; |
| 23 |
import org.apache.curator.x.async.modeled.cached.CachedModeledFramework; |
| 24 |
import org.apache.curator.x.async.modeled.typed.TypedModeledFramework2; |
| 25 |
import pubsub.messages.LocationAvailable; |
| 26 |
import pubsub.messages.UserCreated; |
| 27 |
import pubsub.models.Group; |
| 28 |
import pubsub.models.Instance; |
| 29 |
import pubsub.models.InstanceType; |
| 30 |
import pubsub.models.Message; |
| 31 |
import pubsub.models.Priority; |
| 32 |
|
| 33 |
public class Subscriber { |
| 34 |
private final AsyncCuratorFramework client; |
| 35 |
|
| 36 |
public Subscriber(AsyncCuratorFramework client) { |
| 37 |
this.client = client; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Start a subscriber (a CachedModeledFramework instance) using the LocationAvailable client template |
| 42 |
* |
| 43 |
* @param group group to listen for |
| 44 |
* @param priority priority to listen for |
| 45 |
* @return CachedModeledFramework instance (already started) |
| 46 |
*/ |
| 47 |
public CachedModeledFramework<LocationAvailable> startLocationAvailableSubscriber(Group group, Priority priority) { |
| 48 |
return startSubscriber(Clients.locationAvailableClient, group, priority); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Start a subscriber (a CachedModeledFramework instance) using the UserCreated client template |
| 53 |
* |
| 54 |
* @param group group to listen for |
| 55 |
* @param priority priority to listen for |
| 56 |
* @return CachedModeledFramework instance (already started) |
| 57 |
*/ |
| 58 |
public CachedModeledFramework<UserCreated> startUserCreatedSubscriber(Group group, Priority priority) { |
| 59 |
return startSubscriber(Clients.userCreatedClient, group, priority); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Start a subscriber (a CachedModeledFramework instance) using the Instance client template |
| 64 |
* |
| 65 |
* @param instanceType type to listen for |
| 66 |
* @return CachedModeledFramework instance (already started) |
| 67 |
*/ |
| 68 |
public CachedModeledFramework<Instance> startInstanceSubscriber(InstanceType instanceType) { |
| 69 |
CachedModeledFramework<Instance> resolved = |
| 70 |
Clients.instanceClient.resolved(client, instanceType).cached(); |
| 71 |
resolved.start(); |
| 72 |
return resolved; |
| 73 |
} |
| 74 |
|
| 75 |
private <T extends Message> CachedModeledFramework<T> startSubscriber( |
| 76 |
TypedModeledFramework2<T, Group, Priority> typedClient, Group group, Priority priority) { |
| 77 |
CachedModeledFramework<T> resolved = |
| 78 |
typedClient.resolved(client, group, priority).cached(); |
| 79 |
resolved.start(); |
| 80 |
return resolved; |
| 81 |
} |
| 82 |
} |