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.String
id
static boolean
isInitialized
static java.util.Map<java.lang.String,java.util.Queue<Pmessage>>
messages
static java.util.logging.Logger
theLogger
-
Constructor Summary
Constructors Modifier Constructor Description protected
SimpleMessageSystem()
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 boolean
canSend(Pmessage message)
Test whether the system is ready to send to a given recipient.PmessageSystem
getPmessageSystem(java.lang.String senderId)
Factory method returns a working instance for the specified senderId.PmessageSystem
getPmessageSystem(Profile p)
Factory method returns a working instance for the specified configuration Profile.boolean
hasNext()
Return true if there is a Pmessage waiting to be received.static void
main(java.lang.String[] args)
Pmessage
next()
Return the next waiting Pmessage or void if there is none.void
printAgents()
Return all the agent names associated with this instance.void
send(java.lang.String sender, java.lang.String receiver, java.lang.String event)
Send a simple event from one message agent to another.void
send(Pmessage message)
Send a Pmessage to the destination specified in the Pmessage object.boolean
shutdownNow()
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:
shutdownNow
in interfacePmessageSystem
-
printAgents
public void printAgents()Description copied from interface:PmessageSystem
Return 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:
printAgents
in interfacePmessageSystem
-
getPmessageSystem
Description copied from interface:PmessageSystem
Factory method returns a working instance for the specified senderId.- Specified by:
getPmessageSystem
in 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:
send
in interfacePmessageSystem
-
canSend
Test whether the system is ready to send to a given recipient.- Specified by:
canSend
in 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:
send
in interfacePmessageSystem
- Parameters:
message
-- Throws:
MessageSystemException
-
hasNext
public boolean hasNext()Return true if there is a Pmessage waiting to be received.- Specified by:
hasNext
in interfacePmessageSystem
- Returns:
- boolean
-
next
Return the next waiting Pmessage or void if there is none.- Specified by:
next
in interfacePmessageSystem
- Returns:
- Pmessage
- Throws:
MessageSystemException
-
main
public static void main(java.lang.String[] args) throws MessageSystemException, java.lang.InterruptedException, ConfigurationError- Parameters:
args
-- Throws:
MessageSystemException
java.lang.InterruptedException
ConfigurationError
-
getPmessageSystem
Description copied from interface:PmessageSystemEE
Factory method returns a working instance for the specified configuration Profile.- Specified by:
getPmessageSystem
in interfacePmessageSystemEE
- Parameters:
p
- the Profile containing configuration information.- Returns:
- exemplar
-