Class SimpleMessageSystem
- All Implemented Interfaces:
PmessageSystem,PmessageSystemEE
@ThreadSafe public class SimpleMessageSystem extends java.lang.Object implements PmessageSystemEE
Warning - only demo0P uses this package. It is suitable for testing simple software on a single machine but inadequate for testing JMS software even on a single machine.
In an operational system, the sender and recipient would be separate process initialized with separate identities. In this simple, one process implementation, senders and receivers are separate threads in the same process. Consequently, this implementation requires a different SimpleMessageSystem object for each thread. The first object that is instantiated prepares class members shared by the objects.
Warning: this simple implementation does not protect data from loss if the software is shutdown while there are still messages in the queue. Furthermore, it saves all messages and never cleans up. Consequently, it is only suitable for short-term tests or demonstrations.
Warning: this simple implementation does not sign a message so that the recipient may verify it. The interface is agnostic about the message body which will be encrypted and/or signed as the case may require. However, a message without a body is also used as a protocol even and those should be signed and verified to protect the integrity of the system. Be sure to select a more complete implementation of PmessageSystem for production software.
-
Field Summary
Fields Modifier and Type Field Description java.lang.Stringidstatic booleanisInitializedstatic java.util.Map<java.lang.String,java.util.Queue<Pmessage>>messagesstatic java.util.logging.LoggertheLogger -
Constructor Summary
Constructors Modifier Constructor Description protectedSimpleMessageSystem()This constructor sets up the shared area and returns an instance that is only suitable for using the factory method.SimpleMessageSystem(java.lang.String senderId)The constructor sets up one sender. -
Method Summary
Modifier and Type Method Description booleancanSend(Pmessage message)Test whether the system is ready to send to a given recipient.PmessageSystemgetPmessageSystem(java.lang.String senderId)Factory method returns a working instance for the specified senderId.PmessageSystemgetPmessageSystem(Profile p)Factory method returns a working instance for the specified configuration Profile.booleanhasNext()Return true if there is a Pmessage waiting to be received.static voidmain(java.lang.String[] args)Pmessagenext()Return the next waiting Pmessage or void if there is none.voidprintAgents()Return all the agent names associated with this instance.voidsend(java.lang.String sender, java.lang.String receiver, java.lang.String event)Send a simple event from one message agent to another.voidsend(Pmessage message)Send a Pmessage to the destination specified in the Pmessage object.booleanshutdownNow()The SimpleMessageSystem has no mechanism to determine when it can shutdown so the condition shutdownNow() is always false.
-
Field Details
-
isInitialized
public static boolean isInitialized -
messages
-
id
public java.lang.String id -
theLogger
public static java.util.logging.Logger theLogger
-
-
Constructor Details
-
SimpleMessageSystem
protected SimpleMessageSystem()This constructor sets up the shared area and returns an instance that is only suitable for using the factory method. -
SimpleMessageSystem
public SimpleMessageSystem(java.lang.String senderId)The constructor sets up one sender. If this is the first object to be constructed, it will also set up the logfile and other shared resources. Note that the filepath is used for the first object but it is ignored for all the rest because the shared resources were previously established. This constructor is not compatible with the convention for program configuration. Instead, one should use one exemplar of the class to provide additional instances via the factory method.- Parameters:
senderId- The identification of the thread that will send/get from this service- Throws:
MessageSystemException
-
-
Method Details
-
shutdownNow
public boolean shutdownNow()The SimpleMessageSystem has no mechanism to determine when it can shutdown so the condition shutdownNow() is always false.- Specified by:
shutdownNowin interfacePmessageSystem
-
printAgents
public void printAgents()Description copied from interface:PmessageSystemReturn all the agent names associated with this instance. Usually, there is just one. However, in certain test procedures as well as in the subclass SimpleMessageSystem there may be several.- Specified by:
printAgentsin interfacePmessageSystem
-
getPmessageSystem
Description copied from interface:PmessageSystemFactory method returns a working instance for the specified senderId.- Specified by:
getPmessageSystemin interfacePmessageSystem- Parameters:
senderId- the Id of the entity that will use this instance of the message system.- Returns:
- exemplar
-
send
public void send(java.lang.String sender, java.lang.String receiver, java.lang.String event)Send a simple event from one message agent to another. Only the name of the event is sent.- Specified by:
sendin interfacePmessageSystem
-
canSend
Test whether the system is ready to send to a given recipient.- Specified by:
canSendin interfacePmessageSystem- Returns:
- true if ready to send
-
send
Send a Pmessage to the destination specified in the Pmessage object. The Pmessage cannot provide a body as an InputStream. That option is not implemented.- Specified by:
sendin interfacePmessageSystem- Parameters:
message-- Throws:
MessageSystemException
-
hasNext
public boolean hasNext()Return true if there is a Pmessage waiting to be received.- Specified by:
hasNextin interfacePmessageSystem- Returns:
- boolean
-
next
Return the next waiting Pmessage or void if there is none.- Specified by:
nextin interfacePmessageSystem- Returns:
- Pmessage
- Throws:
MessageSystemException
-
main
public static void main(java.lang.String[] args) throws MessageSystemException, java.lang.InterruptedException, ConfigurationError- Parameters:
args-- Throws:
MessageSystemExceptionjava.lang.InterruptedExceptionConfigurationError
-
getPmessageSystem
Description copied from interface:PmessageSystemEEFactory method returns a working instance for the specified configuration Profile.- Specified by:
getPmessageSystemin interfacePmessageSystemEE- Parameters:
p- the Profile containing configuration information.- Returns:
- exemplar
-