首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iPOJO组件实例化但没有可见的输出

iPOJO组件实例化但没有可见的输出
EN

Stack Overflow用户
提问于 2014-01-22 19:19:43
回答 1查看 403关注 0票数 2

我有两个iPOJO组件。

1-提供"Hello“服务的提供者包。以下是该构成部分的实现:

代码语言:javascript
复制
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对象作为折叠的消费者包:

代码语言:javascript
复制
 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上启动它们,如下所示:

代码语言:javascript
复制
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 provider服务的使用情况。我使用了实例声明,作为以下内容:

代码语言:javascript
复制
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执行了这个操作,如下所示:

代码语言:javascript
复制
<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>

现在,我可以从我的应用程序中看到工厂是有效的。这是命令中的输出:

代码语言:javascript
复制
g! ipojo:factories
Factory my-factory (VALID)
Factory org.apache.felix.ipojo.arch.gogo.Arch (UNKNOWN) - Private

因此,工厂“我的工厂”与以前不同。

但是,我的实例不可用,它是作为以下内容创建的:

代码语言:javascript
复制
DefaultInstanceDeclaration providerDeclaration = new DefaultInstanceDeclaration(b.getBundleContext(), "my-factory");
                        providerDeclaration.start();

同样,这没有显示错误,但它没有在包的start()方法中显示预期的输出,在命令中,我可以看到:

代码语言:javascript
复制
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

你能帮忙吗?谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-01 19:54:25

使用实例声明对我不起作用。但是,我能够使用工厂服务实例化我的组件,如下所示:

代码语言:javascript
复制
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应用程序加载。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21291983

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档