Hi Christian

all is packaged in the same bundle ?

It's better to have one bundle for common/API, one for service provider,
one for service "client".

You can take a look on  the current example:

https://github.com/apache/karaf/pull/484

I plan to merge and include in the distribution pretty soon.

Regards
JB

On 19/07/2018 16:25, Christian Eugster wrote:
> I have built a simple maven project for learning but I am stuck. I generated 
> a bundle that contains api and implementation and a DAO service. The dao 
> service (CarService) provides methods to create and select car entities 
> (simple entities with just a name field as id). I dropped the generated 
> bundle into karaf (4.2.1-SNAPSHOT) and it seems to run:
> 
> 8<----------
> 
> karaf@root()> bundle:services 198
> 
> jpa Bundle (198) provides:
> --------------------------
> [javax.persistence.EntityManagerFactory]
> [org.apache.aries.jpa.supplier.EmSupplier]
> [javax.persistence.EntityManager]
> [org.apache.aries.jpa.template.JpaTemplate]
> [org.osgi.service.cm.ManagedService]
> [org.osgi.service.jpa.EntityManagerFactoryBuilder]
> [aries.example.api.CarService]
> karaf@root()>
> 
> karaf@root()> service:list CarService
> [aries.example.api.CarService]
> ------------------------------
>  component.id = 12
>  component.name = aries.example.service.CarServiceImpl
>  JpaTemplate.target = (osgi.unit.name=aries)
>  service.bundleid = 198
>  service.id = 316
>  service.scope = bundle
> Provided by :
>  jpa Bundle (198)
> 
> karaf@root()>
> 
> 8<----------
> 
> To see, if the service is ok, i added a command bundle to create cars with 
> help of this service, that is tracked with a service tracker in the execute 
> method of the command:
> 
> package aries.example.command;
> 
> import org.apache.karaf.shell.api.action.Action;
> import org.apache.karaf.shell.api.action.Argument;
> import org.apache.karaf.shell.api.action.Command;
> import org.apache.karaf.shell.api.action.lifecycle.Service;
> import org.osgi.framework.FrameworkUtil;
> import org.osgi.framework.ServiceReference;
> import org.osgi.util.tracker.ServiceTracker;
> 
> import aries.example.api.Car;
> import aries.example.api.CarService;
> 
> @Command(scope = "aries", name = "create", description = "Create cars")
> @Service
> public class Create implements Action
> {
>       @Argument(name = "Name", description = "Argument to the command", 
> required = true, multiValued = false)
>       private String name;
> 
>       @Override
>       public Object execute() throws Exception
>       {
>               ServiceTracker<CarService, CarService> tracker = new 
> ServiceTracker<CarService, 
> CarService>(FrameworkUtil.getBundle(this.getClass()).getBundleContext(), 
> CarService.class, null);
>               tracker.open();
>               try
>               {
>                       ServiceReference<CarService> reference = 
> tracker.getServiceReference();
>                       CarService carService = tracker.getService(reference);
>                       if (carService == null)
>                       {
>                               System.out.println("Service not available.");
>                       }
>                       else
>                       {
>                               carService.createCar(name);
>                               Car car = carService.getCar(name);
>                               System.out.println(car.getName());
>                       }
>               }
>               finally
>               {
>                       tracker.close();
>               }
>               return null;
>       }
> }
> 
> The problem is, that the tracker does not return the service, or in the 
> actual example above, the service reference. I have just no idea what could 
> going wrong and I spent a lot of time with it. Any hints? Thanks!
> 

-- 
Jean-Baptiste Onofré
[email protected]
http://blog.nanthrax.net
Talend - http://www.talend.com

Reply via email to