运行OSGI Bundle项目

时间:2017-08-02 07:43:45

标签: eclipse osgi bundle osgi-bundle activator

我正在使用Eclipse IDE并尝试运行osgi bundle项目。我使用插件项目创建了2个包。

  1. CalculatorService(充当发布者)
  2. CalculatorClient(充当订阅者)
  3. 这是我的项目结构

    project structure

    当我尝试运行CalculatorService时,它执行正常并且服务处于活动状态。然后,CalculatorServiceACTIVE后,我会运行CalculatorClient。但它保持RESOLVED状态,而不是ACTIVE

    Service status

    尽管无法获得CalculatorClient Bundle ACTIVE,我尝试启动该服务并获得如下gogo异常。

    gogo exception

    由于此异常表示我start()捆绑的CalculatorClient方法存在错误,我已将其附在此处。

    package calculatorclient;
    
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    
    import org.osgi.framework.BundleActivator;
    import org.osgi.framework.BundleContext;
    import org.osgi.framework.ServiceReference;
    
    import calculatorservice.ICalculatorService;
    
    public class CalculatorClient implements BundleActivator {
    
        ServiceReference servRef;
        private static BundleContext context;
    
        static BundleContext getContext() {
            return context;
        }
    
        /*
         * (non-Javadoc)
         * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
         */
        public void start(BundleContext bundleContext) throws Exception {
            CalculatorClient.context = bundleContext;
            System.out.println("Start calculate client service");
            servRef = context.getServiceReference(ICalculatorService.class.getName());
            ICalculatorService calc = (ICalculatorService)context.getService(servRef);
    
            //Print
            System.out.println("Enter Operator");
            BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
            String oper = br1.readLine();
            BufferedReader br2 = new BufferedReader(new InputStreamReader(System.in));
            double no1 = Double.parseDouble(br2.readLine());
            BufferedReader br3 = new BufferedReader(new InputStreamReader(System.in));
            double no2 = Double.parseDouble(br3.readLine());
    
            System.out.print("Answer is ");
            calc.calculateService(oper, no1, no2);
    
        }
    
        /*
         * (non-Javadoc)
         * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
         */
        public void stop(BundleContext bundleContext) throws Exception {
            CalculatorClient.context = null;
            System.out.println("Good Bye !!!");
            context.ungetService(servRef);
        }
    
    }
    

    CalculatorService包的激活器类

    package calculatorservice;
    
    import org.osgi.framework.BundleActivator;
    import org.osgi.framework.BundleContext;
    import org.osgi.framework.ServiceRegistration;
    
    public class CalculatorActivator implements BundleActivator {
    
        ServiceRegistration servReg;
        private static BundleContext context;
    
        static BundleContext getContext() {
            return context;
        }
    
        /*
         * (non-Javadoc)
         * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
         */
        public void start(BundleContext bundleContext) throws Exception {
            CalculatorActivator.context = bundleContext;
            ICalculatorService calc = new CalculatorServiceImpl();
            servReg = context.registerService(ICalculatorService.class.getName(), calc, null);
        }
    
        /*
         * (non-Javadoc)
         * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
         */
        public void stop(BundleContext bundleContext) throws Exception {
            CalculatorActivator.context = null;
            servReg.unregister();
        }
    
    }
    

    CalculatorService MANIFEST.MF

    Manifest-Version: 1.0
    Bundle-ManifestVersion: 2
    Bundle-Name: CalculatorService
    Bundle-SymbolicName: CalculatorService
    Bundle-Version: 1.0.0.qualifier
    Bundle-Activator: calculatorservice.CalculatorActivator
    Bundle-RequiredExecutionEnvironment: JavaSE-1.8
    Import-Package: org.osgi.framework;version="1.3.0"
    Bundle-ActivationPolicy: lazy
    Export-Package: calculatorservice
    

    CalculatorClient MANIFEST.MF

    Manifest-Version: 1.0
    Bundle-ManifestVersion: 2
    Bundle-Name: CalculatorClient
    Bundle-SymbolicName: CalculatorClient
    Bundle-Version: 1.0.0.qualifier
    Bundle-Activator: calculatorclient.CalculatorClient
    Bundle-RequiredExecutionEnvironment: JavaSE-1.8
    Import-Package: calculatorservice, org.osgi.framework;version="1.3.0"
    Bundle-ActivationPolicy: lazy
    

    堆栈跟踪

    !STACK 0
    org.osgi.framework.BundleException: Could not resolve module: org.sonatype.m2e.mavenarchiver [1674]
      Unresolved requirement: Require-Bundle: org.eclipse.m2e.jdt; bundle-version="[1.0.0,2.0.0)"
        -> Bundle-SymbolicName: org.eclipse.m2e.jdt; bundle-version="1.6.2.20150902-0002"; singleton:="true"
           org.eclipse.m2e.jdt [1376]
             Unresolved requirement: Require-Bundle: org.eclipse.m2e.core; bundle-version="[1.6.0,1.7.0)"
               -> Bundle-SymbolicName: org.eclipse.m2e.core; bundle-version="1.6.2.20150902-0002"; singleton:="true"
                  org.eclipse.m2e.core [1371]
                    Unresolved requirement: Require-Bundle: org.eclipse.m2e.workspace.cli; bundle-version="0.1.0"
    
        at org.eclipse.osgi.container.Module.start(Module.java:434)
        at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.incStartLevel(ModuleContainer.java:1582)
        at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.incStartLevel(ModuleContainer.java:1562)
        at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.doContainerStartLevel(ModuleContainer.java:1533)
        at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.dispatchEvent(ModuleContainer.java:1476)
        at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.dispatchEvent(ModuleContainer.java:1)
        at org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java:230)
        at org.eclipse.osgi.framework.eventmgr.EventManager$EventThread.run(EventManager.java:340)
    

    有关我无法将此CalculatorClient捆绑包从RESOLVED状态转变为ACTIVE状态的任何建议,我们将非常感谢。

1 个答案:

答案 0 :(得分:0)

我对两个Activator类进行了一些小修改。

  • 我不是使用start()stop()方法中声明的bundle上下文实例,而是使用了start()stop()方法中的bundlecontext对象参数

  • @Overridestart()方法之前添加stop()注释。

  • 在实现发布者包中的接口的类中,在方法声明之前添加@Override注释。

  • 从Activator类中删除项目初始化期间创建的任何不必要的默认声明。

发布商的激活类

package calculatorservice;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;

public class CalculatorActivator implements BundleActivator {

    ServiceRegistration serviceRegistration;

    @Override
    public void start(BundleContext bundleContext) throws Exception {
        ICalculatorService iCalculatorService = new CalculatorServiceImpl();
        serviceRegistration = bundleContext.registerService(ICalculatorService.class.getName(), iCalculatorService, null);
    }

    @Override
    public void stop(BundleContext bundleContext) throws Exception {
        serviceRegistration.unregister();
    }

}

发布商的实施类

package calculatorservice;

public class CalculatorServiceImpl implements ICalculatorService{
    @Override
    public void calculateService(String operator, double no1, double no2){
        String PLUS = "+";
        String MINUS = "-";
        String MUL = "*";
        String DIV = "/";
        if(PLUS.equals(operator))
        {
            System.out.print((no1+no2));
        }
        else if (MINUS.equals(operator)){
            System.out.println((no1-no2));
        }
        else if (MUL.equals(operator)){
            System.out.print((no1*no2));
        }
        else if(DIV.equals(operator)){
            if(no2 == 0.0){
                System.out.print(" \"Divide by zeo error \" ");
            }
            else{
                System.out.print((no1/no2));
            }
        }
        else if (operator == ""){
            System.out.print(" \"Enter value for operator\" ");
        }
        else{
            System.out.print(" \"Enter one of the operators +_*/ \" ");
        }
    }
}

客户的激活类

package calculatorclient;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

import calculatorservice.ICalculatorService;

public class CalculatorClient implements BundleActivator {

    ServiceReference serviceReference;

    @Override
    public void start(BundleContext bundleContext) throws Exception {
        System.out.println("Start calculate client Service");
        serviceReference = bundleContext.getServiceReference(ICalculatorService.class.getName());
        ICalculatorService iCalculatorService = (ICalculatorService)bundleContext.getService(serviceReference);

        System.out.println("Enter operator");
        BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
        String operator = br1.readLine();

        System.out.println("Enter no1 = ");
        BufferedReader br2 = new BufferedReader(new InputStreamReader(System.in));
        double no1 = Double.parseDouble(br2.readLine());

        System.out.println("Enter no2 = ");
        BufferedReader br3 = new BufferedReader(new InputStreamReader(System.in));
        double no2 = Double.parseDouble(br3.readLine());

        iCalculatorService.calculateService(operator,no1,no2);
    }

    @Override
    public void stop(BundleContext bundleContext) throws Exception {
        System.out.println("Good Bye!!!");
        bundleContext.ungetService(serviceReference);       
    }

}

然后我分别启动了发布商MANIFEST.MF。在ACTIVE之后,我启动了客户MANIFEST.MF。一旦这两个都ACTIVE启动客户端服务,程序就可以正常运行。