Allow InterceptHandler to specify the messages they can intercept. This allows us to improve the performance of the broker interceptor, and will make log traces more clear.

- Add methods to return an unique identifier and the supported message types at the InterceptHandler interface.
- Include a default implementation of the new methods in the AbstractInterceptHandler. By default, all the interceptors will be able to handle any message (same as before).
- Comment verifyNoMoreInteractions() call from the BrokerInterceptorTest.testAddAndRemoveInterceptHandler(). The removeInterceptHandler() method of the broker interceptor
  polls the InterceptHandler, so an interaction will always exist after the call to onSubscribe().
This commit is contained in:
Luis Barrios
2017-01-10 17:06:13 +01:00
parent 1d75a25aa3
commit 46fcdc0894
15 changed files with 132 additions and 48 deletions
@@ -15,7 +15,13 @@
*/
package io.moquette.interception;
import io.moquette.interception.messages.*;
import io.moquette.interception.messages.InterceptAcknowledgedMessage;
import io.moquette.interception.messages.InterceptConnectMessage;
import io.moquette.interception.messages.InterceptConnectionLostMessage;
import io.moquette.interception.messages.InterceptDisconnectMessage;
import io.moquette.interception.messages.InterceptPublishMessage;
import io.moquette.interception.messages.InterceptSubscribeMessage;
import io.moquette.interception.messages.InterceptUnsubscribeMessage;
/**
* Basic abstract class usefull to avoid empty methods creation in subclasses.
@@ -23,7 +29,12 @@ import io.moquette.interception.messages.*;
* Created by andrea on 08/12/15.
*/
public abstract class AbstractInterceptHandler implements InterceptHandler {
@Override
public Class<?>[] getInterceptedMessageTypes() {
return InterceptHandler.ALL_MESSAGE_TYPES;
}
@Override
public void onConnect(InterceptConnectMessage msg) {}
@@ -18,6 +18,11 @@ public class HazelcastInterceptHandler extends AbstractInterceptHandler {
public HazelcastInterceptHandler(Server server){
this.hz = server.getHazelcastInstance();
}
@Override
public String getID() {
return HazelcastInterceptHandler.class.getName() + "@" + hz.getName();
}
@Override
public void onPublish(InterceptPublishMessage msg) {
@@ -15,7 +15,13 @@
*/
package io.moquette.interception;
import io.moquette.interception.messages.*;
import io.moquette.interception.messages.InterceptAcknowledgedMessage;
import io.moquette.interception.messages.InterceptConnectMessage;
import io.moquette.interception.messages.InterceptConnectionLostMessage;
import io.moquette.interception.messages.InterceptDisconnectMessage;
import io.moquette.interception.messages.InterceptPublishMessage;
import io.moquette.interception.messages.InterceptSubscribeMessage;
import io.moquette.interception.messages.InterceptUnsubscribeMessage;
import io.moquette.parser.proto.messages.AbstractMessage;
import io.moquette.spi.impl.subscriptions.Subscription;
@@ -32,7 +38,24 @@ import io.moquette.spi.impl.subscriptions.Subscription;
* @author Wagner Macedo
*/
public interface InterceptHandler {
public static final Class<?> ALL_MESSAGE_TYPES[] = { InterceptConnectMessage.class,
InterceptDisconnectMessage.class, InterceptConnectionLostMessage.class, InterceptPublishMessage.class,
InterceptSubscribeMessage.class, InterceptUnsubscribeMessage.class, InterceptAcknowledgedMessage.class };
/**
* Returns the identifier of this intercept handler.
* @return
*/
String getID();
/**
* Returns the InterceptMessage subtypes that this handler can process. If the result is null or equal to ALL_MESSAGE_TYPES,
* all the message types will be processed.
* @return
*/
Class<?>[] getInterceptedMessageTypes();
void onConnect(InterceptConnectMessage msg);
void onDisconnect(InterceptDisconnectMessage msg);
@@ -49,7 +49,7 @@ public interface Interceptor {
void notifyMessageAcknowledged(InterceptAcknowledgedMessage msg);
boolean addInterceptHandler(InterceptHandler interceptHandler);
void addInterceptHandler(InterceptHandler interceptHandler);
boolean removeInterceptHandler(InterceptHandler interceptHandler);
void removeInterceptHandler(InterceptHandler interceptHandler);
}
@@ -20,7 +20,7 @@ import io.moquette.parser.proto.messages.AbstractMessage;
/**
* @author Wagner Macedo
*/
public abstract class InterceptAbstractMessage {
public abstract class InterceptAbstractMessage implements InterceptMessage {
private final AbstractMessage msg;
InterceptAbstractMessage(AbstractMessage msg) {
@@ -2,7 +2,7 @@ package io.moquette.interception.messages;
import static io.moquette.spi.IMessagesStore.StoredMessage;
public class InterceptAcknowledgedMessage {
public class InterceptAcknowledgedMessage implements InterceptMessage {
final private StoredMessage msg;
private final String username;
private final String topic;
@@ -3,7 +3,7 @@ package io.moquette.interception.messages;
/**
* @author Wagner Macedo
*/
public class InterceptConnectionLostMessage {
public class InterceptConnectionLostMessage implements InterceptMessage {
private final String clientID;
private final String username;
@@ -3,7 +3,7 @@ package io.moquette.interception.messages;
/**
* @author Wagner Macedo
*/
public class InterceptDisconnectMessage {
public class InterceptDisconnectMessage implements InterceptMessage {
private final String clientID;
private final String username;
@@ -0,0 +1,10 @@
package io.moquette.interception.messages;
/**
* An interface that sets the root of the interceptor messages type hierarchy.
* @author lbarrios
*
*/
public interface InterceptMessage {
}
@@ -6,7 +6,7 @@ import io.moquette.spi.impl.subscriptions.Subscription;
/**
* @author Wagner Macedo
*/
public class InterceptSubscribeMessage {
public class InterceptSubscribeMessage implements InterceptMessage {
private final Subscription subscription;
private final String username;
@@ -3,7 +3,7 @@ package io.moquette.interception.messages;
/**
* @author Wagner Macedo
*/
public class InterceptUnsubscribeMessage {
public class InterceptUnsubscribeMessage implements InterceptMessage {
private final String topicFilter;
private final String clientID;
private final String username;