| 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 java.util.List; |
| 23 |
import java.util.Objects; |
| 24 |
import java.util.stream.Collectors; |
| 25 |
import org.apache.curator.framework.api.transaction.CuratorOp; |
| 26 |
import org.apache.curator.x.async.AsyncCuratorFramework; |
| 27 |
import org.apache.curator.x.async.modeled.ModeledFramework; |
| 28 |
import org.apache.curator.x.async.modeled.typed.TypedModeledFramework2; |
| 29 |
import org.slf4j.Logger; |
| 30 |
import org.slf4j.LoggerFactory; |
| 31 |
import pubsub.messages.LocationAvailable; |
| 32 |
import pubsub.messages.UserCreated; |
| 33 |
import pubsub.models.Group; |
| 34 |
import pubsub.models.Instance; |
| 35 |
import pubsub.models.Message; |
| 36 |
import pubsub.models.Priority; |
| 37 |
|
| 38 |
public class Publisher { |
| 39 |
private final Logger log = LoggerFactory.getLogger(getClass()); |
| 40 |
private final AsyncCuratorFramework client; |
| 41 |
|
| 42 |
public Publisher(AsyncCuratorFramework client) { |
| 43 |
this.client = Objects.requireNonNull(client, "client cannot be null"); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Publish the given instance using the Instance client template |
| 48 |
* |
| 49 |
* @param instance instance to publish |
| 50 |
*/ |
| 51 |
public void publishInstance(Instance instance) { |
| 52 |
ModeledFramework<Instance> resolvedClient = Clients.instanceClient.resolved(client, instance.getType()); |
| 53 |
resolvedClient.set(instance).exceptionally(e -> { |
| 54 |
log.error("Could not publish instance: " + instance, e); |
| 55 |
return null; |
| 56 |
}); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Publish the given instances using the Instance client template in a transaction |
| 61 |
* |
| 62 |
* @param instances instances to publish |
| 63 |
*/ |
| 64 |
public void publishInstances(List<Instance> instances) { |
| 65 |
List<CuratorOp> operations = instances.stream() |
| 66 |
.map(instance -> Clients.instanceClient |
| 67 |
.resolved(client, instance.getType()) |
| 68 |
.createOp(instance)) |
| 69 |
.collect(Collectors.toList()); |
| 70 |
client.transaction().forOperations(operations).exceptionally(e -> { |
| 71 |
log.error("Could not publish instances: " + instances, e); |
| 72 |
return null; |
| 73 |
}); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Publish the given LocationAvailable using the LocationAvailable client template |
| 78 |
* |
| 79 |
* @param group group |
| 80 |
* @param locationAvailable message to publish |
| 81 |
*/ |
| 82 |
public void publishLocationAvailable(Group group, LocationAvailable locationAvailable) { |
| 83 |
publishMessage(Clients.locationAvailableClient, group, locationAvailable); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Publish the given UserCreated using the UserCreated client template |
| 88 |
* |
| 89 |
* @param group group |
| 90 |
* @param userCreated message to publish |
| 91 |
*/ |
| 92 |
public void publishUserCreated(Group group, UserCreated userCreated) { |
| 93 |
publishMessage(Clients.userCreatedClient, group, userCreated); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Publish the given LocationAvailables using the LocationAvailable client template in a transaction |
| 98 |
* |
| 99 |
* @param group group |
| 100 |
* @param locationsAvailable messages to publish |
| 101 |
*/ |
| 102 |
public void publishLocationsAvailable(Group group, List<LocationAvailable> locationsAvailable) { |
| 103 |
publishMessages(Clients.locationAvailableClient, group, locationsAvailable); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Publish the given UserCreateds using the UserCreated client template in a transaction |
| 108 |
* |
| 109 |
* @param group group |
| 110 |
* @param usersCreated messages to publish |
| 111 |
*/ |
| 112 |
public void publishUsersCreated(Group group, List<UserCreated> usersCreated) { |
| 113 |
publishMessages(Clients.userCreatedClient, group, usersCreated); |
| 114 |
} |
| 115 |
|
| 116 |
private <T extends Message> void publishMessage( |
| 117 |
TypedModeledFramework2<T, Group, Priority> typedClient, Group group, T message) { |
| 118 |
ModeledFramework<T> resolvedClient = typedClient.resolved(client, group, message.getPriority()); |
| 119 |
resolvedClient.set(message).exceptionally(e -> { |
| 120 |
log.error("Could not publish message: " + message, e); |
| 121 |
return null; |
| 122 |
}); |
| 123 |
} |
| 124 |
|
| 125 |
private <T extends Message> void publishMessages( |
| 126 |
TypedModeledFramework2<T, Group, Priority> typedClient, Group group, List<T> messages) { |
| 127 |
List<CuratorOp> operations = messages.stream() |
| 128 |
.map(message -> typedClient |
| 129 |
.resolved(client, group, message.getPriority()) |
| 130 |
.createOp(message)) |
| 131 |
.collect(Collectors.toList()); |
| 132 |
client.transaction().forOperations(operations).exceptionally(e -> { |
| 133 |
log.error("Could not publish messages: " + messages, e); |
| 134 |
return null; |
| 135 |
}); |
| 136 |
} |
| 137 |
} |