我有2个iPOJO组件 .
1-提供“Hello”服务的提供程序包 . 以下是组件的实现:
package helloipojo;
import helloipojo.service.HelloService;
import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Invalidate;
import org.apache.felix.ipojo.annotations.Provides;
import org.apache.felix.ipojo.annotations.Validate;
@Component(name="my-factory")
@Provides
public class HelloServiceImpl implements HelloService{
@Override
public void sayHello() {
System.out.println("Hello iPojo!");
}
@Validate
public void start() throws Exception {
System.out.println("Hello, I am ipojo bundle start method");
}
@Invalidate
public void stop() throws Exception {
System.out.println("Bye Bye, I am ipojo bundle stop method");
}
}
2-使用HelloService对象的Consumer bundle作为以下内容:
package helloserviceconsumer;
import helloipojo.service.HelloService;
import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Invalidate;
import org.apache.felix.ipojo.annotations.Requires;
import org.apache.felix.ipojo.annotations.Validate;
@Component(name="my-consumer-factory")
public class HelloConsumer {
@Requires
HelloService helloObject;
@Validate
private void start() {
// Starting method
//...
helloObject.sayHello();
//...
}
@Invalidate
protected void stop() {
// Stopping method
if(helloObject!=null) { helloObject.sayHello(); }
else System.out.println("hello service GONE!");
}
}
在一个单独的Java应用程序中,我加载这两个bundle并在Apache Felix上启动它们,如下所示:
Bundle b = bundleContext1.installBundle("file:C:\\Users\\zaid.almahmoud\\Desktop\\plugins\\HelloService_1.0.0.201401222235.jar");
b.start();
Bundle c = bundleContext1.installBundle("file:C:\\Users\\zaid.almahmoud\\Desktop\\plugins\\HelloServiceConsumer_1.0.0.201401222257.jar");
c.start();
以上所有工作都很好 .
现在,我想动态地实例化这两个组件,并观察bundle使用者对bundle provider服务的消耗 . 我使用了Instance Declaration,如下所示:
DefaultInstanceDeclaration providerDeclaration = new DefaultInstanceDeclaration(b.getBundleContext(), "my-factory");
providerDeclaration.start();
DefaultInstanceDeclaration consumerDeclaration = new DefaultInstanceDeclaration(c.getBundleContext(), "my-consumer-factory");
consumerDeclaration.start();
运行应用程序时没有错误 . 但是,我看不到服务提供者和使用者的start()方法中存在的“Hello”消息 . 我看到绝对没有 . 这意味着组件未正确实例化 . 我哪里做错了?谢谢 .
Update
我发现我没有在我的包上应用iPOJO操作,所以我使用iPOJO Ant Task执行了这项操作,如下所示:
<project>
<target name="main">
<!-- Change the path to point on the iPOJO Ant task jar-->
<taskdef name="ipojo"
classname="org.apache.felix.ipojo.task.IPojoTask"
classpath="C:/Users/zaid.almahmoud/feasibility-codes/ipojo/ipojo-distribution-1.11.0/bundle/org.apache.felix.ipojo.ant-1.11.0.jar"/>
<ipojo
input="C:/Users/zaid.almahmoud/Desktop/plugins/HelloService_1.0.0.201401222235.jar"
output="C:/Users/zaid.almahmoud/Desktop/plugins/Manipulated_HelloService.jar"
/>
</target>
</project>
现在,我可以在我的应用程序中看到工厂有效 . 这是命令中的输出:
g! ipojo:factories
Factory my-factory (VALID)
Factory org.apache.felix.ipojo.arch.gogo.Arch (UNKNOWN) - Private
因此,工厂“我的工厂”不像以前那样可用 .
但是,我的实例不可用,其创建如下:
DefaultInstanceDeclaration providerDeclaration = new DefaultInstanceDeclaration(b.getBundleContext(), "my-factory");
providerDeclaration.start();
同样,这不会显示错误,但它不会在bundle的start()方法中显示预期的输出,并且在命令中,我可以看到:
g! ipojo:instance my-factory-0
Instance named 'my-factory-0' not found
g! ipojo:instances
Instance org.apache.felix.ipojo.arch.gogo.Arch-0 -> valid
你能帮帮忙吗?谢谢 .
1 回答
使用实例声明对我不起作用 . 虽然,我能够使用Factory Service实例化我的组件,如下所示:
我得到这个代码只能在我的java应用程序加载的 placing it into a bundle 之后才能工作 .