2017-08-02 107 views
0

我正在開發Eclipse IDE,並試圖運行osgi捆綁項目。我已經使用插件項目創建了2個捆綁包。運行OSGI捆綁項目

  1. CalculatorService的(充當發佈)
  2. CalculatorClient(充當用戶)

這是我的項目結構

project structure

當我嘗試運行CalculatorService它執行得很好,服務處於活動狀態。然後一旦CalculatorServiceACTIVE,我運行CalculatorClient。但它保持在RESOLVED狀態,而不是ACTIVE

Service status

在-儘管不能夠得到CalculatorClient捆綁ACTIVE的,我試圖啓動該服務,並得到了GOGO例外如下。

gogo exception

由於此異常說有什麼erraneous與我的CalculatorClientstart()方法,我也附後它。

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

012的
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狀態將得到高度讚賞任何建議。

+0

如果您使用的是Equinox,請查看OSGi容器的配置文件夾。如果沒有另行指定,Equinox會將錯誤消息放入配置文件夾中的一些唯一命名的文件中。由於您無法知道捆綁包的啓動順序,因此您不應該在Activator的start方法中使用context.getService(),因爲其他捆綁包可能尚未啓動。 –

+0

@BalazsZsoldos。在我的項目中,我最初通過右鍵單擊該包的MANIFEST.MF然後選擇Run as - > OSGI Framework來啓動CalculatorService包。並且一旦'CalculatorService捆綁包'是'ACTIVE',我在'CalculatorClient'捆綁包的'MANIFEST.MF'上做同樣的事情。因此,在這種情況下,不管是否使用'context.getService(),我都會按照相同的順序單獨啓動它們。 –

+0

那麼,在你的啓動方法中有一個異常,但我不知道它的棧跟蹤在哪裏寫的。因此,我只能提出一個醜陋的解決方案,你應該用try-catch包裝你的start方法的entiry體,然後在catch中調用e.printstacktrace。你至少會看到真正的例外。 –

回答

0

我對兩個Activator類做了一些小的修改。

  • 而不是使用聲明出的start()stop()方法捆上下文實例,我以前的BundleContext的start()stop()方法中對象參數。

  • start()stop()方法之前添加@Override註釋。

  • 在實現發佈者包內的接口的類的方法聲明之前添加一個@Override註釋。

  • 從Activator類中刪除項目初始化期間創建的任何不必要的默認聲明。

發行者的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啓動客戶端服務,程序將正常運行。