tags:

views:

47

answers:

3

I am creating a set of widgets in Java that decodes and displays messages received at a serial interface.

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

How to I program the application in a way to distribute the messages correctly to the relevant widgets?

+1  A: 

sounds like a jms topic/subscription. why reinvent the wheel?

duffymo
Good to see you, %. ;o)
yawmark
Please help shed some light on this. I have come across some articles on JMS but I do not know if I should use it.
If this is for a single app, JMS is overkill
Will Hartung
great to see you too, steve.
duffymo
@Will, this is only for a single app. Can you advise what I should use?
overkill? not for anyone who knows jms. trade that off against rewriting a lot of code.
duffymo
JMS is heavyweight for such a simple need.
Eddie
I am using J2SE. How do I add JMS functionality into my app?
You need to add a JMS server of some type, and then implement server side and client side JMS code. For two items in the same JVM, JMS is overkill.
Eddie
A: 

One easy way to do this is to add each widget to a map by ID, and to provide each message to the widget by pulling it out of the map and calling some method on it. This means that each widget has to implement an interface that you can call to display the message. If the widgets are not in your control, then you can create a thin wrapper class (implementing an interface) and add this wrapper class -- with a widget -- to the map, one instance per ID.

Eddie
+1  A: 

If this is for a single app (i.e. a main and couple of threads), JMS is overkill.

The basics of this is a simple queue (of which Java has several good ones, BlockingQueue waving its hand in the back over there).

The serial port reads its data, formats a some relevant message object, and dumps it on a central Message Queue. This can be as simple as a BlockingQueue singleton.

Next, you'll need a queue listener/dispatcher.

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

When it gets a message it then dispatches it to the waiting "widgets".

How it "knows" what widgets get what is up to you.

It can be a simple registration scheme:

String messageType = "XYZ";
MyMessageListener listener = new MyMessageListener();
EventQueueFactory.registerListener(messageType, listener);

Then you can do something like:

public void registerListener(String type, MessageListener listener) {
    List<MessageListener> listeners = registrationMap.get(type);
    if (listeners == null) {
        listeneres = new ArrayList<MessageListener>();
        registrationMap.put(type, listeners);
    }
    listeners.add(listener);
}

public void dispatchMessage(Message msg) {
    List<MessageListener> listeners = registrationMap.get(type);
    if (listeners != null) {
        for(MessageListener listener : listeners) {
            listener.send(msg);
        }
    }
}

Also, if you're using Swing, it has a whole suite of Java Bean property listeners and what not that you could leverage as well.

That's the heart of it. That should give you enough rope to keep you in trouble.

Will Hartung
Will, thank you very much for the detailed reply.
This is a good approach for a simple app. If it grows beyond this, look into JMS (or EventAdmin service if you are OSGI based). Even the simple Observer/Observable would probably suffice here.
Robin