messaging/broker : update handlers
This commit is contained in:
parent
e407698e84
commit
c5ea09d09d
@ -1,8 +1,12 @@
|
||||
package messagebroker;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
|
||||
public abstract class BaseMessageHandler implements MessageHandler {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(BaseMessageHandler.class);
|
||||
|
||||
private MessageHandler nextHandler;
|
||||
private final MessageHandlerContext context;
|
||||
@ -14,9 +18,11 @@ public abstract class BaseMessageHandler implements MessageHandler {
|
||||
|
||||
public void handleMessage(String message) throws UnknownMessageFormatException {
|
||||
if (nextHandler != null) {
|
||||
LOGGER.debug("Trying next handler in chain: {}", nextHandler);
|
||||
nextHandler.handleMessage(message);
|
||||
} else {
|
||||
String msg = "Unable to handle message " + message;
|
||||
LOGGER.warn(msg);
|
||||
throw new UnknownMessageFormatException(msg);
|
||||
}
|
||||
}
|
||||
@ -24,6 +30,11 @@ public abstract class BaseMessageHandler implements MessageHandler {
|
||||
@Override
|
||||
public void setNextHandler(MessageHandler nextHandler) {
|
||||
this.nextHandler = nextHandler;
|
||||
LOGGER.debug("Next handler of {} set to {}", getClass().getCanonicalName(), nextHandler.getClass().getCanonicalName());
|
||||
}
|
||||
|
||||
public MessageHandler getNextHandler() {
|
||||
return nextHandler;
|
||||
}
|
||||
|
||||
protected MessageHandlerContext getContext() {
|
||||
|
@ -1,12 +1,13 @@
|
||||
package messagebroker;
|
||||
|
||||
import messagebroker.handlers.AddHandlerMessageHandler;
|
||||
import messagebroker.handlers.ListHandlersMessageHandler;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
|
||||
@ -44,6 +45,10 @@ public class MessageBroker {
|
||||
messageHandlerFactory.addHandler(handlerName);
|
||||
}
|
||||
|
||||
public MessageHandler getHandler() {
|
||||
return messageHandlerFactory.getHandler();
|
||||
}
|
||||
|
||||
private class ClientHandler implements Runnable {
|
||||
private final Socket socket;
|
||||
|
||||
@ -82,7 +87,8 @@ public class MessageBroker {
|
||||
|
||||
int port = Integer.parseInt(args[0]);
|
||||
MessageBroker broker = new MessageBroker(port);
|
||||
broker.addHandler("messagebroker.handlers.AddHandler");
|
||||
broker.addHandler(AddHandlerMessageHandler.class.getCanonicalName());
|
||||
broker.addHandler(ListHandlersMessageHandler.class.getCanonicalName());
|
||||
broker.run();
|
||||
}
|
||||
}
|
||||
|
@ -2,5 +2,6 @@ package messagebroker;
|
||||
|
||||
public interface MessageHandler {
|
||||
void handleMessage(String message) throws UnknownMessageFormatException;
|
||||
MessageHandler getNextHandler();
|
||||
void setNextHandler(MessageHandler nextHandler);
|
||||
}
|
||||
|
@ -3,9 +3,6 @@ package messagebroker;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.*;
|
||||
|
||||
class MessageHandlerFactory {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(MessageHandlerFactory.class);
|
||||
|
||||
@ -23,7 +20,7 @@ class MessageHandlerFactory {
|
||||
Class<? extends MessageHandler> handlerClass = Class.forName(handlerName)
|
||||
.asSubclass(MessageHandler.class);
|
||||
|
||||
LOGGER.debug("Class {} found for handler name {}", handlerClass, handlerName);
|
||||
LOGGER.debug("Class {} found for handler name {}", handlerClass.getCanonicalName(), handlerName);
|
||||
MessageHandler next = newMessageHandler(handlerClass);
|
||||
if (firstHandler == null) {
|
||||
firstHandler = next;
|
||||
|
@ -33,6 +33,7 @@ public class AddHandlerMessageHandler extends BaseMessageHandler {
|
||||
sendResponse(msg);
|
||||
}
|
||||
} else {
|
||||
LOGGER.debug("Pattern \"{}\" does not match. Cannot handle message {}", msgPattern, message);
|
||||
super.handleMessage(message);
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,6 @@ import messagebroker.UnknownMessageFormatException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class EchoMessageHandler extends BaseMessageHandler {
|
||||
@ -25,6 +24,7 @@ public class EchoMessageHandler extends BaseMessageHandler {
|
||||
LOGGER.info("Parsed message {}", parsed);
|
||||
sendResponse(parsed);
|
||||
} else {
|
||||
LOGGER.debug("Pattern \"{}\" does not match. Cannot handle message {}", PATTERN, message);
|
||||
super.handleMessage(message);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,32 @@
|
||||
package messagebroker.handlers;
|
||||
|
||||
import messagebroker.BaseMessageHandler;
|
||||
import messagebroker.MessageHandlerContext;
|
||||
import messagebroker.UnknownMessageFormatException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class ListHandlersMessageHandler extends BaseMessageHandler {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(EchoMessageHandler.class);
|
||||
private static final String PATTERN = "CONTROL.LIST_HANDLERS";
|
||||
|
||||
public ListHandlersMessageHandler(MessageHandlerContext context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleMessage(String message) throws UnknownMessageFormatException {
|
||||
if (message.equals(PATTERN)) {
|
||||
LOGGER.info("Received message {}", message);
|
||||
var response = new StringBuilder();
|
||||
for (var nextHandler = getContext().getBroker().getHandler(); nextHandler != null; nextHandler = nextHandler.getNextHandler()){
|
||||
response.append(nextHandler.getClass().getCanonicalName())
|
||||
.append("\n");
|
||||
}
|
||||
sendResponse(response.toString());
|
||||
} else {
|
||||
LOGGER.debug("Pattern \"{}\" does not match. Cannot handle message {}", PATTERN, message);
|
||||
super.handleMessage(message);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
package messagebroker.handlers;
|
||||
|
||||
import messagebroker.BaseMessageHandler;
|
||||
import messagebroker.MessageHandlerContext;
|
||||
import messagebroker.UnknownMessageFormatException;
|
||||
|
||||
public class NewHandler extends BaseMessageHandler {
|
||||
private final MessageHandlerContext context;
|
||||
|
||||
public NewHandler(MessageHandlerContext context) {
|
||||
super(context);
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleMessage(String message) throws UnknownMessageFormatException {
|
||||
super.handleMessage(message);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user