felix-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Beyer, Doug" <doug.be...@troppussoftware.com>
Subject RE: Basic osgi logging question
Date Sat, 09 Jul 2011 14:42:24 GMT
Thanks for the suggestions Caspar.

The attached code is not my real bundle code. It is stripped down demo code which illustrates
my problem. Normally I use a ServiceTracker. 

I solved my problem this morning. By reviewing the Felix log service source code, I found
that when you "unget" a LogReaderService reference (e.g. BundleContext.ungetService( ref )
), the LogReaderServiceFactory removes all its listeners. However, by experimentation, I found
that letting the service reference go out of scope doesn't remove the listeners. Of course,
this could be because garbage collection hasn't yet happened.

Admittedly, the Felix Log service documentation's sample code doesn't do an unget. I added
that to my code thinking I was "doing better". I don't remember any documentation about the
LogReaderService stating explicitly that ungetting it would remove its listeners. After re-reading
the osgi java docs for ServiceFactory.ungetService(), it's apparent to me now that ungetting
the LogReaderServcie would probably screw up any registered log listeners. But for someone
new to osgi and the log service specifically, I think it would help if that important piece
of information was added to the Felix Log Service documentation. That alone would have saved
me a lot of time.

Thanks again for volunteering your suggestions.

-----Original Message-----
From: Caspar MacRae [mailto:earcam@gmail.com] 
Sent: Friday, July 08, 2011 4:28 PM
To: users@felix.apache.org
Subject: Re: Basic osgi logging question

Hi Doug,

I don't have any answers - just a couple of suggestions that might help you track down where
the issue is.

(Already assuming that your breakpoints/stepping confirm the code is entering the two if(
ref != null ){ ... } blocks and that you can see all the log messages via shell commands or
log file.  Also checked for multiple
providers)

You should probably consider using a ServiceTracker instead of looking up the service only
once when your bundle is started.  This is more dynamic; so will cope with cases such as LogService/LogReaderService's
bundle being started after yours, and also stop/start/install of the LogService bundle, as
well as multiple LogService providers (unlikely in this case, but may be the issue affecting
you).

Also consider putting a LogReaderService.getLog() at the end of your start method to check
retrieval of your own messages.

cheers,

Caspar


On 8 Jul 2011 23:18, "Beyer, Doug" <doug.beyer@troppussoftware.com> wrote:
> Using the osgi compendim log service, I can't get my LogReaderService
listener to ever receive a log entry. Note that I'm using the Felix org.apache.felix.log bundle
for the log service.
>
> The super simple bundle code is shown at the bottom of this email.
>
> When debugging in eclipse, a breakpoint in my listener's
LogListener.logged() never gets hit nor does its println() get shown in the eclipse osgi console.
However, I do see all the println()'s in the activator's start() showing up in the eclipse
osgi console; and breakpoints in the start() method get hit as expected.
>
> If I run the bundle in Felix, again, all the start() method's 
> println()'s
get shown in the Felix console, but the println() in my listener's
LogListener.logged() doesn't get shown in the console. This leads me to believe that the logged()
function is, again, not getting called.
>
> Why is my listener's LogListener.logged() function not getting called?
>
> Thanks in advance.
>
>
> package com.troppus.bundle.logdriver3.impl;
>
> import org.osgi.framework.*;
> import org.osgi.service.log.*;
>
>
> public class Activator2 implements BundleActivator {
>
///////////////////////////////////////////////////////////////////////////////////////////
> // Constructors.
>
> public Activator2() {}
>
>
>
///////////////////////////////////////////////////////////////////////////////////////////
> // Public attributes.
>
> public void start( BundleContext context ) throws Exception { String 
> bundleName = (String)context.getBundle().getHeaders().get(
Constants.BUNDLE_NAME );
> System.out.println( bundleName + " starting..." ); m_BundleContext = 
> context;
>
> ServiceReference ref = m_BundleContext.getServiceReference(
LogReaderService.class.getName() );
> if (ref != null)
> {
> LogReaderService reader = 
> (LogReaderService)m_BundleContext.getService(
ref );
> reader.addLogListener( new LogWriter() );
>
> m_BundleContext.ungetService( ref );
> }
>
> ref = m_BundleContext.getServiceReference( LogService.class.getName() 
> ); if ( ref != null ) { LogService log = 
> (LogService)m_BundleContext.getService(ref);
>
> System.out.println( "logging a message..." ); log.log( 
> LogService.LOG_WARNING, "a log
message!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" );
> System.out.println( "logged a message." );
>
> m_BundleContext.ungetService( ref );
> }
>
> System.out.println( bundleName + " started." ); }
>
>
> public void stop( BundleContext context ) throws Exception
> {
> // The stop() method should clean up anything done in the start() method.
If you used threads
> // in the bundle, wait for all threads to stop before returning from
stop(). Also, if your registered
> // services use resources that must be cleaned up, it's prudent to
manually unregister those services
> // (i.e. ServiceRegistration.unregister() )before cleaning up the
resources so that noone could
> // attempt to use the services without those resources.
>
> String bundleName = (String)m_BundleContext.getBundle().getHeaders().get(
Constants.BUNDLE_NAME );
> System.out.println( bundleName + " stopping..." );
>
> m_BundleContext = null;
>
> System.out.println( bundleName + " stopped." );
> }
>
>
>
///////////////////////////////////////////////////////////////////////////////////////////
> // Private attributes.
>
> private BundleContext m_BundleContext = null;
> }
>
> class LogWriter implements LogListener
> {
> // Invoked by the log service implementation for each log entry
> public void logged(LogEntry entry)
> {
> System.out.println(entry.getMessage());
> }
> }
>
Mime
View raw message