Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

Adobe Summit 2023 [19th to 23rd March, Las Vegas and Virtual] | Complete AEM Session & Lab list
SOLVED

I created an OSGI service that accesses a configuration in configMgr. How do I access this service inside a workflow item?

Avatar

Level 8

in my custom class, I am extending com.adobe.granite.workflow.exec.WorkflowProcess.

 

@component(service = WorkflowProcess.class, property = {"process.label=My Custom Workflow Process"})
public class MyCustomWorkflowStep implements WorkflowProcess {

 

Inside a Sling Model, I can use @inject annotation (javax.inject.Inject). But I'm unsure on how to do this inside a workflow. Or perhaps what's the best way of accessing a configuration inside a workflow.

 

Thank you.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

With @reference annotation , you can invoke OSGi service just like the below one -

/**
*
*/
package com.aem.demo.core.workflows;

import java.util.Date;
import java.util.Objects;

import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
import org.apache.jackrabbit.vault.util.JcrConstants;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.adobe.granite.workflow.WorkflowException;
import com.adobe.granite.workflow.WorkflowSession;
import com.adobe.granite.workflow.exec.WorkItem;
import com.adobe.granite.workflow.exec.WorkflowProcess;
import com.adobe.granite.workflow.metadata.MetaDataMap;
import com.day.cq.mailer.MessageGateway;
import com.day.cq.mailer.MessageGatewayService;
import com.day.cq.mailer.email.EmailTemplate;
import com.drew.lang.annotations.Nullable;

/**
* @author debal This workflow process step is responsible to send email
* notification with payload (content) path and expiry datetime details
*
*/
@Component(property = { Constants.SERVICE_DESCRIPTION + "=This workflow step is responsible to send Email Notification",
Constants.SERVICE_VENDOR + "=AEM Demo Debal", "process.label" + "=Email Sending Notification process" })
public class ContentExpiryNotification implements WorkflowProcess {

private final Logger logger = LoggerFactory.getLogger(ContentExpiryNotification.class);


@Reference
EmailTemplate<Email> emailTemplate;

@Reference
MessageGatewayService messageGatewayService;

@Override
public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap)
throws WorkflowException {

String payLoadPath = workItem.getWorkflowData().getPayload().toString();
logger.info("*** Payload path *** {}", payLoadPath);

@Nullable
ResourceResolver resourceResolver = workflowSession.adaptTo(ResourceResolver.class);
if (Objects.nonNull(resourceResolver)) {
String resourceContentpath = payLoadPath.concat("/").concat(JcrConstants.JCR_CONTENT);

logger.info("*** Resource Content Path *** {}", resourceContentpath);
@Nullable
Resource resource = resourceResolver.getResource(resourceContentpath);
if (Objects.nonNull(resource)) {

@Nullable
ValueMap resourceValueMap = resource.adaptTo(ValueMap.class);
if (Objects.nonNull(resourceValueMap)) {
@Nullable
Date resourceOffedate = resourceValueMap.get("offTime", Date.class);
logger.info("*** Content Expiration Date *** {}", resourceOffedate);
if (Objects.nonNull(resourceOffedate)) {
sendMail(payLoadPath, resourceOffedate);
}

}
}

}

}

private void sendMail(String payLoadPath, Date expiryDate) {

MessageGateway<Email> messageGateway;
Email email = new SimpleEmail();
StringBuilder sb = new StringBuilder();

String emailToRecipients = "debal.india2014@gmail.com";

try {
email.addTo(emailToRecipients);
email.setSubject("AEM Content Expiration Notofication");
email.setFrom("debal.india2016@gmail.com");
email.setMsg("*********" + sb.append("Notification Message details***********").append(payLoadPath)
.append(" ").append(expiryDate));

messageGateway = messageGatewayService.getGateway(Email.class);

messageGateway.send((Email) email);

} catch (EmailException e) {

logger.error("SMTP issue", e.getMessage());
}

}

}

 

2 Replies

Avatar

Correct answer by
Community Advisor

With @reference annotation , you can invoke OSGi service just like the below one -

/**
*
*/
package com.aem.demo.core.workflows;

import java.util.Date;
import java.util.Objects;

import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
import org.apache.jackrabbit.vault.util.JcrConstants;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.adobe.granite.workflow.WorkflowException;
import com.adobe.granite.workflow.WorkflowSession;
import com.adobe.granite.workflow.exec.WorkItem;
import com.adobe.granite.workflow.exec.WorkflowProcess;
import com.adobe.granite.workflow.metadata.MetaDataMap;
import com.day.cq.mailer.MessageGateway;
import com.day.cq.mailer.MessageGatewayService;
import com.day.cq.mailer.email.EmailTemplate;
import com.drew.lang.annotations.Nullable;

/**
* @author debal This workflow process step is responsible to send email
* notification with payload (content) path and expiry datetime details
*
*/
@Component(property = { Constants.SERVICE_DESCRIPTION + "=This workflow step is responsible to send Email Notification",
Constants.SERVICE_VENDOR + "=AEM Demo Debal", "process.label" + "=Email Sending Notification process" })
public class ContentExpiryNotification implements WorkflowProcess {

private final Logger logger = LoggerFactory.getLogger(ContentExpiryNotification.class);


@Reference
EmailTemplate<Email> emailTemplate;

@Reference
MessageGatewayService messageGatewayService;

@Override
public void execute(WorkItem workItem, WorkflowSession workflowSession, MetaDataMap metaDataMap)
throws WorkflowException {

String payLoadPath = workItem.getWorkflowData().getPayload().toString();
logger.info("*** Payload path *** {}", payLoadPath);

@Nullable
ResourceResolver resourceResolver = workflowSession.adaptTo(ResourceResolver.class);
if (Objects.nonNull(resourceResolver)) {
String resourceContentpath = payLoadPath.concat("/").concat(JcrConstants.JCR_CONTENT);

logger.info("*** Resource Content Path *** {}", resourceContentpath);
@Nullable
Resource resource = resourceResolver.getResource(resourceContentpath);
if (Objects.nonNull(resource)) {

@Nullable
ValueMap resourceValueMap = resource.adaptTo(ValueMap.class);
if (Objects.nonNull(resourceValueMap)) {
@Nullable
Date resourceOffedate = resourceValueMap.get("offTime", Date.class);
logger.info("*** Content Expiration Date *** {}", resourceOffedate);
if (Objects.nonNull(resourceOffedate)) {
sendMail(payLoadPath, resourceOffedate);
}

}
}

}

}

private void sendMail(String payLoadPath, Date expiryDate) {

MessageGateway<Email> messageGateway;
Email email = new SimpleEmail();
StringBuilder sb = new StringBuilder();

String emailToRecipients = "debal.india2014@gmail.com";

try {
email.addTo(emailToRecipients);
email.setSubject("AEM Content Expiration Notofication");
email.setFrom("debal.india2016@gmail.com");
email.setMsg("*********" + sb.append("Notification Message details***********").append(payLoadPath)
.append(" ").append(expiryDate));

messageGateway = messageGatewayService.getGateway(Email.class);

messageGateway.send((Email) email);

} catch (EmailException e) {

logger.error("SMTP issue", e.getMessage());
}

}

}

 

Avatar

Employee Advisor

Hey @jayv25585659 ,

 

As you have already mentioned that you are using a OSGIService which is accessing the OSGIConfig.
MyConfiguration:

@ObjectClassDefinition(
		name = "My Configuration", 
		description = "This configuration reads the values")
public @interface MyConfiguration {
// All attribute definitions
}

 

MyServiceUsingConfig looks like :

@Component(service = MyServiceInterface.class, immediate = true)
@Designate(ocd = MyConfiguration.class)
public class MyServiceUsingConfig implements MyServiceInterface{
    public void doSomething()
}

 

Now simply to call the same anywhere in Scheduler/Workflow/Servlet, you can use @Reference annotation:

@Component(service = WorkflowProcess.class, property = {"process.label=My Custom Workflow Process"})
public class MyCustomWorkflowStep implements WorkflowProcess {
   @Reference
   private MyServiceUsingConfig thisService;
  
   //use method
   thisService.doSomething();
}

 

Hope this helps.

Thanks,

Milind