|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package com.azure.messaging.servicebus; |
| 5 | + |
| 6 | +import com.azure.core.annotation.ServiceClient; |
| 7 | +import com.azure.core.util.logging.ClientLogger; |
| 8 | +import com.azure.messaging.servicebus.administration.ServiceBusAdministrationAsyncClient; |
| 9 | +import com.azure.messaging.servicebus.administration.models.CorrelationRuleFilter; |
| 10 | +import com.azure.messaging.servicebus.administration.models.CreateRuleOptions; |
| 11 | +import com.azure.messaging.servicebus.administration.models.RuleFilter; |
| 12 | +import com.azure.messaging.servicebus.administration.models.RuleProperties; |
| 13 | +import com.azure.messaging.servicebus.administration.models.SqlRuleAction; |
| 14 | +import com.azure.messaging.servicebus.administration.models.SqlRuleFilter; |
| 15 | +import com.azure.messaging.servicebus.implementation.MessagingEntityType; |
| 16 | +import com.azure.messaging.servicebus.implementation.ServiceBusConnectionProcessor; |
| 17 | +import com.azure.messaging.servicebus.implementation.ServiceBusManagementNode; |
| 18 | +import reactor.core.publisher.Flux; |
| 19 | +import reactor.core.publisher.Mono; |
| 20 | + |
| 21 | +import static com.azure.core.util.FluxUtil.fluxError; |
| 22 | +import static com.azure.core.util.FluxUtil.monoError; |
| 23 | +import static com.azure.messaging.servicebus.implementation.Messages.INVALID_OPERATION_DISPOSED_RULE_MANAGER; |
| 24 | + |
| 25 | +import java.util.Objects; |
| 26 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 27 | + |
| 28 | +/** |
| 29 | + * An <b>asynchronous</b> rule manager responsible for managing rules for a specific topic subscription. The rule manager |
| 30 | + * requires only Listen claims, whereas the {@link ServiceBusAdministrationAsyncClient} requires Manage claims. |
| 31 | + * |
| 32 | + * <p><strong>Create an instance of rule manager</strong></p> |
| 33 | + * <!-- src_embed com.azure.messaging.servicebus.servicebusrulemanagerasyncclient.instantiation --> |
| 34 | + * <pre> |
| 35 | + * // The required parameters is connectionString, a way to authenticate with Service Bus using credentials. |
| 36 | + * // The connectionString/queueName must be set by the application. The 'connectionString' format is shown below. |
| 37 | + * // "Endpoint={fully-qualified-namespace};SharedAccessKeyName={policy-name};SharedAccessKey={key}" |
| 38 | + * |
| 39 | + * ServiceBusRuleManagerAsyncClient ruleManager = new ServiceBusClientBuilder() |
| 40 | + * .connectionString(connectionString) |
| 41 | + * .ruleManager() |
| 42 | + * .topicName(topicName) |
| 43 | + * .subscriptionName(subscriptionName) |
| 44 | + * .buildAsyncClient(); |
| 45 | + * </pre> |
| 46 | + * <!-- end com.azure.messaging.servicebus.servicebusrulemanagerasyncclient.instantiation --> |
| 47 | + * |
| 48 | + * <p><strong>Create a rule to a Service Bus subscription</strong></p> |
| 49 | + * <!-- src_embed com.azure.messaging.servicebus.servicebusrulemanagerasyncclient.createRule --> |
| 50 | + * <pre> |
| 51 | + * RuleFilter trueRuleFilter = new TrueRuleFilter(); |
| 52 | + * CreateRuleOptions options = new CreateRuleOptions(trueRuleFilter); |
| 53 | + * ruleManager.createRule("new-rule", options).subscribe( |
| 54 | + * unused -> { }, |
| 55 | + * err -> System.err.println("Error occurred when create a rule, err: " + err), |
| 56 | + * () -> System.out.println("Create complete.") |
| 57 | + * ); |
| 58 | + * </pre> |
| 59 | + * <!-- end com.azure.messaging.servicebus.servicebusrulemanagerasyncclient.createRule --> |
| 60 | + * |
| 61 | + * <p><strong>Fetch all rules.</strong></p> |
| 62 | + * <!-- src_embed com.azure.messaging.servicebus.servicebusrulemanagerasyncclient.getRules --> |
| 63 | + * <pre> |
| 64 | + * ruleManager.getRules().subscribe(ruleProperties -> System.out.println(ruleProperties.getName())); |
| 65 | + * </pre> |
| 66 | + * <!-- end com.azure.messaging.servicebus.servicebusrulemanagerasyncclient.getRules --> |
| 67 | + * |
| 68 | + * <p><strong>Delete a rule.</strong></p> |
| 69 | + * <!-- src_embed com.azure.messaging.servicebus.servicebusrulemanagerasyncclient.deleteRule --> |
| 70 | + * <pre> |
| 71 | + * ruleManager.deleteRule("exist-rule").subscribe( |
| 72 | + * unused -> { }, |
| 73 | + * err -> System.err.println("Error occurred when delete rule, err: " + err), |
| 74 | + * () -> System.out.println("Delete complete.") |
| 75 | + * ); |
| 76 | + * </pre> |
| 77 | + * <!-- end com.azure.messaging.servicebus.servicebusrulemanagerasyncclient.deleteRule --> |
| 78 | + * @see ServiceBusClientBuilder |
| 79 | + */ |
| 80 | +@ServiceClient(builder = ServiceBusClientBuilder.class, isAsync = true) |
| 81 | +public class ServiceBusRuleManagerAsyncClient implements AutoCloseable { |
| 82 | + private static final ClientLogger LOGGER = new ClientLogger(ServiceBusRuleManagerAsyncClient.class); |
| 83 | + |
| 84 | + private final String entityPath; |
| 85 | + private final MessagingEntityType entityType; |
| 86 | + private final ServiceBusConnectionProcessor connectionProcessor; |
| 87 | + private final Runnable onClientClose; |
| 88 | + private final AtomicBoolean isDisposed = new AtomicBoolean(); |
| 89 | + |
| 90 | + /** |
| 91 | + * Creates a rule manager that manages rules for a Service Bus subscription. |
| 92 | + * |
| 93 | + * @param entityPath The name of the topic and subscription. |
| 94 | + * @param entityType The type of the Service Bus resource. |
| 95 | + * @param connectionProcessor The AMQP connection to the Service Bus resource. |
| 96 | + * @param onClientClose Operation to run when the client completes. |
| 97 | + */ |
| 98 | + ServiceBusRuleManagerAsyncClient(String entityPath, MessagingEntityType entityType, |
| 99 | + ServiceBusConnectionProcessor connectionProcessor, Runnable onClientClose) { |
| 100 | + this.entityPath = Objects.requireNonNull(entityPath, "'entityPath' cannot be null."); |
| 101 | + this.entityType = Objects.requireNonNull(entityType, "'entityType' cannot be null."); |
| 102 | + this.connectionProcessor = Objects.requireNonNull(connectionProcessor, |
| 103 | + "'connectionProcessor' cannot be null."); |
| 104 | + this.onClientClose = onClientClose; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Gets the fully qualified namespace. |
| 109 | + * |
| 110 | + * @return The fully qualified namespace. |
| 111 | + */ |
| 112 | + public String getFullyQualifiedNamespace() { |
| 113 | + return connectionProcessor.getFullyQualifiedNamespace(); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Gets the name of the Service Bus resource. |
| 118 | + * |
| 119 | + * @return The name of the Service Bus resource. |
| 120 | + */ |
| 121 | + public String getEntityPath() { |
| 122 | + return entityPath; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Creates a rule to the current subscription to filter the messages reaching from topic to the subscription. |
| 127 | + * |
| 128 | + * @param ruleName Name of rule. |
| 129 | + * @param options The options for the rule to add. |
| 130 | + * @return A Mono that completes when the rule is created. |
| 131 | + * |
| 132 | + * @throws NullPointerException if {@code options}, {@code ruleName} is null. |
| 133 | + * @throws IllegalStateException if client is disposed. |
| 134 | + * @throws IllegalArgumentException if {@code ruleName} is empty string, action of {@code options} is not null and not |
| 135 | + * instanceof {@link SqlRuleAction}, filter of {@code options} is not instanceof {@link SqlRuleFilter} or |
| 136 | + * {@link CorrelationRuleFilter}. |
| 137 | + * @throws ServiceBusException if filter matches {@code ruleName} is already created in subscription. |
| 138 | + */ |
| 139 | + public Mono<Void> createRule(String ruleName, CreateRuleOptions options) { |
| 140 | + if (Objects.isNull(options)) { |
| 141 | + return monoError(LOGGER, new NullPointerException("'options' cannot be null.")); |
| 142 | + } |
| 143 | + return createRuleInternal(ruleName, options); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Creates a rule to the current subscription to filter the messages reaching from topic to the subscription. |
| 148 | + * |
| 149 | + * @param ruleName Name of rule. |
| 150 | + * @param filter The filter expression against which messages will be matched. |
| 151 | + * @return A Mono that completes when the rule is created. |
| 152 | + * |
| 153 | + * @throws NullPointerException if {@code filter}, {@code ruleName} is null. |
| 154 | + * @throws IllegalStateException if client is disposed. |
| 155 | + * @throws IllegalArgumentException if ruleName is empty string, {@code filter} is not instanceof {@link SqlRuleFilter} or |
| 156 | + * {@link CorrelationRuleFilter}. |
| 157 | + * @throws ServiceBusException if filter matches {@code ruleName} is already created in subscription. |
| 158 | + */ |
| 159 | + public Mono<Void> createRule(String ruleName, RuleFilter filter) { |
| 160 | + CreateRuleOptions options = new CreateRuleOptions(filter); |
| 161 | + return createRuleInternal(ruleName, options); |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Fetches all rules associated with the topic and subscription. |
| 166 | + * |
| 167 | + * @return A list of rules associated with the topic and subscription. |
| 168 | + * |
| 169 | + * @throws IllegalStateException if client is disposed. |
| 170 | + * @throws UnsupportedOperationException if client cannot support filter with descriptor in message body. |
| 171 | + */ |
| 172 | + public Flux<RuleProperties> getRules() { |
| 173 | + if (isDisposed.get()) { |
| 174 | + return fluxError(LOGGER, new IllegalStateException( |
| 175 | + String.format(INVALID_OPERATION_DISPOSED_RULE_MANAGER, "getRules") |
| 176 | + )); |
| 177 | + } |
| 178 | + |
| 179 | + return connectionProcessor |
| 180 | + .flatMap(connection -> connection.getManagementNode(entityPath, entityType)) |
| 181 | + .flatMapMany(ServiceBusManagementNode::getRules); |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * Removes the rule on the subscription identified by {@code ruleName}. |
| 186 | + * |
| 187 | + * @param ruleName Name of rule to delete. |
| 188 | + * @return A Mono that completes when the rule is deleted. |
| 189 | + * |
| 190 | + * @throws NullPointerException if {@code ruleName} is null. |
| 191 | + * @throws IllegalStateException if client is disposed. |
| 192 | + * @throws IllegalArgumentException if {@code ruleName} is empty string. |
| 193 | + * @throws ServiceBusException if cannot find filter matches {@code ruleName} in subscription. |
| 194 | + */ |
| 195 | + public Mono<Void> deleteRule(String ruleName) { |
| 196 | + if (isDisposed.get()) { |
| 197 | + return monoError(LOGGER, new IllegalStateException( |
| 198 | + String.format(INVALID_OPERATION_DISPOSED_RULE_MANAGER, "deleteRule") |
| 199 | + )); |
| 200 | + } |
| 201 | + |
| 202 | + if (ruleName == null) { |
| 203 | + return monoError(LOGGER, new NullPointerException("'ruleName' cannot be null.")); |
| 204 | + } else if (ruleName.isEmpty()) { |
| 205 | + return monoError(LOGGER, new IllegalArgumentException("'ruleName' cannot be an empty string.")); |
| 206 | + } |
| 207 | + |
| 208 | + return connectionProcessor |
| 209 | + .flatMap(connection -> connection.getManagementNode(entityPath, entityType)) |
| 210 | + .flatMap(managementNode -> managementNode.deleteRule(ruleName)); |
| 211 | + } |
| 212 | + |
| 213 | + /** |
| 214 | + * Disposes of the {@link ServiceBusRuleManagerAsyncClient}. If the client has a dedicated connection, the underlying |
| 215 | + * connection is also closed. |
| 216 | + */ |
| 217 | + @Override |
| 218 | + public void close() { |
| 219 | + if (isDisposed.getAndSet(true)) { |
| 220 | + return; |
| 221 | + } |
| 222 | + |
| 223 | + onClientClose.run(); |
| 224 | + } |
| 225 | + |
| 226 | + private Mono<Void> createRuleInternal(String ruleName, CreateRuleOptions options) { |
| 227 | + if (isDisposed.get()) { |
| 228 | + return monoError(LOGGER, new IllegalStateException( |
| 229 | + String.format(INVALID_OPERATION_DISPOSED_RULE_MANAGER, "createRule") |
| 230 | + )); |
| 231 | + } |
| 232 | + |
| 233 | + if (ruleName == null) { |
| 234 | + return monoError(LOGGER, new NullPointerException("'ruleName' cannot be null.")); |
| 235 | + } else if (ruleName.isEmpty()) { |
| 236 | + return monoError(LOGGER, new IllegalArgumentException("'ruleName' cannot be an empty string.")); |
| 237 | + } |
| 238 | + |
| 239 | + return connectionProcessor |
| 240 | + .flatMap(connection -> connection.getManagementNode(entityPath, entityType)) |
| 241 | + .flatMap(managementNode -> managementNode.createRule(ruleName, options)); |
| 242 | + } |
| 243 | +} |
0 commit comments