【问题标题】:iPOJO Components instantiated but no visible outputiPOJO 组件已实例化,但没有可见的输出
【发布时间】:2014-01-22 19:19:43
【问题描述】:

我有 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”消息。我什么都看不到。这意味着组件没有正确实例化。我哪里做错了?谢谢。

更新

我发现我没有对我的 bundle 应用 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

你能帮忙吗?谢谢。

【问题讨论】:

    标签: java components factory apache-felix ipojo


    【解决方案1】:

    使用实例声明对我不起作用。不过,我可以使用 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 应用程序加载。

    【讨论】:

      猜你喜欢
      • 2020-05-07
      • 2013-07-20
      • 1970-01-01
      • 2014-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-11
      • 1970-01-01
      相关资源
      最近更新 更多