4 |
5 | |Build/Package|Status|
6 | |------|-------------|
7 | |master|[](https://maven-badges.herokuapp.com/maven-central/com.microsoft.azure/azure-servicebus)|
8 |
9 | # Moved
10 | All new work on SDK is happening in the repository https://github.com/Azure/azure-sdk-for-java/tree/master/sdk/servicebus. This repository is only to address any issues found in versions 1.2.x. And master1.0 is the branch corresponding to versions 1.2.x. Both dev and master branches moved to the new repository.
11 |
12 | # Microsoft Azure Service Bus Client for Java
13 |
14 | This is the next generation Service Bus Java client library that focuses on Queues & Topics. If you are looking for Event Hubs, follow this [link](https://github.com/azure/azure-event-hubs-java).
15 |
16 | Azure Service Bus is an asynchronous messaging cloud platform that enables you to send messages between decoupled systems. Microsoft offers this feature as a service, which means that you do not need to host any of your own hardware in order to use it.
17 |
18 | Refer to [azure.com](https://azure.microsoft.com/services/service-bus/) to learn more about Service Bus. And package can be downloaded from [Maven](http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22com.microsoft.azure%22%20AND%20a%3A%22azure-servicebus%22)
19 |
20 | ## How to provide feedback
21 |
22 | See our [Contribution Guidelines](./.github/CONTRIBUTING.md).
23 |
24 | ## Known issues
25 |
26 | ## FAQ
27 | ### Where is the API document?
28 | Click [here](https://docs.microsoft.com/en-us/java/api/overview/azure/servicebus/clientlibrary).
29 |
30 | ### Where can I find examples that use this library?
31 |
32 | The samples are located in this repo. [Java Samples](https://github.com/Azure/azure-service-bus/tree/master/samples/Java).
33 |
34 | ### Can I manage Service Bus entities with this library?
35 |
36 | Only rules management of subscription will be supported in this client library. This library focuses on Azure Service Bus Data Plane functionalities (e.g. send, receive).
37 |
38 | The standard way to manage Azure resources is by using [Azure Resource Manager](https://docs.microsoft.com/en-us/azure/azure-resource-manager/resource-group-overview). In order to use functionality that previously existed in the azure-servicebus Java library, this is [Azure Service Bus Management Library](https://mvnrepository.com/artifact/com.microsoft.azure/azure-mgmt-servicebus) which is available on Maven. And this is the [API document](https://docs.microsoft.com/en-us/java/api/com.microsoft.azure.management.servicebus). This management library will enable use cases that dynamically create/read/update/delete resources.
39 |
--------------------------------------------------------------------------------
/SECURITY.md:
--------------------------------------------------------------------------------
1 |
2 |
3 | ## Security
4 |
5 | Microsoft takes the security of our software products and services seriously, which includes all source code repositories managed through our GitHub organizations, which include [Microsoft](https://github.com/microsoft), [Azure](https://github.com/Azure), [DotNet](https://github.com/dotnet), [AspNet](https://github.com/aspnet), [Xamarin](https://github.com/xamarin), and [our GitHub organizations](https://opensource.microsoft.com/).
6 |
7 | If you believe you have found a security vulnerability in any Microsoft-owned repository that meets [Microsoft's definition of a security vulnerability](https://aka.ms/opensource/security/definition), please report it to us as described below.
8 |
9 | ## Reporting Security Issues
10 |
11 | **Please do not report security vulnerabilities through public GitHub issues.**
12 |
13 | Instead, please report them to the Microsoft Security Response Center (MSRC) at [https://msrc.microsoft.com/create-report](https://aka.ms/opensource/security/create-report).
14 |
15 | If you prefer to submit without logging in, send email to [secure@microsoft.com](mailto:secure@microsoft.com). If possible, encrypt your message with our PGP key; please download it from the [Microsoft Security Response Center PGP Key page](https://aka.ms/opensource/security/pgpkey).
16 |
17 | You should receive a response within 24 hours. If for some reason you do not, please follow up via email to ensure we received your original message. Additional information can be found at [microsoft.com/msrc](https://aka.ms/opensource/security/msrc).
18 |
19 | Please include the requested information listed below (as much as you can provide) to help us better understand the nature and scope of the possible issue:
20 |
21 | * Type of issue (e.g. buffer overflow, SQL injection, cross-site scripting, etc.)
22 | * Full paths of source file(s) related to the manifestation of the issue
23 | * The location of the affected source code (tag/branch/commit or direct URL)
24 | * Any special configuration required to reproduce the issue
25 | * Step-by-step instructions to reproduce the issue
26 | * Proof-of-concept or exploit code (if possible)
27 | * Impact of the issue, including how an attacker might exploit the issue
28 |
29 | This information will help us triage your report more quickly.
30 |
31 | If you are reporting for a bug bounty, more complete reports can contribute to a higher bounty award. Please visit our [Microsoft Bug Bounty Program](https://aka.ms/opensource/security/bounty) page for more details about our active programs.
32 |
33 | ## Preferred Languages
34 |
35 | We prefer all communications to be in English.
36 |
37 | ## Policy
38 |
39 | Microsoft follows the principle of [Coordinated Vulnerability Disclosure](https://aka.ms/opensource/security/cvd).
40 |
41 |
42 |
--------------------------------------------------------------------------------
/azure-servicebus/azure-servicebus.pom:
--------------------------------------------------------------------------------
1 |
2 | azure-servicebus
3 | Java library for Azure Service Bus
4 | 4.0.0
5 | com.microsoft.azure
6 | azure-servicebus
7 | 1.2.18
8 |
9 |
10 | The MIT License (MIT)
11 | http://opensource.org/licenses/MIT
12 | repo
13 |
14 |
15 | https://github.com/Azure/azure-service-bus-java
16 |
17 | scm:git:https://github.com/Azure/azure-service-bus-java
18 | scm:git:git@github.com:Azure/azure-service-bus-java.git
19 | HEAD
20 |
21 |
22 |
23 | microsoft
24 | Microsoft
25 |
26 |
27 |
28 |
29 |
30 |
31 | org.apache.maven.plugins
32 | maven-compiler-plugin
33 | 3.5
34 |
35 | 1.8
36 | 1.8
37 | true
38 | true
39 | true
40 |
41 |
42 |
43 | org.apache.maven.plugins
44 | maven-surefire-plugin
45 | 2.20
46 |
47 |
48 |
49 |
50 |
51 |
52 | org.apache.qpid
53 | proton-j
54 | 0.31.0
55 |
56 |
57 | junit
58 | junit
59 | 4.12
60 | test
61 |
62 |
63 | org.slf4j
64 | slf4j-api
65 | 1.7.0
66 |
67 |
68 | com.microsoft.azure
69 | adal4j
70 | 1.3.0
71 |
72 |
73 |
74 |
--------------------------------------------------------------------------------
/azure-servicebus/pom.xml:
--------------------------------------------------------------------------------
1 |
2 | azure-servicebus
3 | Java library for Azure Service Bus
4 | 4.0.0
5 | azure-servicebus
6 | ${client-current-version}
7 | https://github.com/Azure/azure-service-bus-java
8 |
9 |
10 | com.microsoft.azure
11 | azure-servicebus-parent
12 | 1.0.0
13 |
14 |
15 |
16 | UTF-8
17 | UTF-8
18 |
19 |
20 |
21 |
22 |
23 | resources
24 | true
25 |
26 |
27 |
28 |
29 | org.apache.maven.plugins
30 | maven-compiler-plugin
31 | 3.5
32 |
33 | 1.8
34 | 1.8
35 | true
36 | true
37 | true
38 |
39 |
40 |
41 | org.apache.maven.plugins
42 | maven-surefire-plugin
43 | 2.20
44 |
45 | 0
46 | true
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 | org.apache.qpid
55 | proton-j
56 | ${proton-j-version}
57 |
58 |
59 | com.microsoft.azure
60 | adal4j
61 | 1.3.0
62 |
63 |
64 | junit
65 | junit
66 | ${junit-version}
67 | test
68 |
69 |
70 | org.slf4j
71 | slf4j-api
72 | ${slf4j-version}
73 |
74 |
79 |
80 | org.slf4j
81 | slf4j-log4j12
82 | ${slf4j-version}
83 |
84 |
89 |
90 |
91 |
--------------------------------------------------------------------------------
/azure-servicebus/resources/client.properties:
--------------------------------------------------------------------------------
1 | client.version = ${project.version}
--------------------------------------------------------------------------------
/azure-servicebus/src/main/java/com/microsoft/azure/servicebus/ClientSettings.java:
--------------------------------------------------------------------------------
1 | package com.microsoft.azure.servicebus;
2 |
3 | import java.time.Duration;
4 |
5 | import com.microsoft.azure.servicebus.primitives.ClientConstants;
6 | import com.microsoft.azure.servicebus.primitives.RetryPolicy;
7 | import com.microsoft.azure.servicebus.security.TokenProvider;
8 |
9 | /**
10 | * Class encapsulating common client level settings like TokenProvider, RetryPolicy, OperationTimeout.
11 | * @since 1.2.0
12 | *
13 | */
14 | public class ClientSettings {
15 |
16 | private TokenProvider tokenProvider;
17 | private RetryPolicy retryPolicy;
18 | private Duration operationTimeout;
19 |
20 | /**
21 | * Creates a new instance with the given token provider, default retry policy and default operation timeout.
22 | * @param tokenProvider {@link TokenProvider} instance
23 | *
24 | * @see RetryPolicy#getDefault()
25 | */
26 | public ClientSettings(TokenProvider tokenProvider)
27 | {
28 | this(tokenProvider, RetryPolicy.getDefault(), Duration.ofSeconds(ClientConstants.DEFAULT_OPERATION_TIMEOUT_IN_SECONDS));
29 | }
30 |
31 | /**
32 | * Creates a new instance with the given token provider, retry policy and operation timeout.
33 | * @param tokenProvider {@link TokenProvider} instance
34 | * @param retryPolicy {@link RetryPolicy} instance
35 | * @param operationTimeout default operation timeout to be used for all client operations. Client can override this value by explicitly specifying a timeout in the operation.
36 | */
37 | public ClientSettings(TokenProvider tokenProvider, RetryPolicy retryPolicy, Duration operationTimeout)
38 | {
39 | this.tokenProvider = tokenProvider;
40 | this.retryPolicy = retryPolicy;
41 | this.operationTimeout = operationTimeout;
42 | }
43 |
44 | /**
45 | * Gets the token provider contained in this instance.
46 | * @return TokenProvider contained in this instance
47 | */
48 | public TokenProvider getTokenProvider()
49 | {
50 | return tokenProvider;
51 | }
52 |
53 | /**
54 | * Gets the retry policy contained in this instance.
55 | * @return RetryPolicy contained in this instance
56 | */
57 | public RetryPolicy getRetryPolicy()
58 | {
59 | return retryPolicy;
60 | }
61 |
62 | /**
63 | * Gets the operation timeout contained in this instance.
64 | * @return operation timeout contained in this instance
65 | */
66 | public Duration getOperationTimeout()
67 | {
68 | return operationTimeout;
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/azure-servicebus/src/main/java/com/microsoft/azure/servicebus/ExceptionPhase.java:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | package com.microsoft.azure.servicebus;
5 |
6 | /**
7 | * Enumeration to represent the phase in a message pump or session pump at which an exception occurred.
8 | *
9 | * @since 1.0
10 | */
11 | public enum ExceptionPhase {
12 | /**
13 | * Receiving messages from Azure Service Bus threw an exception.
14 | */
15 | RECEIVE,
16 | /**
17 | * Renewing the lock of a message threw an exception.
18 | */
19 | RENEWMESSAGELOCK,
20 | /**
21 | * Completing a message threw an exception
22 | */
23 | COMPLETE,
24 | /**
25 | * Abandoning a message threw an exception
26 | */
27 | ABANDON,
28 | /**
29 | * Application code in message handler or session handler threw an exception. Applications should ideally not throw exceptions from message handler or session handler.
30 | */
31 | USERCALLBACK,
32 | /**
33 | * Closing a session threw an exception.
34 | */
35 | SESSIONCLOSE,
36 | /**
37 | * Accepting a session threw an exception.
38 | */
39 | ACCEPTSESSION,
40 | /**
41 | * Renewing the lock of a session threw an exception.
42 | */
43 | RENEWSESSIONLOCK
44 | }
45 |
--------------------------------------------------------------------------------
/azure-servicebus/src/main/java/com/microsoft/azure/servicebus/ICloseable.java:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | package com.microsoft.azure.servicebus;
5 |
6 | import java.util.concurrent.CompletableFuture;
7 |
8 | import com.microsoft.azure.servicebus.primitives.ServiceBusException;
9 |
10 | /**
11 | * Defines a standard way of properly closing and disposing objects.
12 | * @since 1.0
13 | *
14 | */
15 | public interface ICloseable {
16 | /**
17 | * Closes and disposes any resources associated with this object. An object cannot be used after it is closed. This is an asynchronous method that returns a CompletableFuture immediately.
18 | * This object is completely closed when the returned CompletableFuture is completed.
19 | * @return a CompletableFuture representing the closing of this object.
20 | */
21 | CompletableFuture closeAsync();
22 |
23 | /**
24 | * Synchronously closes and disposes any resources associated with this object. Calling this method is equivalent of calling closeAsync().get(). This method blocks until this object is closed.
25 | * @throws ServiceBusException If this object cannot be properly closed.
26 | */
27 | void close() throws ServiceBusException;
28 | }
29 |
--------------------------------------------------------------------------------
/azure-servicebus/src/main/java/com/microsoft/azure/servicebus/IMessageBrowser.java:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | package com.microsoft.azure.servicebus;
5 |
6 | import java.util.Collection;
7 | import java.util.concurrent.CompletableFuture;
8 |
9 | import com.microsoft.azure.servicebus.primitives.ServiceBusException;
10 |
11 | /**
12 | * Represents a message browser that can browse messages from Azure Service Bus.
13 | */
14 | public interface IMessageBrowser {
15 |
16 | /**
17 | * reads next the active message without changing the state of the receiver or the message source.
18 | * The first call to {@link IMessageBrowser#peek()} fetches the first active message for this receiver.
19 | * Each subsequent call fetches the subsequent message in the entity.
20 | *
21 | * @return {@link Message} peeked
22 | * @throws InterruptedException if the current thread was interrupted while waiting
23 | * @throws ServiceBusException if peek failed
24 | */
25 | IMessage peek() throws InterruptedException, ServiceBusException;
26 |
27 | /**
28 | * Reads next the active message without changing the state of the receiver or the message source.
29 | *
30 | * @param fromSequenceNumber The sequence number from where to read the message.
31 | * @return {@link Message} peeked
32 | * @throws InterruptedException if the current thread was interrupted while waiting
33 | * @throws ServiceBusException if peek failed
34 | */
35 | IMessage peek(long fromSequenceNumber) throws InterruptedException, ServiceBusException;
36 |
37 | /**
38 | * Reads next batch of the active messages without changing the state of the receiver or the message source.
39 | *
40 | * @param messageCount The number of messages.
41 | * @return Batch of {@link Message} peeked
42 | * @throws InterruptedException if the current thread was interrupted while waiting
43 | * @throws ServiceBusException if peek failed
44 | */
45 | Collection peekBatch(int messageCount) throws InterruptedException, ServiceBusException;
46 |
47 | /**
48 | * Reads next batch of the active messages without changing the state of the receiver or the message source.
49 | *
50 | * @param fromSequenceNumber The sequence number from where to read the message.
51 | * @param messageCount The number of messages.
52 | * @return Batch of {@link Message} peeked
53 | * @throws InterruptedException if the current thread was interrupted while waiting
54 | * @throws ServiceBusException if peek failed
55 | */
56 | Collection peekBatch(long fromSequenceNumber, int messageCount) throws InterruptedException, ServiceBusException;
57 |
58 | /**
59 | * Asynchronously reads the active messages without changing the state of the receiver or the message source.
60 | *
61 | * @return {@link Message} peeked
62 | */
63 | CompletableFuture peekAsync();
64 |
65 | /**
66 | * Asynchronously reads next the active message without changing the state of the receiver or the message source.
67 | *
68 | * @param fromSequenceNumber The sequence number from where to read the message.
69 | * @return CompletableFuture that returns {@link Message} peeked.
70 | */
71 | CompletableFuture peekAsync(long fromSequenceNumber);
72 |
73 | /**
74 | * Asynchronously reads the next batch of active messages without changing the state of the receiver or the message source.
75 | *
76 | * @param messageCount The number of messages.
77 | * @return CompletableFuture that returns batch of {@link Message} peeked.
78 | */
79 | CompletableFuture> peekBatchAsync(int messageCount);
80 |
81 | /**
82 | * Asynchronously reads the next batch of active messages without changing the state of the receiver or the message source.
83 | *
84 | * @param fromSequenceNumber The sequence number from where to read the message.
85 | * @param messageCount The number of messages.
86 | * @return CompletableFuture that returns batch of {@link Message} peeked.
87 | */
88 | CompletableFuture> peekBatchAsync(long fromSequenceNumber, int messageCount);
89 | }
90 |
--------------------------------------------------------------------------------
/azure-servicebus/src/main/java/com/microsoft/azure/servicebus/IMessageEntityClient.java:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | package com.microsoft.azure.servicebus;
5 |
6 | /**
7 | * Represents a messaging client to an Azure Service Bus entity.
8 | *
9 | * @since 1.0
10 | */
11 | public interface IMessageEntityClient extends ICloseable {
12 | /**
13 | * Gets the path of the entity this client is sending messages to or receiving messages from.
14 | *
15 | * @return path of the entity this client is connecting to
16 | */
17 | public String getEntityPath();
18 | }
19 |
--------------------------------------------------------------------------------
/azure-servicebus/src/main/java/com/microsoft/azure/servicebus/IMessageHandler.java:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | package com.microsoft.azure.servicebus;
5 |
6 | import java.util.concurrent.CompletableFuture;
7 |
8 | /**
9 | * Defines the contract for registering the callback {@link QueueClient#registerMessageHandler(IMessageHandler)} and {@link SubscriptionClient#registerMessageHandler(IMessageHandler)} for {@link QueueClient} and {@link SubscriptionClient}.
10 | */
11 | public interface IMessageHandler {
12 |
13 | /**
14 | * The callback for message pump to pass received {@link Message}s.
15 | *
16 | * @param message The received {@link Message}.
17 | * @return CompletableFuture for the message handler.
18 | */
19 | public CompletableFuture onMessageAsync(IMessage message);
20 |
21 | /**
22 | * Receiving the exceptions that passed by pump during message processing.
23 | *
24 | * @param exception Exception received in pump.
25 | * @param phase Exception phase.
26 | */
27 | public void notifyException(Throwable exception, ExceptionPhase phase);
28 | }
29 |
--------------------------------------------------------------------------------
/azure-servicebus/src/main/java/com/microsoft/azure/servicebus/IMessageSession.java:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | package com.microsoft.azure.servicebus;
5 |
6 | import java.time.Instant;
7 | import java.util.concurrent.CompletableFuture;
8 |
9 | import com.microsoft.azure.servicebus.primitives.ServiceBusException;
10 |
11 | /**
12 | * Describes a Session object. IMessageSession can be used to perform operations on sessions.
13 | *
14 | * Service Bus Sessions, also called 'Groups' in the AMQP 1.0 protocol, are unbounded sequences of related messages. ServiceBus guarantees ordering of messages in a session.
15 | *
16 | * Any sender can create a session when submitting messages into a Topic or Queue by setting the {@link Message#sessionId} property on Message to some application defined unique identifier. At the AMQP 1.0 protocol level, this value maps to the group-id property.
17 | *
18 | * Sessions come into existence when there is at least one message with the session's SessionId in the Queue or Topic subscription. Once a Session exists, there is no defined moment or gesture for when the session expires or disappears.
19 | */
20 | public interface IMessageSession extends IMessageReceiver {
21 |
22 | /**
23 | * @return Gets the SessionId.
24 | */
25 | String getSessionId();
26 |
27 | /**
28 | * @return Gets the time that the session identified by {@link IMessageSession#getSessionId()} is locked until for this client.
29 | */
30 | Instant getLockedUntilUtc();
31 |
32 | /**
33 | * Renews the lock on the session specified by the {@link IMessageSession#getSessionId()}. The lock will be renewed based on the setting specified on the entity.
34 | *
35 | * When you accept a session, the session is locked for this client instance by the service for a duration as specified during the Queue/Subscription creation.
36 | * If processing of the session requires longer than this duration, the session-lock needs to be renewed. For each renewal, the session-lock is renewed by
37 | * the entity's LockDuration.
38 | *
39 | * Renewal of session renews all the messages in the session as well. Each individual message need not be renewed.
40 | *
41 | * @throws InterruptedException if the current thread was interrupted while waiting
42 | * @throws ServiceBusException if the renew failed.
43 | */
44 | void renewSessionLock() throws InterruptedException, ServiceBusException;
45 |
46 | /**
47 | * Renews the lock on the session specified by the {@link IMessageSession#getSessionId()}. The lock will be renewed based on the setting specified on the entity.
48 | *
49 | * @return a CompletableFuture representing the pending renew.
50 | * @see IMessageSession#renewSessionLock()
51 | */
52 | CompletableFuture renewSessionLockAsync();
53 |
54 | /**
55 | * Set a custom state on the session which can be later retrieved using {@link IMessageSession#getState()}.
56 | *
57 | * @param state The session state.
58 | * @throws InterruptedException if the current thread was interrupted while waiting
59 | * @throws ServiceBusException if the set state failed.
60 | */
61 | void setState(byte[] state) throws InterruptedException, ServiceBusException;
62 |
63 | /**
64 | * Asynchronously set a custom state on the session which can be later retrieved using {@link IMessageSession#getState()}.
65 | *
66 | * @param state The session state.
67 | * @return a CompletableFuture representing the pending session state setting.
68 | * @see IMessageSession#setState
69 | */
70 | CompletableFuture setStateAsync(byte[] state);
71 |
72 | /**
73 | * Gets the session state.
74 | *
75 | * @return The session state
76 | * @throws InterruptedException if the current thread was interrupted while waiting
77 | * @throws ServiceBusException if get state failed.
78 | */
79 | byte[] getState() throws InterruptedException, ServiceBusException;
80 |
81 | /**
82 | * Asynchronously gets the session state.
83 | * @return a CompletableFuture representing the pending session state retrieving.
84 | * @see IMessageSession#getState
85 | */
86 | CompletableFuture getStateAsync();
87 | }
88 |
--------------------------------------------------------------------------------
/azure-servicebus/src/main/java/com/microsoft/azure/servicebus/IMessageSessionEntity.java:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | package com.microsoft.azure.servicebus;
5 |
6 | import java.time.Instant;
7 | import java.util.Collection;
8 | import java.util.concurrent.CompletableFuture;
9 |
10 | import com.microsoft.azure.servicebus.primitives.ServiceBusException;
11 |
12 | /**
13 | * Represents a session full client entity.
14 | */
15 | public interface IMessageSessionEntity {
16 | // int getSessionPrefetchCount();
17 | //
18 | // void setSessionPrefetchCount(int prefetchCount);
19 | //
20 | // IMessageSession acceptMessageSession() throws InterruptedException, ServiceBusException;
21 | //
22 | // IMessageSession acceptMessageSession(Duration serverWaitTime) throws InterruptedException, ServiceBusException;
23 | //
24 | // IMessageSession acceptMessageSession(String sessionId) throws InterruptedException, ServiceBusException;
25 | //
26 | // IMessageSession acceptMessageSession(String sessionId, Duration serverWaitTime) throws InterruptedException, ServiceBusException;
27 | //
28 | // CompletableFuture acceptMessageSessionAsync();
29 | //
30 | // CompletableFuture acceptMessageSessionAsync(Duration serverWaitTime);
31 | //
32 | // CompletableFuture acceptMessageSessionAsync(String sessionId);
33 | //
34 | // CompletableFuture acceptMessageSessionAsync(String sessionId, Duration serverWaitTime);
35 |
36 | /**
37 | * Gets the message sessions, enabling you to browse sessions on queues.
38 | *
39 | * @return A collection of sessions.
40 | * @throws InterruptedException if the current thread was interrupted while waiting.
41 | * @throws ServiceBusException if get sessions failed.
42 | */
43 | Collection getMessageSessions() throws InterruptedException, ServiceBusException;
44 |
45 | /**
46 | * Retrieves all message sessions whose session state was updated since lastUpdatedTime.
47 | *
48 | * @param lastUpdatedTime The time the session was last updated.
49 | * @return A collection of sessions.
50 | * @throws InterruptedException if the current thread was interrupted while waiting.
51 | * @throws ServiceBusException if get sessions failed.
52 | */
53 | Collection getMessageSessions(Instant lastUpdatedTime) throws InterruptedException, ServiceBusException;
54 |
55 | /**
56 | * Asynchronously gets the message sessions, enabling you to browse sessions on queues.
57 | *
58 | * @return a CompletableFuture representing the pending operation to get sessions.
59 | * @see IMessageSessionEntity#getMessageSessions
60 | */
61 | CompletableFuture> getMessageSessionsAsync();
62 |
63 | /**
64 | * Asynchronously retrieves all message sessions whose session state was updated since lastUpdatedTime.
65 | *
66 | * @param lastUpdatedTime The time the session was last updated.
67 | * @return a CompletableFuture representing the pending operation to get sessions.
68 | * @see IMessageSessionEntity#getMessageSessions(Instant)
69 | */
70 | CompletableFuture> getMessageSessionsAsync(Instant lastUpdatedTime);
71 | }
72 |
--------------------------------------------------------------------------------
/azure-servicebus/src/main/java/com/microsoft/azure/servicebus/IQueueClient.java:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | package com.microsoft.azure.servicebus;
5 |
6 | /**
7 | * QueueClient can be used for all basic interactions with a Service Bus Queue.
8 | */
9 | public interface IQueueClient extends IMessageSender, IMessageAndSessionPump, IMessageEntityClient {
10 |
11 | /**
12 | * Gets the {@link ReceiveMode} of the current receiver
13 | *
14 | * @return The receive mode.
15 | */
16 | public ReceiveMode getReceiveMode();
17 |
18 | /**
19 | * Gets the name of the queue.
20 | *
21 | * @return The name of the queue.
22 | */
23 | public String getQueueName();
24 | }
25 |
--------------------------------------------------------------------------------
/azure-servicebus/src/main/java/com/microsoft/azure/servicebus/ISessionHandler.java:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | package com.microsoft.azure.servicebus;
5 |
6 | import java.util.concurrent.CompletableFuture;
7 |
8 | /**
9 | * Defines the contract for registering the session message processing callback {@link QueueClient#registerSessionHandler(ISessionHandler)} or {@link SubscriptionClient#registerSessionHandler(ISessionHandler)} for {@link QueueClient} and {@link SubscriptionClient}.
10 | */
11 | public interface ISessionHandler {
12 |
13 | /**
14 | * The callback for message pump to pass received {@link Message}s.
15 | *
16 | * @param session The {@link MessageSession} of the message.
17 | * @param message The received {@link Message}.
18 | * @return CompletableFuture for the message handler.
19 | */
20 | public CompletableFuture onMessageAsync(IMessageSession session, IMessage message);
21 |
22 | /**
23 | * Called just before a session is closed by the session pump
24 | *
25 | * @param session session being closed
26 | * @return a future that executes the action
27 | */
28 | public CompletableFuture OnCloseSessionAsync(IMessageSession session);
29 |
30 | /**
31 | * Receiving the exceptions that passed by pump during message processing.
32 | *
33 | * @param exception Exception received in pump.
34 | * @param phase Exception phase.
35 | */
36 | public void notifyException(Throwable exception, ExceptionPhase phase);
37 | }
38 |
--------------------------------------------------------------------------------
/azure-servicebus/src/main/java/com/microsoft/azure/servicebus/ISubscriptionClient.java:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | package com.microsoft.azure.servicebus;
5 |
6 | import java.util.Collection;
7 | import java.util.concurrent.CompletableFuture;
8 |
9 | import com.microsoft.azure.servicebus.primitives.ServiceBusException;
10 | import com.microsoft.azure.servicebus.rules.Filter;
11 | import com.microsoft.azure.servicebus.rules.RuleDescription;
12 |
13 | /**
14 | * SubscriptionClient can be used for all basic interactions with a Service Bus Subscription.
15 | */
16 | public interface ISubscriptionClient extends IMessageEntityClient, IMessageAndSessionPump {
17 |
18 | /**
19 | * Gets the {@link ReceiveMode} of the current receiver
20 | *
21 | * @return The receive mode.
22 | */
23 | public ReceiveMode getReceiveMode();
24 |
25 | /**
26 | * Adds a rule to the current subscription to filter the messages reaching from topic to the subscription.
27 | *
28 | * @param ruleDescription The rule description that provides the rule to add.
29 | * @throws InterruptedException if the current thread was interrupted while waiting
30 | * @throws ServiceBusException if add rule failed
31 | */
32 | public void addRule(RuleDescription ruleDescription) throws InterruptedException, ServiceBusException;
33 |
34 | /**
35 | * Asynchronously adds a rule to the current subscription to filter the messages reaching from topic to the subscription.
36 | *
37 | * @param ruleDescription The rule description that provides the rule to add.
38 | * @return a CompletableFuture representing the pending rule add operation.
39 | */
40 | public CompletableFuture addRuleAsync(RuleDescription ruleDescription);
41 |
42 | /**
43 | * Adds a rule with specified name and {@link Filter} to the current subscription to filter the messages reaching from topic to the subscription.
44 | *
45 | * @param ruleName The rule name
46 | * @param filter The {@link Filter} to add.
47 | * @throws InterruptedException if the current thread was interrupted while waiting
48 | * @throws ServiceBusException if add rule failed
49 | */
50 | public void addRule(String ruleName, Filter filter) throws InterruptedException, ServiceBusException;
51 |
52 | /**
53 | * Asynchronously adds a rule with specified name and {@link Filter} to the current subscription to filter the messages reaching from topic to the subscription.
54 | *
55 | * @param ruleName The rule name
56 | * @param filter The {@link Filter} to add.
57 | * @return a CompletableFuture representing the pending rule add operation.
58 | */
59 | public CompletableFuture addRuleAsync(String ruleName, Filter filter);
60 |
61 | /**
62 | * Asynchronously removes the rule on the subscription identified by ruleName
63 | *
64 | * @param ruleName he name of rule.
65 | * @return a CompletableFuture representing the pending rule remove operation.
66 | */
67 | public CompletableFuture removeRuleAsync(String ruleName);
68 |
69 | /**
70 | * Removes the rule on the subscription identified by ruleName
71 | *
72 | * @param ruleName The name of rule.
73 | * @throws InterruptedException if the current thread was interrupted while waiting
74 | * @throws ServiceBusException if remove rule failed
75 | */
76 | public void removeRule(String ruleName) throws InterruptedException, ServiceBusException;
77 |
78 | /**
79 | * Get all rules associated with the subscription.
80 | *
81 | * @return The collection fo the rules.
82 | * @throws InterruptedException if the current thread was interrupted while waiting
83 | * @throws ServiceBusException if get rules failed
84 | */
85 | public Collection getRules() throws ServiceBusException, InterruptedException;
86 |
87 | /**
88 | * Get all rules associated with the subscription.
89 | *
90 | * @return a CompletableFuture representing the pending get rules operation.
91 | */
92 | public CompletableFuture> getRulesAsync();
93 |
94 | /**
95 | * Gets the name of the topic, for this subscription.
96 | *
97 | * @return the name of the topic
98 | */
99 | public String getTopicName();
100 |
101 | /**
102 | * Gets the subscription name.
103 | * @return The subscription name.
104 | */
105 | public String getSubscriptionName();
106 | }
107 |
--------------------------------------------------------------------------------
/azure-servicebus/src/main/java/com/microsoft/azure/servicebus/ITopicClient.java:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | package com.microsoft.azure.servicebus;
5 |
6 | /**
7 | * TopicClient can be used for all basic interactions with a Service Bus topic.
8 | * {@code
9 | *
10 | * }
11 | */
12 | public interface ITopicClient extends IMessageSender, IMessageBrowser, IMessageEntityClient {
13 |
14 | /**
15 | * Get the name of the topic
16 | * @return the name of the topic
17 | */
18 | public String getTopicName();
19 | }
20 |
--------------------------------------------------------------------------------
/azure-servicebus/src/main/java/com/microsoft/azure/servicebus/InitializableEntity.java:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | package com.microsoft.azure.servicebus;
5 |
6 | import java.util.concurrent.CompletableFuture;
7 |
8 | import com.microsoft.azure.servicebus.primitives.ClientEntity;
9 |
10 | /**
11 | * Represents an entity that needs to be initialized before using. This class defines a standard way of properly initializing client objects.
12 | *
13 | * @since 1.0
14 | */
15 | abstract class InitializableEntity extends ClientEntity {
16 |
17 | //TODO Init and close semantics are primitive now. Fix them with support for other states like Initializing, Closing, and concurrency.
18 | protected InitializableEntity(String clientId) {
19 | super(clientId);
20 | }
21 |
22 | /**
23 | * Initializes this object. This method is asynchronous and returns a CompletableFuture immediately. Initializing of the object is complete when the returned future completes.
24 | *
25 | * @return CompletableFuture representing the initialization
26 | */
27 | abstract CompletableFuture initializeAsync();
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/azure-servicebus/src/main/java/com/microsoft/azure/servicebus/MessageBody.java:
--------------------------------------------------------------------------------
1 | package com.microsoft.azure.servicebus;
2 |
3 | import java.io.Serializable;
4 | import java.util.List;
5 |
6 | /**
7 | * This class encapsulates the body of a message. Body types map to AMQP message body types.
8 | * It has getters and setters for multiple body types.
9 | * Client should test for body type before calling corresponding get method.
10 | * Get methods not corresponding to the type of the body return null.
11 | */
12 | public class MessageBody implements Serializable{
13 |
14 | private static final long serialVersionUID = 7215009530928988502L;
15 |
16 | private MessageBodyType bodyType;
17 | private Object valueData;
18 | private List> sequenceData;
19 | private List binaryData;
20 |
21 | private MessageBody() {}
22 |
23 | /**
24 | * Creates message body of AMQPValue type.
25 | * @param value AMQPValue content of the message. It must be of a type supported by AMQP.
26 | * @return MessageBody instance wrapping around the value data.
27 | */
28 | public static MessageBody fromValueData(Object value)
29 | {
30 | if(value == null)
31 | {
32 | throw new IllegalArgumentException("Value data is null.");
33 | }
34 |
35 | MessageBody body = new MessageBody();
36 | body.bodyType = MessageBodyType.VALUE;
37 | body.valueData = value;
38 | body.sequenceData = null;
39 | body.binaryData = null;
40 | return body;
41 | }
42 |
43 | /**
44 | * Creates a message body from a list of AMQPSequence sections.Each AMQPSequence section is in turn a list of objects.
45 | * Please note that this version of the SDK supports only one AMQPSequence section in a message. It means only a list of exactly one sequence in it is accepted as message body.
46 | * @param sequenceData a list of AMQPSequence sections. Each AMQPSequence section is in turn a list of objects. Every object in each list must of a type supported by AMQP.
47 | * @return MessageBody instance wrapping around the sequence data.
48 | */
49 | public static MessageBody fromSequenceData(List> sequenceData)
50 | {
51 | if(sequenceData == null || sequenceData.size() == 0 || sequenceData.size() > 1)
52 | {
53 | throw new IllegalArgumentException("Sequence data is null or has more than one collection in it.");
54 | }
55 |
56 | MessageBody body = new MessageBody();
57 | body.bodyType = MessageBodyType.SEQUENCE;
58 | body.valueData = null;
59 | body.sequenceData = sequenceData;
60 | body.binaryData = null;
61 | return body;
62 | }
63 |
64 | /**
65 | * Creates a message body from a list of Data sections.Each Data section is a byte array.
66 | * Please note that this version of the SDK supports only one Data section in a message. It means only a list of exactly one byte array in it is accepted as message body.
67 | * @param binaryData a list of byte arrays.
68 | * @return MessageBody instance wrapping around the binary data.
69 | */
70 | public static MessageBody fromBinaryData(List binaryData)
71 | {
72 | if(binaryData == null || binaryData.size() == 0 || binaryData.size() > 1)
73 | {
74 | throw new IllegalArgumentException("Binary data is null or has more than one byte array in it.");
75 | }
76 |
77 | MessageBody body = new MessageBody();
78 | body.bodyType = MessageBodyType.BINARY;
79 | body.valueData = null;
80 | body.sequenceData = null;
81 | body.binaryData = binaryData;
82 | return body;
83 | }
84 |
85 | /**
86 | * Returns the content of message body.
87 | * @return value of message body only if the MessageBody is of Value type. Returns null otherwise.
88 | */
89 | public Object getValueData() {
90 | return valueData;
91 | }
92 |
93 | /**
94 | * Returns the content of message body.
95 | * @return a list of AMQPSequence sections only if the MessageBody is of Sequence type. Returns null otherwise. Each AMQPSequence section is in turn a list of objects.
96 | */
97 | public List> getSequenceData() {
98 | return sequenceData;
99 | }
100 |
101 | /**
102 | * Returns the content of message body.
103 | * @return message body as list of byte arrays only if the MessageBody is of Binary type. Returns null otherwise.
104 | */
105 | public List getBinaryData() {
106 | return binaryData;
107 | }
108 |
109 | /**
110 | * Return the type of content in this message body.
111 | * @return type of message content
112 | */
113 | public MessageBodyType getBodyType() {
114 | return bodyType;
115 | }
116 | }
117 |
--------------------------------------------------------------------------------
/azure-servicebus/src/main/java/com/microsoft/azure/servicebus/MessageBodyType.java:
--------------------------------------------------------------------------------
1 | package com.microsoft.azure.servicebus;
2 |
3 | /**
4 | * Enumeration to represent body type of a message.
5 | *
6 | */
7 | public enum MessageBodyType {
8 | /**
9 | * Message content is byte array, equivalent to AMQP Data.
10 | */
11 | BINARY,
12 | /**
13 | * Message content is a list of objects, equivalent to AMQP Sequence. Each object must be of a type supported by AMQP.
14 | */
15 | SEQUENCE,
16 | /**
17 | * Message content is a single object, equivalent to AMQP Value. The object must be of a type supported by AMQP.
18 | */
19 | VALUE
20 | }
21 |
--------------------------------------------------------------------------------
/azure-servicebus/src/main/java/com/microsoft/azure/servicebus/MessageHandlerOptions.java:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 | package com.microsoft.azure.servicebus;
4 |
5 | import java.time.Duration;
6 | import java.util.Locale;
7 |
8 | /**
9 | * The OnMessage handler processing options.
10 | */
11 | public final class MessageHandlerOptions {
12 | private static final boolean DEFAULT_AUTO_COMPLETE = true;
13 | private static final int DEFAULT_MAX_CONCURRENT_CALLS = 1;
14 | private static final int DEFAULT_MAX_RENEW_TIME_MINUTES = 5;
15 | private static final int DEFAULT_MESSAGE_WAIT_TIME_MINUTES = 1;
16 |
17 | private boolean autoComplete;
18 | private Duration maxAutoRenewDuration;
19 | private Duration messageWaitDuration;
20 | private int maxConcurrentCalls;
21 |
22 | /**
23 | * Default constructor for create {@link MessageHandlerOptions} with default settings.
24 | * {@link MessageHandlerOptions#getMaxConcurrentCalls()} default value is 1.
25 | * {@link MessageHandlerOptions#getMaxAutoRenewDuration()} default value is 5 minutes.
26 | * {@link MessageHandlerOptions#isAutoComplete()} default is true.
27 | */
28 | public MessageHandlerOptions() {
29 | this(DEFAULT_MAX_CONCURRENT_CALLS, DEFAULT_AUTO_COMPLETE, Duration.ofMinutes(DEFAULT_MAX_RENEW_TIME_MINUTES));
30 | }
31 |
32 | /**
33 | * Create a instance of {@link MessageHandlerOptions}.
34 | *
35 | * @param maxConcurrentCalls maximum number of concurrent calls to the onMessage handler
36 | * @param autoComplete true if the pump should automatically complete message after onMessageHandler action is completed. false otherwise.
37 | * @param maxAutoRenewDuration - Maximum duration within which the client keeps renewing the message lock if the processing of the message is not completed by the handler.
38 | */
39 | public MessageHandlerOptions(int maxConcurrentCalls, boolean autoComplete, Duration maxAutoRenewDuration) {
40 | this(maxConcurrentCalls, autoComplete, maxAutoRenewDuration, Duration.ofMinutes(DEFAULT_MESSAGE_WAIT_TIME_MINUTES));
41 | }
42 |
43 | /**
44 | * Create a instance of {@link MessageHandlerOptions}.
45 | *
46 | * @param maxConcurrentCalls maximum number of concurrent calls to the onMessage handler
47 | * @param autoComplete true if the pump should automatically complete message after onMessageHandler action is completed. false otherwise.
48 | * @param maxAutoRenewDuration - Maximum duration within which the client keeps renewing the message lock if the processing of the message is not completed by the handler.
49 | * @param messageWaitDuration duration to wait for receiving the message
50 | */
51 | public MessageHandlerOptions(int maxConcurrentCalls, boolean autoComplete, Duration maxAutoRenewDuration, Duration messageWaitDuration) {
52 | this.autoComplete = autoComplete;
53 | this.maxAutoRenewDuration = maxAutoRenewDuration;
54 | this.maxConcurrentCalls = maxConcurrentCalls;
55 | this.messageWaitDuration = messageWaitDuration;
56 | }
57 |
58 | /**
59 | * Whether the auto complete is set to true.
60 | *
61 | * @return true to complete the message processing automatically on successful execution of the operation; otherwise, false.
62 | */
63 | public boolean isAutoComplete() {
64 | return this.autoComplete;
65 | }
66 |
67 | /**
68 | * Gets the maximum number of concurrent calls to the callback the message pump should initiate.
69 | *
70 | * @return The maximum number of concurrent calls to the callback.
71 | */
72 | public int getMaxConcurrentCalls() {
73 | return this.maxConcurrentCalls;
74 | }
75 |
76 | /**
77 | * Gets the maximum duration within which the lock will be renewed automatically. This value should be greater than the longest message lock duration; for example, the LockDuration Property.
78 | *
79 | * @return The maximum duration during which locks are automatically renewed.
80 | */
81 | public Duration getMaxAutoRenewDuration() {
82 | return this.maxAutoRenewDuration;
83 | }
84 |
85 | /**
86 | * Gets the time to wait for receiving a message. Defaults to 1 minute.
87 | * @return The wait duration for receive calls.
88 | */
89 | public Duration getMessageWaitDuration() { return this.messageWaitDuration; }
90 |
91 | @Override
92 | public String toString() {
93 | return String.format(Locale.US, "MessageHandlerOptions - AutoComplete:%s, MaxConcurrentCalls:%s, MaxAutoRenewDuration:%s", this.autoComplete, this.maxConcurrentCalls, this.maxAutoRenewDuration);
94 | }
95 | }
96 |
--------------------------------------------------------------------------------
/azure-servicebus/src/main/java/com/microsoft/azure/servicebus/MessageSession.java:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | package com.microsoft.azure.servicebus;
5 |
6 | import java.net.URI;
7 | import java.time.Instant;
8 | import java.util.concurrent.CompletableFuture;
9 |
10 | import com.microsoft.azure.servicebus.primitives.MessagingEntityType;
11 | import com.microsoft.azure.servicebus.primitives.MessagingFactory;
12 | import com.microsoft.azure.servicebus.primitives.ServiceBusException;
13 |
14 | public class MessageSession extends MessageReceiver implements IMessageSession {
15 | private String requestedSessionId;
16 |
17 | MessageSession(URI namespaceEndpointURI, String entityPath, MessagingEntityType entityType, String requestedSessionId, ClientSettings clientSettings, ReceiveMode receiveMode) {
18 | super(namespaceEndpointURI, entityPath, entityType, clientSettings, receiveMode);
19 | this.requestedSessionId = requestedSessionId;
20 | }
21 |
22 | MessageSession(MessagingFactory messagingFactory, String entityPath, MessagingEntityType entityType, String requestedSessionId, ReceiveMode receiveMode) {
23 | super(messagingFactory, entityPath, entityType, receiveMode);
24 | this.requestedSessionId = requestedSessionId;
25 | }
26 |
27 | @Override
28 | protected final boolean isSessionReceiver() {
29 | return true;
30 | }
31 |
32 | @Override
33 | protected boolean isBrowsableSession() {
34 | return false;
35 | }
36 |
37 | @Override
38 | protected String getRequestedSessionId() {
39 | return this.requestedSessionId;
40 | }
41 |
42 | @Override
43 | public Instant getLockedUntilUtc() {
44 | return this.getInternalReceiver().getSessionLockedUntilUtc();
45 | }
46 |
47 | @Override
48 | public void renewSessionLock() throws InterruptedException, ServiceBusException {
49 | Utils.completeFuture(this.renewSessionLockAsync());
50 | }
51 |
52 | @Override
53 | public CompletableFuture renewSessionLockAsync() {
54 | return this.getInternalReceiver().renewSessionLocksAsync();
55 | }
56 |
57 | @Override
58 | public void setState(byte[] sessionState) throws InterruptedException, ServiceBusException {
59 | Utils.completeFuture(this.setStateAsync(sessionState));
60 | }
61 |
62 | @Override
63 | public CompletableFuture setStateAsync(byte[] sessionState) {
64 | return this.getInternalReceiver().setSessionStateAsync(sessionState);
65 | }
66 |
67 | @Override
68 | public byte[] getState() throws InterruptedException, ServiceBusException {
69 | return Utils.completeFuture(this.getStateAsync());
70 | }
71 |
72 | @Override
73 | public CompletableFuture getStateAsync() {
74 | return this.getInternalReceiver().getSessionStateAsync();
75 | }
76 |
77 | @Override
78 | public String getSessionId() {
79 | return this.getInternalReceiver().getSessionId();
80 | }
81 | }
82 |
--------------------------------------------------------------------------------
/azure-servicebus/src/main/java/com/microsoft/azure/servicebus/ReceiveMode.java:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | package com.microsoft.azure.servicebus;
5 |
6 | /**
7 | * Enumeration to represent the two receive modes Azure Service Bus supports.
8 | *
9 | * @since 1.0
10 | */
11 | public enum ReceiveMode {
12 | /**
13 | * In this mode, received message is not deleted from the queue or subscription, instead it is temporarily locked to the receiver, making it invisible to other receivers. Then the service waits for one of the three events
14 | *
15 | *
If the receiver processes the message successfully, it calls complete and the message will be deleted.
16 | *
If the receiver decides that it can't process the message successfully, it calls abandon and the message will be unlocked and made available to other receivers.
17 | *
If the receiver wants to defer the processing of the message to a later point in time, it calls defer and the message will be deferred. A deferred can only be received by its sequence number.
18 | *
If the receiver wants to dead-letter the message, it calls deadLetter and the message will be moved to a special sub-queue called deadletter queue.
19 | *
If the receiver calls neither of these methods within a configurable period of time (by default, 60 seconds), the service assumes the receiver has failed. In this case, it behaves as if the receiver had called abandon, making the message available to other receivers
20 | *
21 | */
22 | PEEKLOCK,
23 | /**
24 | * In this mode, received message is removed from the queue or subscription and immediately deleted. This option is simple, but if the receiver crashes
25 | * before it finishes processing the message, the message is lost. Because it's been removed from the queue, no other receiver can access it.
26 | */
27 | RECEIVEANDDELETE
28 | }
29 |
--------------------------------------------------------------------------------
/azure-servicebus/src/main/java/com/microsoft/azure/servicebus/SessionBrowser.java:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | package com.microsoft.azure.servicebus;
5 |
6 | import java.util.ArrayList;
7 | import java.util.Collection;
8 | import java.util.Date;
9 | import java.util.concurrent.CompletableFuture;
10 |
11 | import org.slf4j.Logger;
12 | import org.slf4j.LoggerFactory;
13 |
14 | import com.microsoft.azure.servicebus.primitives.MessagingEntityType;
15 | import com.microsoft.azure.servicebus.primitives.MessagingFactory;
16 | import com.microsoft.azure.servicebus.primitives.MiscRequestResponseOperationHandler;
17 |
18 | final class SessionBrowser {
19 | private static final Logger TRACE_LOGGER = LoggerFactory.getLogger(SessionBrowser.class);
20 | private static final int PAGESIZE = 100;
21 | // .net DateTime.MaxValue need to be passed
22 | private static final Date MAXDATE = new Date(253402300800000l);
23 |
24 | private final MessagingFactory messagingFactory;
25 | private final String entityPath;
26 | private final MessagingEntityType entityType;
27 | private MiscRequestResponseOperationHandler miscRequestResponseHandler;
28 |
29 | SessionBrowser(MessagingFactory messagingFactory, String entityPath, MessagingEntityType entityType, MiscRequestResponseOperationHandler miscRequestResponseHandler) {
30 | this.messagingFactory = messagingFactory;
31 | this.entityPath = entityPath;
32 | this.entityType = entityType;
33 | this.miscRequestResponseHandler = miscRequestResponseHandler;
34 | }
35 |
36 | public CompletableFuture> getMessageSessionsAsync() {
37 | return this.getMessageSessionsAsync(MAXDATE);
38 | }
39 |
40 | public CompletableFuture> getMessageSessionsAsync(Date lastUpdatedTime) {
41 | return this.getMessageSessionsAsync(lastUpdatedTime, 0, null);
42 | }
43 |
44 | private CompletableFuture> getMessageSessionsAsync(Date lastUpdatedTime, int lastReceivedSkip, String lastSessionId) {
45 | TRACE_LOGGER.debug("Getting '{}' browsable sessions from entity '{}', lastUpdatedTime '{}', lastReceivedSkip '{}', lastSessionId '{}'", PAGESIZE, this.entityPath, lastUpdatedTime, lastReceivedSkip, lastSessionId);
46 | return this.miscRequestResponseHandler.getMessageSessionsAsync(lastUpdatedTime, lastReceivedSkip, PAGESIZE, lastSessionId).thenComposeAsync((p) ->
47 | {
48 | int newLastReceivedSkip = p.getSecondItem();
49 | String[] sessionIds = p.getFirstItem();
50 | ArrayList sessionsList = new ArrayList<>();
51 | if (sessionIds != null && sessionIds.length > 0) {
52 | TRACE_LOGGER.debug("Got '{}' browsable sessions from entity '{}', receivedSkip '{}'", sessionIds.length, this.entityPath, newLastReceivedSkip);
53 | CompletableFuture[] initFutures = new CompletableFuture[sessionIds.length];
54 | int initFutureIndex = 0;
55 | String newLastSessionId = sessionIds[sessionIds.length - 1];
56 | for (String sessionId : sessionIds) {
57 | BrowsableMessageSession browsableSession = new BrowsableMessageSession(sessionId, this.messagingFactory, this.entityPath, this.entityType);
58 | sessionsList.add(browsableSession);
59 | initFutures[initFutureIndex++] = browsableSession.initializeAsync();
60 | }
61 | CompletableFuture allInitFuture = CompletableFuture.allOf(initFutures);
62 | return allInitFuture.thenComposeAsync((v) -> getMessageSessionsAsync(lastUpdatedTime, newLastReceivedSkip, newLastSessionId), MessagingFactory.INTERNAL_THREAD_POOL).thenApply((c) -> {
63 | sessionsList.addAll(c);
64 | return sessionsList;
65 | });
66 | } else {
67 | TRACE_LOGGER.debug("Got no browsable sessions from entity '{}'", this.entityPath);
68 | return CompletableFuture.completedFuture(sessionsList);
69 | }
70 | }, MessagingFactory.INTERNAL_THREAD_POOL);
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/azure-servicebus/src/main/java/com/microsoft/azure/servicebus/Utils.java:
--------------------------------------------------------------------------------
1 | // Copyright (c) Microsoft. All rights reserved.
2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3 |
4 | package com.microsoft.azure.servicebus;
5 |
6 | import java.util.ArrayList;
7 | import java.util.List;
8 | import java.util.concurrent.CompletableFuture;
9 | import java.util.concurrent.ExecutionException;
10 |
11 | import com.microsoft.azure.servicebus.primitives.ServiceBusException;
12 |
13 | final class Utils {
14 |
15 | static T completeFuture(CompletableFuture future) throws InterruptedException, ServiceBusException {
16 | try {
17 | return future.get();
18 | } catch (InterruptedException ie) {
19 | // Rare instance
20 | throw ie;
21 | } catch (ExecutionException ee) {
22 | Throwable cause = ee.getCause();
23 | if(cause instanceof RuntimeException)
24 | {
25 | throw (RuntimeException)cause;
26 | }
27 | else if (cause instanceof Error)
28 | {
29 | throw (Error)cause;
30 | }
31 | else if (cause instanceof ServiceBusException)
32 | {
33 | throw (ServiceBusException) cause;
34 | }
35 | else
36 | {
37 | throw new ServiceBusException(true, cause);
38 | }
39 | }
40 | }
41 |
42 | static void assertNonNull(String argumentName, Object argument) {
43 | if (argument == null)
44 | throw new IllegalArgumentException("Argument '" + argumentName + "' is null.");
45 | }
46 |
47 | static MessageBody fromSequence(List