如果我参考服务,Gogo Shell命令将消失

时间:2019-07-19 04:39:51

标签: liferay osgi liferay-7 gogo-shell

源代码

在我的模块中,我编写了以下Liferay服务:

package jp.co.my.module;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.module.framework.service.IdentifiableOSGiService;
import com.liferay.portal.kernel.service.BaseLocalServiceImpl;

public class TestService extends BaseLocalServiceImpl
                         implements IdentifiableOSGiService {

    public void test() throws PortalException {
        System.out.println("In the future I will do stuff here");
    }

    @Override
    public String getOSGiServiceIdentifier() {
        return TestService.class.getName();
    }
}

在同一模块中,我编写了使用该服务的命令:

package jp.co.my.module;
import com.liferay.portal.kernel.exception.PortalException;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

@Component(
        property = {
                "osgi.command.function=test",
                "osgi.command.scope=liferay"
        },
        service = Object.class)
public class TestCommand {

    public void test() throws PortalException {
        System.out.println("In the future I will call the service here");
    }

    @Reference private volatile TestService _testService;
}

问题

该模块已正确部署,但在Gogo Shell中该命令不可用:

g! test
gogo: CommandNotFoundException: Command not found: test
g! b 1001
jp.co.my.module_1.0.0 [1001]
  Id=1001, Status=ACTIVE      Data Root=/home/nico/liferay/osgi/state/org.eclipse.osgi/1001/data                                               
  "No registered services."
  Services in use:
    {org.osgi.service.log.LogService, org.eclipse.equinox.log.ExtendedLogService}={service.id=2, service.bundleid=0, service.scope=bundle}             
  No exported packages
  Imported packages
    com.liferay.portal.kernel.exception; version="7.1.0" <org.eclipse.osgi_3.10.200.v20150831-0856 [0]>                                                
    com.liferay.portal.kernel.module.framework.service; version="1.0.0" <org.eclipse.osgi_3.10.200.v20150831-0856 [0]>                                 
    com.liferay.portal.kernel.service; version="1.27.0" <org.eclipse.osgi_3.10.200.v20150831-0856 [0]>                                                 
  No fragment bundles
  No required bundles

但是!!如果我从@Reference private volatile TestService _testService;中删除了TestCommand.java,它会完美运行

g! test
In the future I will call the service here
g! b 1005
jp.co.my.module_1.0.0 [1005]
  Id=1005, Status=ACTIVE      Data Root=/home/nico/liferay/osgi/state/org.eclipse.osgi/1005/data
  "Registered Services"
    {java.lang.Object}={osgi.command.function=test, component.name=jp.co.my.module.TestCommand, component.id=2740, osgi.command.scope=liferay, service.id=7652, service.bundleid=1005, service.scope=bundle}
  Services in use:
    {org.osgi.service.log.LogService, org.eclipse.equinox.log.ExtendedLogService}={service.id=2, service.bundleid=0, service.scope=bundle}
  No exported packages
  Imported packages
    com.liferay.portal.kernel.exception; version="7.1.0" <org.eclipse.osgi_3.10.200.v20150831-0856 [0]>
    com.liferay.portal.kernel.module.framework.service; version="1.0.0" <org.eclipse.osgi_3.10.200.v20150831-0856 [0]>
    com.liferay.portal.kernel.service; version="1.27.0" <org.eclipse.osgi_3.10.200.v20150831-0856 [0]>
  No fragment bundles
  No required bundles

问题:为什么?如何正确引用服务?

1 个答案:

答案 0 :(得分:2)

我还没有使用liferay,但它似乎非常遵循OSGI概念。您的TestCommand是依赖于TestService的组件,因为您已使用@Reference。这意味着TestService也是一个组件。但是您还没有在任何地方将TestService定义为组件。

我的有根据的猜测是TestService也必须使用@Component,因此liferay将知道它是一个组件,将其创建,并将其注入TestCommand:

@Component
public class TestService extends BaseLocalServiceImpl
                     implements IdentifiableOSGiService {

我不知道,尝试在没有定义的Java接口的情况下尝试创建和引用组件可能还会有其他问题。

编辑:我的回答在这里反映了一些信息。 https://portal.liferay.dev/docs/7-0/tutorials/-/knowledge_base/t/osgi-and-modularity-for-liferay-6-developers#osgi-services-and-dependency-injection-with-declarative-services

EDIT2:它完美运行的原因是:删除@Reference会删除TestCommand和TestService之间的损坏的依赖关系链接。在这一行中,依赖关系链在那里但没有解决,因此您的TestCommand将不会启动

EDIT3:

@Component(
    configurationPid = "jp.co.my.module",
    immediate = true,
    property = {}, 
    service = TestService.class
    )