Skip to content
Permalink
Browse files

New Message Handling Interface

  • Loading branch information...
Ghost-Programmer committed Apr 13, 2019
1 parent 0acafc0 commit 47fe181df19049990e8e8e836aa1fff368dbfbc1
@@ -0,0 +1,139 @@
package name.mymiller.extensions.io.message;

import java.io.Serializable;
import java.util.Objects;
import java.util.UUID;

/**
* Message Container sent between systems for the exchange of data. UUID makes
* them unique, message type determines who is listening for the message, and an
* Object containing the data.
*
* @author jmiller
*
*/
public class Message implements Comparable<Message>, Cloneable, Serializable {
/**
* Generated Serial Version ID
*/
public static final long serialVersionUID = -85962065619107195L;

/**
* Message type this container is carrying.
*/
private final String messageType;

/**
* Unique ID for each message
*/
private final UUID uuid;

/**
* Data contained in this message
*/
private final Object data;

/**
* Copy Constructor to create new copy, with a new UUID.
*
* @param copy
*/
public Message(Message copy) {
super();
this.messageType = copy.getMessageType();
this.uuid = UUID.randomUUID();
this.data = copy.getData();
}

/**
* Constructor to generate a new message. UUID is generated automatically.
*
* @param messageType Type of message in this container.
* @param data Data of the for the message.
*/
public Message(String messageType, Object data) {
super();
this.messageType = messageType;
this.uuid = UUID.randomUUID();
this.data = data;
}

/**
* Private Constructor for cloning.
*
* @param messageType Type of message
* @param data Data of message
* @param uuid UUID of this message
*/
private Message(String messageType, Object data, UUID uuid) {
super();
this.messageType = messageType;
this.uuid = uuid;
this.data = data;
}

@Override
protected Object clone() throws CloneNotSupportedException {
return new Message(this.getMessageType(), this.getData(), this.getUuid());
}

@Override
public int compareTo(Message o) {
if (this.uuid.equals(o.uuid)) {
return 0;
}

return this.messageType.compareTo(o.messageType);
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof Message)) {
return false;
}
final Message other = (Message) obj;
return Objects.equals(this.data, other.data) && (this.messageType == other.messageType)
&& Objects.equals(this.uuid, other.uuid);
}

/**
*
* @return Data this message contains
*/
public final Object getData() {
return this.data;
}

/**
*
* @return MessageType this container has.
*/
public final String getMessageType() {
return this.messageType;
}

/**
*
* @return UUID of this message.
*/
public final UUID getUuid() {
return this.uuid;
}

@Override
public int hashCode() {
return Objects.hash(this.data, this.messageType, this.uuid);
}

@Override
public String toString() {
return "Message [messageType=" + this.messageType + ", uuid=" + this.uuid + ", data=" + this.data + "]";
}

}
@@ -0,0 +1,118 @@
package name.mymiller.extensions.io.message;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

import name.mymiller.extensions.lang.singleton.SingletonInterface;

/**
* Receives and distributes messages between listeners.
*
* @author jmiller
*
*/
public class MessageHandler implements SingletonInterface<MessageHandler> {
/**
* Job Manager Global Instance
*/
private static MessageHandler global_instance = null;

/**
* @return Global Instance of the Job Manager
*/
public static MessageHandler getInstance() {
if (MessageHandler.global_instance == null) {
MessageHandler.global_instance = new MessageHandler();
}
return MessageHandler.global_instance;
}

/**
* Map containing the listeners by type
*/
private final Map<String, List<MessageListenerInterface>> listenerMap;

/**
* Constructor to create global instance
*/
protected MessageHandler() {
this.listenerMap = new ConcurrentHashMap<>();
}

/**
* Add a listener for the specific message type.
*
* @param type Message Type to listen
* @param listener Listener object to receive the message
* @return Boolean indicating success of adding listener.
*/
public boolean addListener(String type, MessageListenerInterface listener) {
this.addMessageType(type);

final List<MessageListenerInterface> list = this.listenerMap.get(type);

return list.add(listener);
}

public void addMessageType(String messageType) {
final List<MessageListenerInterface> list = this.listenerMap.get(messageType);

if (list == null) {
this.listenerMap.put(messageType, new CopyOnWriteArrayList<>());
}
}

public void addMessageType(String[] messageTypes) {
Arrays.stream(messageTypes).forEach(type -> {
this.addMessageType(type);
});
}

/**
* Determine if a Listener is registered for a Message Type
*
* @param type Message Type to check for existing listener.
* @param listener Listener to see if is registered
* @return boolean indicating if registered
*/
public boolean isListening(String type, MessageListenerInterface listener) {
final List<MessageListenerInterface> list = this.listenerMap.get(type);

if (list == null) {
return false;
}

return list.contains(listener);
}

/**
* Remove the listener from message type
*
* @param type Type to remove the listener.
* @param listener Listener to remove.
* @return boolean indicating success.
*/
public boolean removeListener(String type, MessageListenerInterface listener) {
final List<MessageListenerInterface> list = this.listenerMap.get(type);

if (list == null) {
return false;
}

return list.remove(listener);
}

public boolean sendMessage(Message message) {
final List<MessageListenerInterface> list = this.listenerMap.get(message.getMessageType());

if (list == null) {
return false;
}
list.forEach(listener -> listener.receiveMessage(message));

return true;
}
}
@@ -0,0 +1,20 @@
/**
*
*/
package name.mymiller.extensions.io.message;

/**
* Interface to receive messages.
*
* @author jmiller
*
*/
public interface MessageListenerInterface {

/**
* Listener interface to receive messages
*
* @param message Message to receive.
*/
public void receiveMessage(final Message message);
}

0 comments on commit 47fe181

Please sign in to comment.
You can’t perform that action at this time.