acs-aem-commons icon indicating copy to clipboard operation
acs-aem-commons copied to clipboard

How to make MCP work in AEM as Cloud?

Open gustavo-rey opened this issue 3 years ago • 4 comments

Required Information

  • AEM Version, including Service Packs, Cumulative Fix Packs, etc: AEM as Cloud, Uber-Jar 6.4.1, OSGi-Core 6.0, OSGi Annotation, Project Java 8, Build Java 11, AEM RELEASE: 2022.4.7138.20220427T075748Z
  • ACS AEM Commons Version: 5.1.2
  • Reproducible on Latest? yes

Expected Behavior | Actual Behavior

Guys, I'm trying to create a process with MCP, but when I deploy my code, the option for my process doesn't appear. I took another project ready at my company that has MCP processes created and even deploying it on my instance, it doesn't appear either. '

I would say that the problem is my code, however, this other project of my company works in another instance.

For AEM as Cloud is it necessary to use some other annotation or something?

This is my code

Factory Class

package com.mycompany.mcp.factory;

import com.adobe.acs.commons.mcp.ProcessDefinitionFactory;
import com.mycompany.mcp.ContentInstanceUpdaterProcess;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.ServiceScope;

//In the ACS tutorial is using this annotation
//import org.apache.felix.scr.annotations.Service;

//@Component
//@Service(ProcessDefinitionFactory.class)

//This is the other approach that the guys in my company use, I've tried too.
@Component(
        service = ProcessDefinitionFactory.class,
        immediate = true,
        scope = ServiceScope.SINGLETON)
public class ContentInstanceUpdaterFactory extends ProcessDefinitionFactory<ContentInstanceUpdaterProcess> {
    @Override
    public String getName() {
        return "Content Instance Updater";
    }

    @Override
    protected ContentInstanceUpdaterProcess createProcessDefinitionInstance() {
        return new ContentInstanceUpdaterProcess();
    }
}

Process Class

package com.mycompany.mcp;

import com.adobe.acs.commons.mcp.ProcessDefinition;
import com.adobe.acs.commons.mcp.ProcessInstance;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jcr.RepositoryException;

public class ContentInstanceUpdaterProcess extends ProcessDefinition {

    private static final Logger LOG = LoggerFactory.getLogger(ContentInstanceUpdaterProcess.class);

    @Override
    public void buildProcess(ProcessInstance processInstance, ResourceResolver resourceResolver) throws LoginException, RepositoryException {
        LOG.error("Worked");
    }

    @Override
    public void storeReport(ProcessInstance processInstance, ResourceResolver resourceResolver) throws RepositoryException, PersistenceException {
        LOG.error("Worked");
    }

    @Override
    public void init() throws RepositoryException {
        LOG.error("Worked");
    }
}

Note: To use the @Service annotation that is in the tutorial, I didn't find any from the OSGi package, only from the Apache Felix package, in the tutorial it doesn't show the imports

gustavo-rey avatar May 26 '22 18:05 gustavo-rey

You should just have to annotate your class with:

@Component(service = { ProcessDefinitionFactory.class } )

Try adding atleast 1 @FormField and a crtical action (just make it NOOP) like in: https://github.com/Adobe-Consulting-Services/acs-aem-commons/blob/master/bundle/src/main/java/com/adobe/acs/commons/mcp/impl/processes/AssetFolderCreator.java

Also - can you amke sure that your Factory OSGi component is active once installed?

davidjgonzalez avatar Jun 01 '22 13:06 davidjgonzalez

@gustavo-rey were you able to resolve this?

davidjgonzalez avatar Jun 22 '22 22:06 davidjgonzalez

I'm not able @davidjgonzalez , I changed my imports that were different from the class that you sent, I also put 1 @FormField and didn't work again.

Just about your suggestion that put @Component(service = { ProcessDefinitionFactory.class } ), my Component annotation doesn't have this attribute, maybe it's my apache felix version, I'm using org.apache.felix.src.annotations-1.9.8.jar

gustavo-rey avatar Jun 23 '22 17:06 gustavo-rey

@gustavo-rey you need to write a factory class to plugin your process in AEM. Have a look at https://github.com/Adobe-Consulting-Services/acs-aem-commons/tree/master/bundle/src/main/java/com/adobe/acs/commons/mcp/impl/processes Each process definition is accompanied by a *Factory class which plugs it in AEM and defines the name that you will see in the UI.

In your case the Factory class would be

package com.mycompany.mcp.factory;

import com.adobe.acs.commons.mcp.ProcessDefinitionFactory;
import org.osgi.service.component.annotations.Component;

@Component(service = ProcessDefinitionFactory.class)
public class ContentInstanceUpdaterProcessFactory extends ProcessDefinitionFactory<ContentInstanceUpdaterProcess> {

    @Override
    public String getName() {
        return "Content Instance Updater";
    }

    @Override
    protected ContentInstanceUpdaterProcess createProcessDefinitionInstance() {
        return new ContentInstanceUpdaterProcess();
    }
}

just put the factory class next to your process definition and deploy the code. It should work. I tried it with the latest AEM SDK for AEM v2022.6.7904.20220629T070041Z-220600 and it worked for me. image

My sample project is https://github.com/YegorKozlov/acs-custom-mcp-process

YegorKozlov avatar Jul 07 '22 14:07 YegorKozlov