2

我有 2 个 iPOJO 组件。

1- 提供“Hello”服务的 Provider 包。下面是组件的实现:

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 对象的消费者捆绑包如下:

 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 应用程序中,我加载这两个包并在 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();

以上所有工作正常。

现在,我想动态地实例化这两个组件,并观察捆绑消费者对捆绑提供者服务的消费情况。我使用了实例声明,如下:

DefaultInstanceDeclaration providerDeclaration = new DefaultInstanceDeclaration(b.getBundleContext(), "my-factory");
                            providerDeclaration.start();

DefaultInstanceDeclaration consumerDeclaration = new DefaultInstanceDeclaration(c.getBundleContext(), "my-consumer-factory");
                            consumerDeclaration.start();

运行应用程序时没有错误。但是,我看不到服务提供者和消费者的 start() 方法中存在的“Hello”消息。我什么都看不到。这意味着组件没有正确实例化。我哪里做错了?谢谢。

更新

我发现我没有在我的包上应用 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

因此,与以前不同,工厂“my-factory”可用。

但是,我的实例不可用,其创建如下:

DefaultInstanceDeclaration providerDeclaration = new DefaultInstanceDeclaration(b.getBundleContext(), "my-factory");
                        providerDeclaration.start();

同样,这不会显示错误,但不会在包的 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

你能帮忙吗?谢谢。

4

1 回答 1

0

使用实例声明对我不起作用。不过,我能够使用 Factory Service 实例化我的组件,如下所示:

ServiceReference[] references = context.getServiceReferences(Factory.class.getName(),"(factory.name=my-factory)");

    if (references == null)
    System.out.println("No reference");


    else {

          System.out.println(references[0].toString());

          Factory factory =  context.getService(references[0]);
          x = factory.createComponentInstance(null); //here instantiating my component
          x.start(); //this starts my component service and executes start method


          System.out.println(x.getState());
          System.out.println(x.getInstanceName());


          x.dispose(); //this stops my component service and executes stop method

         }

仅在将其放入将由我的 java 应用程序加载的包中后,我才使此代码起作用。

于 2014-02-01T19:54:25.597 回答