Skip to content

Commit

Permalink
Create overloads for deadlettertopic.
Browse files Browse the repository at this point in the history
Signed-off-by: Artur Souza <[email protected]>
  • Loading branch information
artursouza committed May 5, 2023
1 parent a752adf commit 74070be
Showing 1 changed file with 136 additions and 116 deletions.
252 changes: 136 additions & 116 deletions sdk-springboot/src/main/java/io/dapr/springboot/DaprRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,133 +23,153 @@
*/
class DaprRuntime {

/**
* The singleton instance.
*/
private static volatile DaprRuntime instance;

/**
* Map of subscription builders.
*/
private final Map<DaprTopicKey, DaprSubscriptionBuilder> subscriptionBuilders = new HashMap<>();

/**
* DaprRuntime should be used as a singleton, using {@link DaprRuntime#getInstance()}. The
* constructor's default scope is available for unit tests only.
*/
private DaprRuntime() {
}

/**
* Returns an DaprRuntime object.
*
* @return An DaprRuntime object.
*/
public static DaprRuntime getInstance() {
if (instance == null) {
synchronized (DaprRuntime.class) {
/**
* The singleton instance.
*/
private static volatile DaprRuntime instance;

/**
* Map of subscription builders.
*/
private final Map<DaprTopicKey, DaprSubscriptionBuilder> subscriptionBuilders = new HashMap<>();

/**
* DaprRuntime should be used as a singleton, using {@link DaprRuntime#getInstance()}. The
* constructor's default scope is available for unit tests only.
*/
private DaprRuntime() {
}

/**
* Returns an DaprRuntime object.
*
* @return An DaprRuntime object.
*/
public static DaprRuntime getInstance() {
if (instance == null) {
instance = new DaprRuntime();
synchronized (DaprRuntime.class) {
if (instance == null) {
instance = new DaprRuntime();
}
}
}
}
}

return instance;
}

/**
* Adds a topic to the list of subscribed topics.
*
* @param pubSubName PubSub name to subscribe to.
* @param topicName Name of the topic being subscribed to.
* @param match Match expression for this route.
* @param priority Priority for this match relative to others.
* @param route Destination route for requests.
* @param metadata Metadata for extended subscription functionality.
*/
public synchronized void addSubscribedTopic(String pubSubName,
String topicName,
String match,
int priority,
String route,
Map<String, String> metadata) {
this.addSubscribedTopic(pubSubName, topicName, match, priority, route, "",
metadata);
}


/**
* Adds a topic to the list of subscribed topics.
*
* @param pubSubName PubSub name to subscribe to.
* @param topicName Name of the topic being subscribed to.
* @param match Match expression for this route.
* @param priority Priority for this match relative to others.
* @param route Destination route for requests.
* @param deadLetterTopic Name of topic to forward undeliverable messages.
* @param metadata Metadata for extended subscription functionality.
*/
public synchronized void addSubscribedTopic(String pubSubName,
String topicName,
String match,
int priority,
String route,
String deadLetterTopic,
Map<String, String> metadata) {
this.addSubscribedTopic(pubSubName, topicName, match, priority, route, deadLetterTopic,
metadata, null);
}

/**
* Adds a topic to the list of subscribed topics.
*
* @param pubSubName PubSub name to subscribe to.
* @param topicName Name of the topic being subscribed to.
* @param match Match expression for this route.
* @param priority Priority for this match relative to others.
* @param route Destination route for requests.
* @param deadLetterTopic Name of topic to forward undeliverable messages.
* @param metadata Metadata for extended subscription functionality.
* @param bulkSubscribe Bulk subscribe configuration.
*/
public synchronized void addSubscribedTopic(String pubSubName,
String topicName,
String match,
int priority,
String route,
String deadLetterTopic,
Map<String, String> metadata,
DaprTopicBulkSubscribe bulkSubscribe) {
DaprTopicKey topicKey = new DaprTopicKey(pubSubName, topicName);

DaprSubscriptionBuilder builder = subscriptionBuilders.get(topicKey);
if (builder == null) {
builder = new DaprSubscriptionBuilder(pubSubName, topicName);
subscriptionBuilders.put(topicKey, builder);
return instance;
}

if (match.length() > 0) {
builder.addRule(route, match, priority);
} else {
builder.setDefaultPath(route);
/**
* Adds a topic to the list of subscribed topics.
*
* @param pubSubName PubSub name to subscribe to.
* @param topicName Name of the topic being subscribed to.
* @param match Match expression for this route.
* @param priority Priority for this match relative to others.
* @param route Destination route for requests.
* @param metadata Metadata for extended subscription functionality.
*/
public synchronized void addSubscribedTopic(String pubSubName,
String topicName,
String match,
int priority,
String route,
Map<String, String> metadata) {
this.addSubscribedTopic(pubSubName, topicName, match, priority, route, metadata, null);
}

if (metadata != null && !metadata.isEmpty()) {
builder.setMetadata(metadata);
/**
* Adds a topic to the list of subscribed topics.
*
* @param pubSubName PubSub name to subscribe to.
* @param topicName Name of the topic being subscribed to.
* @param match Match expression for this route.
* @param priority Priority for this match relative to others.
* @param route Destination route for requests.
* @param deadLetterTopic Name of topic to forward undeliverable messages.
* @param metadata Metadata for extended subscription functionality.
*/
public synchronized void addSubscribedTopic(String pubSubName,
String topicName,
String match,
int priority,
String route,
String deadLetterTopic,
Map<String, String> metadata) {
this.addSubscribedTopic(pubSubName, topicName, match, priority, route, deadLetterTopic,
metadata, null);
}

if (deadLetterTopic != null && !deadLetterTopic.isEmpty()) {
builder.setDeadLetterTopic(deadLetterTopic);
/**
* Adds a topic to the list of subscribed topics.
*
* @param pubSubName PubSub name to subscribe to.
* @param topicName Name of the topic being subscribed to.
* @param match Match expression for this route.
* @param priority Priority for this match relative to others.
* @param route Destination route for requests.
* @param metadata Metadata for extended subscription functionality.
* @param bulkSubscribe Bulk subscribe configuration.
*/
public synchronized void addSubscribedTopic(String pubSubName,
String topicName,
String match,
int priority,
String route,
Map<String, String> metadata,
DaprTopicBulkSubscribe bulkSubscribe) {
this.addSubscribedTopic(pubSubName, topicName, match, priority, route, null,
metadata, bulkSubscribe);
}

if (bulkSubscribe != null) {
builder.setBulkSubscribe(bulkSubscribe);
/**
* Adds a topic to the list of subscribed topics.
*
* @param pubSubName PubSub name to subscribe to.
* @param topicName Name of the topic being subscribed to.
* @param match Match expression for this route.
* @param priority Priority for this match relative to others.
* @param route Destination route for requests.
* @param deadLetterTopic Name of topic to forward undeliverable messages.
* @param metadata Metadata for extended subscription functionality.
* @param bulkSubscribe Bulk subscribe configuration.
*/
public synchronized void addSubscribedTopic(String pubSubName,
String topicName,
String match,
int priority,
String route,
String deadLetterTopic,
Map<String, String> metadata,
DaprTopicBulkSubscribe bulkSubscribe) {
DaprTopicKey topicKey = new DaprTopicKey(pubSubName, topicName);

DaprSubscriptionBuilder builder = subscriptionBuilders.get(topicKey);
if (builder == null) {
builder = new DaprSubscriptionBuilder(pubSubName, topicName);
subscriptionBuilders.put(topicKey, builder);
}

if (match.length() > 0) {
builder.addRule(route, match, priority);
} else {
builder.setDefaultPath(route);
}

if (metadata != null && !metadata.isEmpty()) {
builder.setMetadata(metadata);
}

if (deadLetterTopic != null && !deadLetterTopic.isEmpty()) {
builder.setDeadLetterTopic(deadLetterTopic);
}

if (bulkSubscribe != null) {
builder.setBulkSubscribe(bulkSubscribe);
}
}
}

public synchronized DaprTopicSubscription[] listSubscribedTopics() {
List<DaprTopicSubscription> values = subscriptionBuilders.values().stream()
.map(b -> b.build()).collect(Collectors.toList());
return values.toArray(new DaprTopicSubscription[0]);
}
public synchronized DaprTopicSubscription[] listSubscribedTopics() {
List<DaprTopicSubscription> values = subscriptionBuilders.values().stream()
.map(b -> b.build()).collect(Collectors.toList());
return values.toArray(new DaprTopicSubscription[0]);
}
}

0 comments on commit 74070be

Please sign in to comment.