You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@felix.apache.org by Jean-Baptiste Reich <je...@gmail.com> on 2011/01/04 11:53:44 UTC

On demand bundle

Hello,

I would like to have on demand bundles in felix.
So, is there a mean to load bundles in memory only when needed and to remove
them when they are not necessary anymore with another bundle that acts as a
Manager.
With that it would be possible to precisely control the memory used and to
have policies like:
 - every bundle that stays inactive for 5 minutes is removed
 - limit the total number of loaded bundles
 - download from a repository a bundle to serve a request and remove it
 ...

Maybe this is possible by managing the bundle state in OSGi ? But I don't
know exactly when the bundle is concretely loaded into memory in felix...

Regards

Re: On demand bundle

Posted by David Savage <da...@paremus.com>.
On Wed, Jan 5, 2011 at 10:13 AM, Jean-Baptiste Reich
<je...@gmail.com> wrote:
> Thanks for your answers.
>
> In my case I just want to manage a subset of bundles that expose a certain
> property at registration for example.

To clarify do you mean that the bundles will expose a service that
will publish a certain property?

Assuming you do then the ServiceFactory [1] approach I mentioned
earlier is one way to make this work, something like:

class MyTimedServiceFactory implements ServiceFactory {

   private long _counter;
   private BundleContext _ctx;

   MyTimedServiceFactory(BundleContext ctx) {
     _ctx = ctx;
   }

   public synchronized Object getService(Bundle bundle,
ServiceRegistration reg) {
      if ( _counter++ == 0 ) {
        cancelGCTimer();
      }
      return new MyServiceImpl();
   }

   public synchronized void ungetService(Bundle bundle,
ServiceRegistration reg, Object service) {
      if ( --_counter == 0 ) {
         startGCTimer();
      }
   }

   private void startGCTimer() {
     // start a thread or schedule a task etc to call _ctx.stop();
   }

   private void cancelGCTimer() {
     // interrupt gc thread if previously scheduled
   }
}

class MyActivator implements BundleActivator {
   public void start(BundleContext ctx) {
     Hashtable props = new Hashtable();
     props.put("key", "value");
     ctx.registerService(MyService.class.getName(), new
MyTimedServiceFactory(ctx), props);
   }

   public void stop(BundleContext ctx) {
   }
}

This registers a factory that will provide a service with a key=value
property, when the last client bundle ungets the service a gc thread
is kicked off that will stop the bundle.

There are several nasty complexities with the bundle stopping itself
so in reality it would probably be better to put the GC thread code in
your management agent bundle.

You might also be able to do something funky with ServiceHooks [2] as
Pierre mentioned but that'd be a bit harder to explain in email...

Regards,

Dave

[1] http://www.osgi.org/javadoc/r4v42/org/osgi/framework/ServiceFactory.html
[2] http://www.osgi.org/javadoc/r4v42/org/osgi/framework/hooks/service/package-summary.html

> They will be managed with an agent and
> every managed bundle must be access with this agent (to start and stop and
> refresh). So, just managing the state of these bundles will be enough for me
> as the RESOLVED state unload the bundle from memory and that's my
> requirement.
>
> Regards
>
> 2011/1/4 David Savage <da...@paremus.com>
>
>> On Tue, Jan 4, 2011 at 10:53 AM, Jean-Baptiste Reich
>> <je...@gmail.com> wrote:
>> > Hello,
>> >
>> > I would like to have on demand bundles in felix.
>> > So, is there a mean to load bundles in memory only when needed and to
>> remove
>> > them when they are not necessary anymore with another bundle that acts as
>> a
>> > Manager.
>> > With that it would be possible to precisely control the memory used and
>> to
>> > have policies like:
>> >  - every bundle that stays inactive for 5 minutes is removed
>>
>> This depends on how you define "active" if your bundles provide
>> services then it is possible to find out when the services are no
>> longer in use but you have to do this yourself by registering a
>> ServiceFactory instead of a Service - you then get a callback when
>> services are discarded by clients - you can then do the reference
>> counting and tidy up when there have been no services usages within a
>> specified timeout. Though bundles uninstalling themselves is a hard
>> thing to do so you'd probably want a management agent to do this on
>> your behalf.
>>
>> If the client is a pure library however then I think this is basically
>> impossible from the spec point of view as there is no way to find out
>> when all references to the classes have been discarded. I put together
>> some proof of concept code based on Richard Halls virtual bundles work
>> [1] that would achieve a similar feature for the use case of garbage
>> collecting bundles that provide marshalled classes - but I would guess
>> that this will remain a proof of concept for some time.
>>
>> >  - limit the total number of loaded bundles
>>
>> This would be pretty easy to do if you have a management agent
>> handling all the installs, but you'd have to make sure "there can be
>> only one" management agent as there is no way in the spec at the
>> moment to do a framework wide veto.
>>
>> >  - download from a repository a bundle to serve a request and remove it
>>
>> This sounds a lot like the sort of capabilities our product Nimble is
>> designed to achieve - if you want to take a look there's a free
>> download available and there's online docs here [2] [3].
>>
>> Hope that's of help.
>>
>> Regards,
>>
>> Dave
>>
>> [1] http://svn.apache.org/repos/asf/felix/sandbox/rickhall/vb/
>> [2] http://www.paremus.com/downloads/downloads_nimble.html
>> [3] https://docs.paremus.com//display/NIM20/Home
>> >  ...
>> >
>> > Maybe this is possible by managing the bundle state in OSGi ? But I don't
>> > know exactly when the bundle is concretely loaded into memory in felix...
>> >
>> > Regards
>> >
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
>> For additional commands, e-mail: users-help@felix.apache.org
>>
>>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org


Re: On demand bundle

Posted by Jean-Baptiste Reich <je...@gmail.com>.
Thanks for your answers.

In my case I just want to manage a subset of bundles that expose a certain
property at registration for example. They will be managed with an agent and
every managed bundle must be access with this agent (to start and stop and
refresh). So, just managing the state of these bundles will be enough for me
as the RESOLVED state unload the bundle from memory and that's my
requirement.

Regards

2011/1/4 David Savage <da...@paremus.com>

> On Tue, Jan 4, 2011 at 10:53 AM, Jean-Baptiste Reich
> <je...@gmail.com> wrote:
> > Hello,
> >
> > I would like to have on demand bundles in felix.
> > So, is there a mean to load bundles in memory only when needed and to
> remove
> > them when they are not necessary anymore with another bundle that acts as
> a
> > Manager.
> > With that it would be possible to precisely control the memory used and
> to
> > have policies like:
> >  - every bundle that stays inactive for 5 minutes is removed
>
> This depends on how you define "active" if your bundles provide
> services then it is possible to find out when the services are no
> longer in use but you have to do this yourself by registering a
> ServiceFactory instead of a Service - you then get a callback when
> services are discarded by clients - you can then do the reference
> counting and tidy up when there have been no services usages within a
> specified timeout. Though bundles uninstalling themselves is a hard
> thing to do so you'd probably want a management agent to do this on
> your behalf.
>
> If the client is a pure library however then I think this is basically
> impossible from the spec point of view as there is no way to find out
> when all references to the classes have been discarded. I put together
> some proof of concept code based on Richard Halls virtual bundles work
> [1] that would achieve a similar feature for the use case of garbage
> collecting bundles that provide marshalled classes - but I would guess
> that this will remain a proof of concept for some time.
>
> >  - limit the total number of loaded bundles
>
> This would be pretty easy to do if you have a management agent
> handling all the installs, but you'd have to make sure "there can be
> only one" management agent as there is no way in the spec at the
> moment to do a framework wide veto.
>
> >  - download from a repository a bundle to serve a request and remove it
>
> This sounds a lot like the sort of capabilities our product Nimble is
> designed to achieve - if you want to take a look there's a free
> download available and there's online docs here [2] [3].
>
> Hope that's of help.
>
> Regards,
>
> Dave
>
> [1] http://svn.apache.org/repos/asf/felix/sandbox/rickhall/vb/
> [2] http://www.paremus.com/downloads/downloads_nimble.html
> [3] https://docs.paremus.com//display/NIM20/Home
> >  ...
> >
> > Maybe this is possible by managing the bundle state in OSGi ? But I don't
> > know exactly when the bundle is concretely loaded into memory in felix...
> >
> > Regards
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
> For additional commands, e-mail: users-help@felix.apache.org
>
>

Re: On demand bundle

Posted by David Savage <da...@paremus.com>.
On Tue, Jan 4, 2011 at 10:53 AM, Jean-Baptiste Reich
<je...@gmail.com> wrote:
> Hello,
>
> I would like to have on demand bundles in felix.
> So, is there a mean to load bundles in memory only when needed and to remove
> them when they are not necessary anymore with another bundle that acts as a
> Manager.
> With that it would be possible to precisely control the memory used and to
> have policies like:
>  - every bundle that stays inactive for 5 minutes is removed

This depends on how you define "active" if your bundles provide
services then it is possible to find out when the services are no
longer in use but you have to do this yourself by registering a
ServiceFactory instead of a Service - you then get a callback when
services are discarded by clients - you can then do the reference
counting and tidy up when there have been no services usages within a
specified timeout. Though bundles uninstalling themselves is a hard
thing to do so you'd probably want a management agent to do this on
your behalf.

If the client is a pure library however then I think this is basically
impossible from the spec point of view as there is no way to find out
when all references to the classes have been discarded. I put together
some proof of concept code based on Richard Halls virtual bundles work
[1] that would achieve a similar feature for the use case of garbage
collecting bundles that provide marshalled classes - but I would guess
that this will remain a proof of concept for some time.

>  - limit the total number of loaded bundles

This would be pretty easy to do if you have a management agent
handling all the installs, but you'd have to make sure "there can be
only one" management agent as there is no way in the spec at the
moment to do a framework wide veto.

>  - download from a repository a bundle to serve a request and remove it

This sounds a lot like the sort of capabilities our product Nimble is
designed to achieve - if you want to take a look there's a free
download available and there's online docs here [2] [3].

Hope that's of help.

Regards,

Dave

[1] http://svn.apache.org/repos/asf/felix/sandbox/rickhall/vb/
[2] http://www.paremus.com/downloads/downloads_nimble.html
[3] https://docs.paremus.com//display/NIM20/Home
>  ...
>
> Maybe this is possible by managing the bundle state in OSGi ? But I don't
> know exactly when the bundle is concretely loaded into memory in felix...
>
> Regards
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org


Re: On demand bundle

Posted by Pierre Henry Perret <ph...@gmail.com>.
I think a service hook would do the trick.

A bundle is loaded when is state is RESOLVED (section 3.4.1 from r4.core
OSGi spec doc).
There is an interface call back for the bundles events BundleEvent that
allows this implementation.




2011/1/4 Jean-Baptiste Reich <je...@gmail.com>

> Hello,
>
> I would like to have on demand bundles in felix.
> So, is there a mean to load bundles in memory only when needed and to
> remove
> them when they are not necessary anymore with another bundle that acts as a
> Manager.
> With that it would be possible to precisely control the memory used and to
> have policies like:
>  - every bundle that stays inactive for 5 minutes is removed
>  - limit the total number of loaded bundles
>  - download from a repository a bundle to serve a request and remove it
>  ...
>
> Maybe this is possible by managing the bundle state in OSGi ? But I don't
> know exactly when the bundle is concretely loaded into memory in felix...
>
> Regards
>