-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
client/src/main/java/dvoraka/avservice/client/transport/test/DefaultSimpleBroker.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 |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |