-
Notifications
You must be signed in to change notification settings - Fork 582
637: Configurable correlatorId generation for RPCClient #638
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
src/main/java/com/rabbitmq/client/IncrementingCorrelationIdGenerator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.rabbitmq.client; | ||
|
||
import java.util.function.Supplier; | ||
|
||
public class IncrementingCorrelationIdGenerator implements Supplier<String> { | ||
|
||
private final String _prefix; | ||
private int _correlationId; | ||
|
||
public IncrementingCorrelationIdGenerator(String _prefix) { | ||
this._prefix = _prefix; | ||
} | ||
|
||
@Override | ||
public String get() { | ||
return _prefix + _correlationId++; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ | |
import java.util.Map.Entry; | ||
import java.util.concurrent.TimeoutException; | ||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
|
||
import com.rabbitmq.client.impl.MethodArgumentReader; | ||
import com.rabbitmq.client.impl.MethodArgumentWriter; | ||
|
@@ -79,12 +80,14 @@ public class RpcClient { | |
} | ||
}; | ||
|
||
public static Supplier<String> DEFAULT_CORRELATION_ID_GENERATOR = new IncrementingCorrelationIdGenerator(""); | ||
|
||
private final Function<Object, Response> _replyHandler; | ||
|
||
/** Map from request correlation ID to continuation BlockingCell */ | ||
private final Map<String, BlockingCell<Object>> _continuationMap = new HashMap<String, BlockingCell<Object>>(); | ||
/** Contains the most recently-used request correlation ID */ | ||
private int _correlationId; | ||
private final Supplier<String> _correlationIdGenerator; | ||
|
||
/** Consumer attached to our reply queue */ | ||
private DefaultConsumer _consumer; | ||
|
@@ -109,7 +112,7 @@ public RpcClient(RpcClientParams params) throws | |
_timeout = params.getTimeout(); | ||
_useMandatory = params.shouldUseMandatory(); | ||
_replyHandler = params.getReplyHandler(); | ||
_correlationId = 0; | ||
_correlationIdGenerator = params.getCorrelationIdGenerator(); | ||
|
||
_consumer = setupConsumer(); | ||
if (_useMandatory) { | ||
|
@@ -208,8 +211,7 @@ public Response doCall(AMQP.BasicProperties props, byte[] message, int timeout) | |
BlockingCell<Object> k = new BlockingCell<Object>(); | ||
String replyId; | ||
synchronized (_continuationMap) { | ||
_correlationId++; | ||
replyId = "" + _correlationId; | ||
replyId = _correlationIdGenerator.get(); | ||
props = ((props==null) ? new AMQP.BasicProperties.Builder() : props.builder()) | ||
.correlationId(replyId).replyTo(_replyTo).build(); | ||
_continuationMap.put(replyId, k); | ||
|
@@ -389,14 +391,6 @@ public Map<String, BlockingCell<Object>> getContinuationMap() { | |
return _continuationMap; | ||
} | ||
|
||
/** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a breaking change. The last correlation ID must be kept in some way, likely by keeping the |
||
* Retrieve the correlation id. | ||
* @return the most recently used correlation id | ||
*/ | ||
public int getCorrelationId() { | ||
return _correlationId; | ||
} | ||
|
||
/** | ||
* Retrieve the consumer. | ||
* @return an interface to the client's consumer object | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As-is this static instance may be used by more than one
RpcClient
instance. In that caseSupplier#get
may be invoked concurrently, as thesynchronized (_continuationMap)
block only ensures exclusive access fordoCall
invocations on the same client. This is a problem because as-isIncrementingCorrelationIdGenerator
is not thread-safe.One option would be to replace
int _correlationId
with anAtomicInteger
(or perhaps more appropriately, anAtomicLong
.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. This should not be static. Will remove that.