Listener Pattern Vs OSGI Service

Hello,

I remember reading in an article (I guess in OSGI.org), about using traditional design patterns that we use in other Java projects, may not be suitable for embedded environments. So that is why this question?

I have the following classes and listed are their responsibilities, I am trying to use the Listener pattern (notification paradigm), in order to log the commands/actions that are initiated in my application..this serves as a debug tool also. Here is the list

ActionHelper (addListeners, handleAction, fireEvent)

ActionThread (which has a handle to the ActionHelper, its run method invokes handleAction() on ActionHelper)

ActionListener (processEvent which logs the data)

ActionEvent (eventType, message)

MainClass, which gets called for different operations.

Basically, this class lazy intialiazes ActionHelper, invokes addListeners.

When each operation happens, the MainClass, creates a new ActionThread, sets the ActionHelper to the Thread and Start the thread.

The Thread's run method, now calls back to the handleAction, which knows to delegate the fireEvent.

Note: I am creating a new thread for each operation, so that I don't lock the current process time. Please advise on this.

Well, I hope this explains the situation.

I am thinking WHY NOT USE OSGI SERVICES?

In general, what would be the advantages in using OSGI services instead of ActionListeners, ActionEvents and If there are advantages, how would the pattern be. ?

[1560 byte] By [kumaranm] at [2007-9-26 12:38:45]
# 1

First I would like to describe traditional listeners model:

Traditional model requires that you should use addListener method to receive any events.

This is good and really works within OSGi environment ... but there are some problems. Suppose that the bundle which registered for receiving some events using addListener method but the programmer just forgot to un register that listener in the stop() method. In this situation your bundle is installed but since another service holds reference to your listener it will not be garbage collected and memory will not be freed. OSGi gateway is supposed to run nonstop for a long period of time. If you hat to install/uninstall .. etc many bundles where programmers forgot to remove their listeners the memory occupied by the gateway will increase linearly until it get full and jvm quits with OutOfMemoryError.

The white board approach requires that listeners are registered as services. When listener service is registered service will be notified for this and will send events to registered listeners. When bundle that registered that service, framework will automatically un register all services by this bundle and other bundles and services will be notified that these listeners will be unregistered - so everyone could throw references to that objects. Later since these objects will be not referenced by any other object they will be successfully garbage collected.

v.pavlov at 2007-7-2 11:58:23 > top of Java-index,Java Mobility Forums,Consumer and Commerce...