Extracted a store for subscriptions
This commit is contained in:
@@ -16,7 +16,7 @@ public class SimpleMessaging implements IMessaging {
|
|||||||
//TODO is poorly performace on concurrent load, use at least ReadWriteLock
|
//TODO is poorly performace on concurrent load, use at least ReadWriteLock
|
||||||
|
|
||||||
private ReadWriteLock rwLock = new ReentrantReadWriteLock();
|
private ReadWriteLock rwLock = new ReentrantReadWriteLock();
|
||||||
private List<Subscription> subscriptions = new ArrayList<Subscription>();
|
private SubscriptionsStore subscriptions = new SubscriptionsStore();
|
||||||
|
|
||||||
private INotifier m_notifier;
|
private INotifier m_notifier;
|
||||||
|
|
||||||
@@ -31,10 +31,8 @@ public class SimpleMessaging implements IMessaging {
|
|||||||
|
|
||||||
rwLock.readLock().lock();
|
rwLock.readLock().lock();
|
||||||
try {
|
try {
|
||||||
for (Subscription sub : subscriptions) {
|
for (Subscription sub : subscriptions.matches(topic)) {
|
||||||
if (sub.match(topic)) {
|
m_notifier.notify(sub.clientId, topic, defQos, message);
|
||||||
m_notifier.notify(sub.clientId, topic, defQos, message);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
rwLock.readLock().unlock();
|
rwLock.readLock().unlock();
|
||||||
@@ -44,15 +42,11 @@ public class SimpleMessaging implements IMessaging {
|
|||||||
public void subscribe(String clientId, String topic, QOSType qos) {
|
public void subscribe(String clientId, String topic, QOSType qos) {
|
||||||
Subscription newSubscription = new Subscription(clientId, topic, qos);
|
Subscription newSubscription = new Subscription(clientId, topic, qos);
|
||||||
rwLock.writeLock().lock();
|
rwLock.writeLock().lock();
|
||||||
if (subscriptions.contains(newSubscription)) {
|
|
||||||
rwLock.writeLock().unlock();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
subscriptions.add(newSubscription);
|
subscriptions.add(newSubscription);
|
||||||
rwLock.writeLock().unlock();
|
rwLock.writeLock().unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected List<Subscription> getSubscriptions() {
|
protected SubscriptionsStore getSubscriptions() {
|
||||||
return subscriptions;
|
return subscriptions;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-15
@@ -1,12 +1,11 @@
|
|||||||
package org.dna.mqtt.moquette.messaging.spi.impl;
|
package org.dna.mqtt.moquette.messaging.spi.impl;
|
||||||
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.mockito.ArgumentCaptor;
|
|
||||||
import java.util.List;
|
|
||||||
import org.dna.mqtt.moquette.messaging.spi.INotifier;
|
import org.dna.mqtt.moquette.messaging.spi.INotifier;
|
||||||
import org.dna.mqtt.moquette.proto.messages.AbstractMessage.QOSType;
|
import org.dna.mqtt.moquette.proto.messages.AbstractMessage.QOSType;
|
||||||
import org.junit.Test;
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.mockito.ArgumentCaptor;
|
||||||
import static org.mockito.Mockito.*;
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -43,17 +42,7 @@ public class SimpleMessagingTest {
|
|||||||
|
|
||||||
//Verify
|
//Verify
|
||||||
Subscription subscription = new Subscription(FAKE_CLIENT_ID, FAKE_TOPIC, QOSType.MOST_ONE);
|
Subscription subscription = new Subscription(FAKE_CLIENT_ID, FAKE_TOPIC, QOSType.MOST_ONE);
|
||||||
assertEquals(1, countMatchingSubscriptions(messaging.getSubscriptions(), subscription));
|
assertEquals(1, messaging.getSubscriptions().size());
|
||||||
}
|
|
||||||
|
|
||||||
private int countMatchingSubscriptions(List<Subscription> l, Subscription matchingSub) {
|
|
||||||
int count = 0;
|
|
||||||
for (Subscription s : l) {
|
|
||||||
if (s.equals(matchingSub)) {
|
|
||||||
count++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return count;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
Reference in New Issue
Block a user