dev@glassfish.java.net

[Glassfish CDI/OSGi Integration]About Event Integration between CDI and OSGi

From: Tang Yong <tangyong_at_cn.fujitsu.com>
Date: Tue, 07 Aug 2012 18:41:52 +0900

Hi Sahoo, Siver,

Now, I have finished the Event Integration between CDI and OSGi
basiclly. I defined some cdi/osgi related events which can be used on
@Observes methods by user:

$B-!(B CDIOSGiServiceEvents.CDIOSGiServiceRegistered : When a user registered
a osgi service using @Publish, other users can get the registered
service by observing the event.

$B-"(B CDIOSGiServiceEvents.CDIOSGiServiceUnRegistered : When a osgi service
was unregistered, user can do some by observing the event.

$B-#(B @CDIOSGiEventFilter : User can be interested in some services by using
the qualifier.

The following is a example:

1 A user firstly registered a osgi service

@Publish({
           @Property(name="country", value="CN")
        })
public class SimpleStockQuoteServiceImpl implements StockQuoteService{
...

2 Then, the user defined a servlet in order to consume
"SimpleStockQuoteServiceImpl" service.

@WebServlet(urlPatterns = "/list")
public class StockQuoteServlet extends HttpServlet {

@Inject @ServiceFilter("(country=CN)") Service<StockQuoteService> sqses;
...

At the same time, the user defined two @Observes methods in order to
observe whether having new osgi services or having osgi services
unregistered.

public void bindService
(@Observes @CDIOSGiEventFilter(StockQuoteService.class)
           CDIOSGiServiceEvents.CDIOSGiServiceRegistered event)
{
   System.out.println("bind : " + event.getServiceClassNames());
   sqses.add(event.getService(StockQuoteService.class));
}

public void unbindService
(@Observes @CDIOSGiEventFilter(StockQuoteService.class)
           CDIOSGiServiceEvents.CDIOSGiServiceUnRegistered event)
{
   System.out.println("bind : " + event.getServiceClassNames());
   sqses.remove(event.getService(StockQuoteService.class));
}

3 The user deployed wab containing the serlvet.
At the moment, when the user accessed
"http://localhost:8080/stockquote/list", "SimpleStockQuoteServiceImpl"
service can be consumed normally.

4 Then, the user registered the other osgi service
@Publish({
           @Property(name="country", value="CN")
        })
public class OtherStockQuoteServiceImpl implements StockQuoteService{
...

5 When the user accessed "http://localhost:8080/stockquote/list" again,
not only "SimpleStockQuoteServiceImpl" but also
"OtherStockQuoteServiceImpl" can be all consumed successfully.

6 If the user undeployed the jar containing
"OtherStockQuoteServiceImpl", when the user accessed
"http://localhost:8080/stockquote/list" again, only
"SimpleStockQuoteServiceImpl" can be consumed.

DEMO/Sample:
see
https://github.com/tangyong/gf-cdi-osgi-integration/tree/master/samples/%5BRFP146%5DCDI021

[ToDo]
1) Resolve "[GLASSFISH-17155] - CDI Events don't work when fired by a
OSGi ServiceListener"

2) Improve the current prototype

Thanks.
--Tang