Skip to content

Commit

Permalink
#328 Prepare broker
Browse files Browse the repository at this point in the history
  • Loading branch information
dvoraka committed Jul 9, 2019
1 parent d873132 commit 1896525
Showing 1 changed file with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package dvoraka.avservice.client.transport.test;

import dvoraka.avservice.common.data.replication.ReplicationMessage;
import dvoraka.avservice.common.listener.MessageListener;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;


public class DefaultSimpleBroker implements SimpleBroker<ReplicationMessage> {

private final Map<String, Set<MessageListener<ReplicationMessage>>> queueMap;


public DefaultSimpleBroker() {
queueMap = new ConcurrentHashMap<>();
}

@Override
public void send(String queueName, ReplicationMessage message) {

if (queueMap.get(queueName) != null) {
Set<MessageListener<ReplicationMessage>> listeners = queueMap.get(queueName);
listeners.forEach(listener -> listener.onMessage(message));
}
}

@Override
public void addMessageListener(MessageListener<ReplicationMessage> listener, String queueName) {

if (queueMap.get(queueName) == null) {
Set<MessageListener<ReplicationMessage>> listeners = new CopyOnWriteArraySet<>();
listeners.add(listener);
queueMap.put(queueName, listeners);
} else {
Set<MessageListener<ReplicationMessage>> listeners = queueMap.get(queueName);
listeners.add(listener);
}
}
}

0 comments on commit 1896525

Please sign in to comment.