我正在開發Eclipse IDE,並試圖運行osgi捆綁項目。我已經使用插件項目創建了2個捆綁包。運行OSGI捆綁項目
- CalculatorService的(充當發佈)
- CalculatorClient(充當用戶)
這是我的項目結構
當我嘗試運行CalculatorService
它執行得很好,服務處於活動狀態。然後一旦CalculatorService
是ACTIVE
,我運行CalculatorClient
。但它保持在RESOLVED
狀態,而不是ACTIVE
。
在-儘管不能夠得到CalculatorClient捆綁ACTIVE
的,我試圖啓動該服務,並得到了GOGO例外如下。
由於此異常說有什麼erraneous與我的CalculatorClient
束start()
方法,我也附後它。
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
狀態將得到高度讚賞任何建議。
如果您使用的是Equinox,請查看OSGi容器的配置文件夾。如果沒有另行指定,Equinox會將錯誤消息放入配置文件夾中的一些唯一命名的文件中。由於您無法知道捆綁包的啓動順序,因此您不應該在Activator的start方法中使用context.getService(),因爲其他捆綁包可能尚未啓動。 –
@BalazsZsoldos。在我的項目中,我最初通過右鍵單擊該包的MANIFEST.MF然後選擇Run as - > OSGI Framework來啓動CalculatorService包。並且一旦'CalculatorService捆綁包'是'ACTIVE',我在'CalculatorClient'捆綁包的'MANIFEST.MF'上做同樣的事情。因此,在這種情況下,不管是否使用'context.getService(),我都會按照相同的順序單獨啓動它們。 –
那麼,在你的啓動方法中有一個異常,但我不知道它的棧跟蹤在哪裏寫的。因此,我只能提出一個醜陋的解決方案,你應該用try-catch包裝你的start方法的entiry體,然後在catch中調用e.printstacktrace。你至少會看到真正的例外。 –