PDA

View Full Version : OsgiServiceLifecycleListener called twice?


volvin
May 17th, 2008, 05:29 PM
Hi,

I'm using listeners to capture the bind and unbind of services in a collection (a list). The configuration looks as follows:

<bean id="listener" class="cycle.Listener">
<property name="target" ref="importers" />
</bean>

<osgi:list id="importers" interface="SomeService">
<osgi:listener ref="listener" />
</osgi:list>

The "cycle.Listener" class implements the OsgiServiceLifecycleListener interface.

There are 2 services registered in the OSGi service registry with the 'SomeService' interface. With the above scenario, the bind/unbind method seems to be call twice for every service registered in the service registry.
I'm using Spring DM 1.0.1

Thx

Costin Leau
May 18th, 2008, 05:37 AM
It depends on your listener configuration and the usage of generics.
See this chapter in the docs (http://static.springframework.org/osgi/docs/current/reference/html/service-registry.html#service-registry:refs:listener-best-practices)
for some explanation on why some listener might be called twice.
If you're using generics then you might have hit a bug that is fixed in the nightly builds (will be available in the upcoming RC1):
http://jira.springframework.org/browse/OSGI-396

Additionally you could post your listener code if this doesn't solve your problem.

P.S. Please use [ c o d e ] tags next time you post code.

volvin
May 18th, 2008, 08:55 AM
Hi,

Thx for the feedback.

The listener code does not use generics in the bind/unbind methods:

public class Listener implements OsgiServiceLifecycleListener {
private List<ApplicationGUIModule> list = null;
public void setTarget(List<ApplicationGUIModule> guiModules){
list = guiModules;
}

public void bind(Object arg0, Map arg1) throws Exception {
System.out.println("################### BIND " + arg0.getClass() + "################"); }

public void unbind(Object arg0, Map arg1) throws Exception {
System.out.println("################### UNBIND " + arg0.getClass() + " ###############");
}
}

The configuration is as follows:

<bean id="listener" class="cycle.Listener">
<property name="target" ref="importers" />
</bean>

<osgi:list id="importers" interface="SomeService">
<osgi:listener ref="listener" />
</osgi:list>


There are 2 services registered in the service registry implementing "SomeService". The output of the listener method at startup of the bundle is:

################### BIND class $Proxy16################
################### BIND class $Proxy16################
################### BIND class $Proxy16################
################### BIND class $Proxy16################

All of them are on $Proxy16 ?!

When stopping one of the services, the output is

################### UNBIND class $Proxy16 ###############
################### UNBIND class $Proxy16 ###############

When I remove the injection of the target property:


<bean id="listener" class="cycle.Listener">
<!--<property name="target" ref="importers" /> -->
</bean>


In this code the bind method is only called two times - one time per service.
################### BIND class $Proxy16################
################### BIND class $Proxy16################

Am I missing something?
Thx

Costin Leau
May 18th, 2008, 10:57 AM
Instead of printing the class, try printing the actual object instance. In your case, you are probably getting 4 objects of the same type ($Proxy6) which are bounded to the list.
Try turning on logging to see what services are available and what methods are called on the listener. The category is org.springframework.osgi