Hey,

In my project I had a need to import a service (a DataSource in my case), and I couldn't implement it using a ServiceComposite. So what I did instead was to create a ServiceComposite that implements ServiceImporter, and then create a class that implements ServiceImporter that is then used for the importedBy() in assembly, and which delegates to my ServiceComposite.

The funky thing that this accomplishes is that I can have a native service in Qi4j which is "always on", and which can handle importing external services which are only sometimes available. By implementing isActive() in my ServiceComposite I can check at the time of usage of the imported service if isActive() is true. If not the client can handle that as it wishes.

Here's my assembly code:
module.addServices( DataSourceService.class ).identifiedBy( "datasource" ).visibleIn( Visibility.application );
module.importServices( DataSource.class ).
      importedBy( ServiceInstanceImporter.class ).
      setMetaInfo( "datasource" ).
      identifiedBy( "streamflow" ).visibleIn( Visibility.application );

Currently I have hardcoded all datasource settings in the DataSourceService, *but* if I change this to be meta-info on the imported service I can use the import statement to do the actual settings for the individual datasources, which is then sent to the DataSourceService as ImportedServiceDescriptor!! In other words, the single DataSourceService can handle any number of datasources, generally.

I think this could be a very useful pattern for importing services from external sources in a way that there's an "always on" native service which you can configure *how* to import services, and then use the import-meta-info to describe *what* to import for the specific instances. And then also, there is no limitation on the external services, i.e. they don't have to be composites and they don't have to implement an interface or anything like that.

WDYT? Does this sound useful? If so, I can move the ServiceInstanceImporter to Qi4j from my project so that it's generally available.

/Rickard

_______________________________________________
qi4j-dev mailing list
[email protected]
http://lists.ops4j.org/mailman/listinfo/qi4j-dev

Reply via email to