-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#343] DS light : makes the subscribe blocking like Redis
- Loading branch information
Showing
9 changed files
with
180 additions
and
99 deletions.
There are no files selected for viewing
This file contains 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
70 changes: 57 additions & 13 deletions
70
datashare-api/src/main/java/org/icij/datashare/com/memory/MemoryDataBus.java
This file contains 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 |
---|---|---|
@@ -1,38 +1,82 @@ | ||
package org.icij.datashare.com.memory; | ||
|
||
import org.icij.datashare.com.Channel; | ||
import org.icij.datashare.com.DataBus; | ||
import org.icij.datashare.com.Message; | ||
import org.icij.datashare.com.Publisher; | ||
import org.icij.datashare.com.*; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.Arrays; | ||
import java.util.LinkedHashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
import java.util.function.Consumer; | ||
|
||
import static java.util.Optional.ofNullable; | ||
import static org.icij.datashare.CollectionUtils.asSet; | ||
|
||
public class MemoryDataBus implements Publisher, DataBus { | ||
private final Map<Consumer<Message>, Set<Channel>> subscribers = new ConcurrentHashMap<>(); | ||
private final Logger logger = LoggerFactory.getLogger(getClass()); | ||
private final Map<Consumer<Message>, MessageListener> subscribers = new ConcurrentHashMap<>(); | ||
|
||
public void publish(Channel channel, Message message) { | ||
subscribers.entrySet().stream().filter(e -> e.getValue().contains(channel)).forEach(e -> e.getKey().accept(message)); | ||
public void publish(final Channel channel, final Message message) { | ||
subscribers.values().stream().filter(l -> l.hasSubscribedTo(channel)).forEach(l -> l.accept(message)); | ||
} | ||
|
||
@Override | ||
public void subscribe(Consumer<Message> subscriber, Channel... channels) { | ||
subscribers.put(subscriber, asSet(channels)); | ||
public int subscribe(final Consumer<Message> subscriber, final Channel... channels) throws InterruptedException { | ||
return subscribe(subscriber, () -> logger.debug("subscribed {} to {}", subscriber, Arrays.toString(channels)), channels); | ||
} | ||
|
||
@Override | ||
public void subscribe(Consumer<Message> subscriber, Runnable subscriptionCallback, Channel... channels) { | ||
subscribe(subscriber, channels); | ||
public int subscribe(final Consumer<Message> subscriber, final Runnable subscriptionCallback, final Channel... channels) throws InterruptedException { | ||
MessageListener listener = new MessageListener(subscriber, channels); | ||
subscribers.put(subscriber, listener); | ||
subscriptionCallback.run(); | ||
|
||
synchronized (listener.message) { | ||
while (!listener.isShutdown()) { | ||
listener.message.wait(); | ||
} | ||
} | ||
return listener.nbMessages.get(); | ||
} | ||
|
||
@Override | ||
public void unsubscribe(Consumer<Message> subscriber) { | ||
subscribers.remove(subscriber); | ||
ofNullable(subscribers.remove(subscriber)).ifPresent(l -> { | ||
l.accept(new ShutdownMessage()); | ||
logger.debug("unsubscribed {}", subscriber); | ||
}); | ||
} | ||
|
||
private static class MessageListener implements Consumer<Message> { | ||
private final Consumer<Message> subscriber; | ||
private final LinkedHashSet<Channel> channels; | ||
final AtomicReference<Message> message = new AtomicReference<>(); | ||
final AtomicInteger nbMessages = new AtomicInteger(0); | ||
|
||
public MessageListener(Consumer<Message> subscriber, Channel... channels) { | ||
this.subscriber = subscriber; | ||
this.channels = asSet(channels); | ||
} | ||
|
||
boolean hasSubscribedTo(Channel channel) { | ||
return channels.contains(channel); | ||
} | ||
|
||
@Override | ||
public void accept(Message message) { | ||
this.message.set(message); | ||
subscriber.accept(message); | ||
nbMessages.getAndIncrement(); | ||
synchronized (this.message) { | ||
this.message.notify(); | ||
} | ||
} | ||
|
||
boolean isShutdown() { | ||
return this.message.get() != null && this.message.get().type == Message.Type.SHUTDOWN; | ||
} | ||
} | ||
} |
This file contains 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
4 changes: 3 additions & 1 deletion
4
datashare-api/src/main/java/org/icij/datashare/text/nlp/DatashareListener.java
This file contains 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 |
---|---|---|
@@ -1,4 +1,6 @@ | ||
package org.icij.datashare.text.nlp; | ||
|
||
interface DatashareListener extends Runnable { | ||
import java.util.concurrent.Callable; | ||
|
||
interface DatashareListener extends Callable<Integer> { | ||
} |
This file contains 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 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 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 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
Oops, something went wrong.