我想有关于 Sling Eventing 的实用知识,所以我试图编写代码。我写了一个代码,但我的吊索事件不起作用。我可以在 osgi felix 控制台中看到我的包处于活动状态,但它没有在监听事件。
有什么建议么 。
我想有关于 Sling Eventing 的实用知识,所以我试图编写代码。我写了一个代码,但我的吊索事件不起作用。我可以在 osgi felix 控制台中看到我的包处于活动状态,但它没有在监听事件。
有什么建议么 。
我猜你没有 bundle activate() 方法。我们使用“@Activate”注解来表示它。
import org.apache.felix.scr.annotations.Activate
...
@Activate
void activate(ComponentContext context) {...}
package com.adobe.training.core;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.event.jobs.JobProcessor;
import org.apache.sling.jcr.resource.JcrResourceResolverFactory;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.day.cq.replication.ReplicationAction;
import com.day.cq.replication.ReplicationActionType;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
@SuppressWarnings("deprecation")
@Service(value = EventHandler.class)
@Component(immediate = true)
@Property(name = "event.topics", value = ReplicationAction.EVENT_TOPIC)
public class ReplicationLogger implements EventHandler, JobProcessor {
private static final Logger LOGGER = LoggerFactory.getLogger(ReplicationLogger.class);
@Reference
private JcrResourceResolverFactory jcrResourceResolverFactory;
@Override
public void handleEvent(Event event) {
LOGGER.info("********handling event");
process (event);
}
@Override
public boolean process(Event event) {
LOGGER.info("********processing job");
ReplicationAction action = ReplicationAction.fromEvent(event);
ResourceResolver resourceResolver = null;
if (action.getType().equals(ReplicationActionType.ACTIVATE)) {
try {
resourceResolver = jcrResourceResolverFactory.getAdministrativeResourceResolver(null);
final PageManager pm = resourceResolver.adaptTo(PageManager.class);
final Page page = pm.getContainingPage(action.getPath());
if(page != null) {
LOGGER.info("********activation of page {}", page.getTitle());
}
}
catch (LoginException e) {
e.printStackTrace();
}
finally {
if(resourceResolver != null && resourceResolver.isLive()) {
resourceResolver.close();
}
}
}
return true;
}
}