Java Message Distribution / Subscription? -


I am creating a set of widgets in Java which decodes and displays messages received on serial interfaces.

The message type is defined by a unique identifier. Each widget is only interested in a particular identifier

How do I apply to the contextual widgets in a way to deliver messages correctly?

If it is for an app (i.e. for main and thread), then JMMS is overkill.

The basics of this are a simple queue (out of which very good in Java, the hand of blacking QYUE waving in his hand).

The serial port reads its data, formats some relevant message objects, and dumps it to the central message queue. It can be as simple as a blocking singleton.

Next, you will need a line listener / dispatcher.

This is a different thread that sits on the queue, waiting for the messages.

When a message is found, it sends the wait to "Widgets".

How does this know which widgets you are getting?

This can be a simple registration plan:

  string message type = "XYZ"; MyMessageListener listener = new MyMessageListener (); EventQueueFactory.registerListener (message type, listener);  

Then you can do something like this:

  Public Zero Register Listener (string type, message listener listener) {list & lt; MessageListener & gt; Listener = Registration Map.get (type); If (listeners == faucet) {listeneres = new arreelist & lt; Message Listener & gt; (); RegistrationMap.put (types, listeners); } Listeners.add (listener); } Public Shuttle Message (Message Message) {List & lt; Message Listener & gt; Listener = Registration Map.get (type); If (listeners! = Null) {for (message listener listener: listeners) {listener.send (msg); Apart from this, if you are using swings, its java suite has a full suite of listeners and you can not take advantage of it. 

That's his heart, you need enough rope to keep you in trouble.


Comments