/
ServiceBusSenderClient.java
335 lines (310 loc) · 15.9 KB
/
ServiceBusSenderClient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.messaging.servicebus;
import com.azure.core.amqp.exception.AmqpException;
import com.azure.core.annotation.ServiceClient;
import com.azure.core.util.IterableStream;
import com.azure.messaging.servicebus.models.CreateMessageBatchOptions;
import java.time.Duration;
import java.time.OffsetDateTime;
import java.util.Objects;
/**
* A <b>synchronous</b> sender responsible for sending {@link ServiceBusMessage} to specific queue or topic on
* Azure Service Bus.
*
* <p><strong>Create an instance of sender</strong></p>
* {@codesnippet com.azure.messaging.servicebus.servicebussenderclient.instantiation}
*
* <p><strong>Send messages to a Service Bus resource</strong></p>
* {@codesnippet com.azure.messaging.servicebus.servicebussenderclient.createMessageBatch}
*
* <p><strong>Send messages using a size-limited {@link ServiceBusMessageBatch}</strong></p>
* {@codesnippet com.azure.messaging.servicebus.servicebussenderclient.createMessageBatch#CreateMessageBatchOptions-int}
*
* @see ServiceBusClientBuilder#sender()
* @see ServiceBusSenderAsyncClient To communicate with a Service Bus resource using an asynchronous client.
*/
@ServiceClient(builder = ServiceBusClientBuilder.class)
public final class ServiceBusSenderClient implements AutoCloseable {
private final ServiceBusSenderAsyncClient asyncClient;
private final Duration tryTimeout;
/**
* Creates a new instance of {@link ServiceBusSenderClient} that sends messages to an Azure Service Bus.
*
* @throws NullPointerException if {@code asyncClient} or {@code tryTimeout} is null.
*/
ServiceBusSenderClient(ServiceBusSenderAsyncClient asyncClient, Duration tryTimeout) {
this.asyncClient = Objects.requireNonNull(asyncClient, "'asyncClient' cannot be null.");
this.tryTimeout = Objects.requireNonNull(tryTimeout, "'tryTimeout' cannot be null.");
}
/**
* Cancels the enqueuing of a scheduled message, if they are not already enqueued.
*
* @param sequenceNumber The sequence number of the message to cancel.
*
* @throws IllegalArgumentException if {@code sequenceNumber} is negative.
* @throws ServiceBusException If the message could not be cancelled.
*/
public void cancelScheduledMessage(long sequenceNumber) {
asyncClient.cancelScheduledMessage(sequenceNumber).block(tryTimeout);
}
/**
* Cancels the enqueuing of scheduled messages, if they are not already enqueued.
*
* @param sequenceNumbers The sequence numbers of messages to cancel.
*
* @throws NullPointerException if {@code sequenceNumbers} is null.
* @throws ServiceBusException If the messages could not be cancelled.
*/
public void cancelScheduledMessages(Iterable<Long> sequenceNumbers) {
asyncClient.cancelScheduledMessages(sequenceNumbers).block(tryTimeout);
}
/**
* Creates a {@link ServiceBusMessageBatch} that can fit as many messages as the transport allows.
*
* @return A {@link ServiceBusMessageBatch} that can fit as many messages as the transport allows.
*
* @throws ServiceBusException if the message batch could not be created.
*/
public ServiceBusMessageBatch createMessageBatch() {
return asyncClient.createMessageBatch().block(tryTimeout);
}
/**
* Creates an {@link ServiceBusMessageBatch} configured with the options specified.
*
* @param options A set of options used to configure the {@link ServiceBusMessageBatch}.
* @return A new {@link ServiceBusMessageBatch} configured with the given options.
*
* @throws NullPointerException if {@code options} is null.
* @throws ServiceBusException if the message batch could not be created.
*/
public ServiceBusMessageBatch createMessageBatch(CreateMessageBatchOptions options) {
Objects.requireNonNull(options, "'options' cannot be null.");
return asyncClient.createMessageBatch(options).block(tryTimeout);
}
/**
* Gets the name of the Service Bus resource.
*
* @return The name of the Service Bus resource.
*/
public String getEntityPath() {
return asyncClient.getEntityPath();
}
/**
* Gets the fully qualified namespace.
*
* @return The fully qualified namespace.
*/
public String getFullyQualifiedNamespace() {
return asyncClient.getFullyQualifiedNamespace();
}
/**
* Sends a message to a Service Bus queue or topic.
*
* @param message Message to be sent to Service Bus queue or topic.
*
* @throws NullPointerException if {@code message} is {@code null}.
* @throws AmqpException if {@code message} is larger than the maximum allowed size of a single message.
* @throws ServiceBusException if the message could not be sent.
* @throws IllegalStateException if sender is already disposed.
*/
public void sendMessage(ServiceBusMessage message) {
Objects.requireNonNull(message, "'message' cannot be null.");
asyncClient.sendMessage(message).block(tryTimeout);
}
/**
* Sends a set of {@link ServiceBusMessage} to a Service Bus queue or topic using a batched approach.
* If the size of messages exceed the maximum size of a single batch, an exception will be triggered and the send
* will fail. By default, the message size is the max amount allowed on the link.
*
* @param messages Messages to be sent to Service Bus queue or topic.
*
* @throws NullPointerException if {@code messages} is {@code null}.
* @throws AmqpException if {@code messages} are larger than the maximum allowed size of a single batch.
* @throws ServiceBusException if the messages could not be sent.
* @throws IllegalStateException if sender is already disposed.
*/
public void sendMessages(Iterable<ServiceBusMessage> messages) {
asyncClient.sendMessages(messages).block(tryTimeout);
}
/**
* Sends a message batch to the Azure Service Bus entity this sender is connected to.
*
* @param batch of messages which allows client to send maximum allowed size for a batch of messages.
*
* @throws NullPointerException if {@code batch} is {@code null}.
* @throws IllegalStateException if sender is already disposed.
* @throws ServiceBusException if the message batch could not be sent.
*/
public void sendMessages(ServiceBusMessageBatch batch) {
Objects.requireNonNull(batch, "'batch' cannot be null.");
asyncClient.sendMessages(batch).block(tryTimeout);
}
/**
* Sends a message to a Service Bus queue or topic.
*
* @param message Message to be sent to Service Bus queue or topic.
* @param transactionContext to be set on message before sending to Service Bus.
*
* @throws NullPointerException if {@code message}, {@code transactionContext} or
* {@code transactionContext.transactionId} is {@code null}.
* @throws AmqpException if {@code message} is larger than the maximum allowed size of a single message.
* @throws ServiceBusException if the message could not be sent.
* @throws IllegalStateException if sender is already disposed.
*/
public void sendMessage(ServiceBusMessage message, ServiceBusTransactionContext transactionContext) {
asyncClient.sendMessage(message, transactionContext).block(tryTimeout);
}
/**
* Sends a set of {@link ServiceBusMessage} to a Service Bus queue or topic using a batched approach.
* If the size of messages exceed the maximum size of a single batch, an exception will be triggered and the send
* will fail. By default, the message size is the max amount allowed on the link.
*
* @param messages Messages to be sent to Service Bus queue or topic.
* @param transactionContext to be set on message before sending to Service Bus.
*
* @throws NullPointerException if {@code messages}, {@code transactionContext} or
* {@code transactionContext.transactionId} is {@code null}.
* @throws AmqpException if {@code messages} are larger than the maximum allowed size of a single batch.
* @throws ServiceBusException if messages could not be sent.
* @throws IllegalStateException if sender is already disposed.
*/
public void sendMessages(Iterable<ServiceBusMessage> messages, ServiceBusTransactionContext transactionContext) {
asyncClient.sendMessages(messages, transactionContext).block(tryTimeout);
}
/**
* Sends a message batch to the Azure Service Bus entity this sender is connected to.
*
* @param batch of messages which allows client to send maximum allowed size for a batch of messages.
* @param transactionContext to be set on message before sending to Service Bus.
*
* @throws NullPointerException if {@code batch}, {@code transactionContext} or
* {@code transactionContext.transactionId} is {@code null}.
* @throws ServiceBusException if message batch could not be sent.
* @throws IllegalStateException if sender is already disposed.
*/
public void sendMessages(ServiceBusMessageBatch batch, ServiceBusTransactionContext transactionContext) {
asyncClient.sendMessages(batch, transactionContext).block(tryTimeout);
}
/**
* Sends a scheduled message to the Azure Service Bus entity this sender is connected to. A scheduled message is
* enqueued and made available to receivers only at the scheduled enqueue time.
*
* @param message Message to be sent to the Service Bus Queue or Topic.
* @param scheduledEnqueueTime Datetime at which the message should appear in the Service Bus queue or topic.
*
* @return The sequence number of the scheduled message which can be used to cancel the scheduling of the message.
*
* @throws NullPointerException if {@code message} or {@code scheduledEnqueueTime} is {@code null}.
* @throws ServiceBusException If the message could not be scheduled.
* @throws IllegalStateException if sender is already disposed.
*/
public Long scheduleMessage(ServiceBusMessage message, OffsetDateTime scheduledEnqueueTime) {
return asyncClient.scheduleMessage(message, scheduledEnqueueTime).block(tryTimeout);
}
/**
* Sends a scheduled message to the Azure Service Bus entity this sender is connected to. A scheduled message is
* enqueued and made available to receivers only at the scheduled enqueue time.
*
* @param message Message to be sent to the Service Bus Queue or Topic.
* @param scheduledEnqueueTime Datetime at which the message should appear in the Service Bus queue or topic.
* @param transactionContext to be set on message before sending to Service Bus.
*
* @return The sequence number of the scheduled message which can be used to cancel the scheduling of the message.
*
* @throws IllegalStateException if sender is already disposed.
* @throws NullPointerException if {@code message}, {@code scheduledEnqueueTime}, {@code transactionContext} or
* {@code transactionContext.transactionId} is {@code null}.
* @throws ServiceBusException If the message could not be scheduled.
*/
public Long scheduleMessage(ServiceBusMessage message, OffsetDateTime scheduledEnqueueTime,
ServiceBusTransactionContext transactionContext) {
return asyncClient.scheduleMessage(message, scheduledEnqueueTime, transactionContext).block(tryTimeout);
}
/**
* Sends a batch of scheduled messages to the Azure Service Bus entity this sender is connected to. A scheduled
* message is enqueued and made available to receivers only at the scheduled enqueue time.
*
* @param messages Messages to be sent to the Service Bus queue or topic.
* @param scheduledEnqueueTime Instant at which the message should appear in the Service Bus queue or topic.
*
* @return Sequence numbers of the scheduled messages which can be used to cancel the messages.
*
* @throws IllegalStateException if sender is already disposed.
* @throws NullPointerException If {@code messages} or {@code scheduledEnqueueTime} is {@code null}.
* @throws ServiceBusException If the messages could not be scheduled.
*/
public Iterable<Long> scheduleMessages(Iterable<ServiceBusMessage> messages, OffsetDateTime scheduledEnqueueTime) {
return new IterableStream<>(asyncClient.scheduleMessages(messages, scheduledEnqueueTime));
}
/**
* Sends a batch of scheduled messages to the Azure Service Bus entity this sender is connected to. A scheduled
* message is enqueued and made available to receivers only at the scheduled enqueue time.
*
* @param messages Messages to be sent to the Service Bus Queue or Topic.
* @param scheduledEnqueueTime Instant at which the message should appear in the Service Bus queue or topic.
* @param transactionContext Transaction to associate with the operation.
*
* @return Sequence numbers of the scheduled messages which can be used to cancel the messages.
*
* @throws IllegalStateException if sender is already disposed.
* @throws NullPointerException If {@code messages}, {@code scheduledEnqueueTime}, {@code transactionContext} or
* {@code transactionContext.transactionId} is {@code null}.
* @throws ServiceBusException If the messages could not be scheduled.
*/
public Iterable<Long> scheduleMessages(Iterable<ServiceBusMessage> messages, OffsetDateTime scheduledEnqueueTime,
ServiceBusTransactionContext transactionContext) {
return new IterableStream<>(asyncClient.scheduleMessages(messages, scheduledEnqueueTime, transactionContext));
}
/**
* Starts a new transaction on Service Bus. The {@link ServiceBusTransactionContext} should be passed along to all
* operations that need to be in this transaction.
*
* @return A new {@link ServiceBusTransactionContext}.
*
* @throws IllegalStateException if sender is already disposed.
* @throws IllegalStateException if the sender is disposed.
* @throws ServiceBusException if a transaction cannot be created.
*
* @see ServiceBusReceiverClient#createTransaction()
*/
public ServiceBusTransactionContext createTransaction() {
return asyncClient.createTransaction().block(tryTimeout);
}
/**
* Commits the transaction given {@link ServiceBusTransactionContext}.
*
* @param transactionContext to be committed.
*
* @throws IllegalStateException if sender is already disposed.
* @throws NullPointerException if {@code transactionContext} or {@code transactionContext.transactionId} is null.
* @throws ServiceBusException if the transaction could not be committed.
*
* @see ServiceBusReceiverClient#commitTransaction(ServiceBusTransactionContext)
*/
public void commitTransaction(ServiceBusTransactionContext transactionContext) {
asyncClient.commitTransaction(transactionContext).block(tryTimeout);
}
/**
* Rollbacks the transaction given and all operations associated with it.
*
* @param transactionContext The transaction to rollback.
*
* @throws IllegalStateException if sender is already disposed.
* @throws NullPointerException if {@code transactionContext} or {@code transactionContext.transactionId} is null.
* @throws ServiceBusException if the transaction could not be rolled back.
*
* @see ServiceBusReceiverClient#rollbackTransaction(ServiceBusTransactionContext)
*/
public void rollbackTransaction(ServiceBusTransactionContext transactionContext) {
asyncClient.rollbackTransaction(transactionContext).block(tryTimeout);
}
/**
* Disposes of the {@link ServiceBusSenderClient}. If the client has a dedicated connection, the underlying
* connection is also closed.
*/
@Override
public void close() {
asyncClient.close();
}
}