小编Cpt*_*der的帖子

OSGi组件的默认配置值包含转义的反斜杠

下面是我简单的OSGi组件的代码

@Component(metatype = true, label = "My Component", policy = ConfigurationPolicy.REQUIRE)
@Property(label = "My Component's expression", name = "my.expression", value = "/5 * * * * ? *")
public class MyComponent {

    private static final Logger log = LoggerFactory.getLogger(MyComponent.class);

    @Reference
    private Scheduler scheduler;

    @Activate
    public void activate(final ComponentContext context) {
        final String quartzExpression = PropertiesUtil.toString(
                context.getProperties().get("my.expression"), "");
        ScheduleOptions options = scheduler.EXPR(quartzExpression).name("MyJob");
        scheduler.schedule(new Runnable() {

            @Override
            public void run() {
                log.info("Hello World!");

            }
        }, options);
    }
}
Run Code Online (Sandbox Code Playgroud)

应该做什么:它采用一个Quartz表达式的配置值,并使用它来计划记录简单问候的作业。

默认情况下,我希望该作业每5秒执行一次,这是该属性的默认值。现在,由于我还指定了ConfigurationPolicy.REQUIRE,所以我实际上需要创建配置。为此,我转到OSGi控制台的配置管理器, …

java osgi aem

4
推荐指数
1
解决办法
939
查看次数

模拟 OSGi 上下文中的服务激活异常

在我的 Maven 项目中,我创建了一个简单的 OSGi 服务,它只接受一个引用:

import org.apache.sling.api.resource.ResourceResolverFactory;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

@Component
public class MyFoo {
    @Reference
    private ResourceResolverFactory factory;
}
Run Code Online (Sandbox Code Playgroud)

然后,使用 osgi-mock 教程,我创建了以下测试类:

import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.testing.mock.osgi.junit.OsgiContext;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
public class MyFooTest {
    @Rule
    public OsgiContext mockContext = new OsgiContext();

    @Test
    public void test() {
        ResourceResolverFactory mockFactory = Mockito.mock(ResourceResolverFactory.class);

        mockContext.registerService(ResourceResolverFactory.class, mockFactory);
        mockContext.registerInjectActivateService(new MyFoo());
    }

}
Run Code Online (Sandbox Code Playgroud)

测试在最后一行崩溃,但有以下异常:

org.apache.sling.testing.mock.osgi.NoScrMetadataException: No OSGi SCR metadata found for class MyFoo

    at org.apache.sling.testing.mock.osgi.OsgiServiceUtil.injectServices(OsgiServiceUtil.java:381)
    at org.apache.sling.testing.mock.osgi.MockOsgi.injectServices(MockOsgi.java:148)
    at …
Run Code Online (Sandbox Code Playgroud)

osgi unit-testing sling aem

3
推荐指数
1
解决办法
2158
查看次数

标签 统计

aem ×2

osgi ×2

java ×1

sling ×1

unit-testing ×1