Activate method not getting executed | Community
Skip to main content
manpreetk
Level 2
June 13, 2019
Solved

Activate method not getting executed

  • June 13, 2019
  • 4 replies
  • 4904 views

Hi All, I have created a simple service in Sling and deployed the same on my AEM instance. However, the activate method is not getting called for this class. Here is my implementation class: package com.company.project.auth.impl; import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Component; import org.osgi.service.metatype.annotations.Designate; import org.slf4j.Logger; @Designate(ocd=MyConfiguration.class) @Component(service = MyRequestImpl.class, immediate = true, configurationPid = "com.company.project.auth.impl.MyRequestImpl") public class MyRequestImpl implements MyRequest{                 protected final transient Logger logger = LoggerFactory.getLogger(this.getClass());                                 @Activate                 protected void activate(                                                 final MyConfiguration config) {                                 logger.info("Calling Activate");                 }                 @Override                 public String myFunction() {                                 logger.info("Calling my function");                          } } I am able to see  the log, "Calling my function" but not the one, "Calling Activate". Please suggest a solution.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by arunpatidar

Hi,

Can you check if your service is active or not? I tried with your code with small modification works for me.

package com.aem.community.core.services;

import org.osgi.service.component.annotations.Activate;

import org.osgi.service.component.annotations.Component;

import org.osgi.service.metatype.annotations.Designate;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import com.aem.community.core.Config;

@Designate(ocd = Config.class)

@Component(service = MyRequest.class, immediate = true, configurationPid = "com.aem.community.core.services.MyRequest")

public class MyRequestImpl implements MyRequest {

  protected final transient Logger logger = LoggerFactory.getLogger(this.getClass());

  @Activate

  protected void activate(final Config config) {

  logger.info("Calling Activate");

  }

  @Override

  public String myFunction() {

  logger.info("Calling my function");

  return "myfunction";

  }

}

4 replies

arunpatidar
Community Advisor
arunpatidarCommunity AdvisorAccepted solution
Community Advisor
June 13, 2019

Hi,

Can you check if your service is active or not? I tried with your code with small modification works for me.

package com.aem.community.core.services;

import org.osgi.service.component.annotations.Activate;

import org.osgi.service.component.annotations.Component;

import org.osgi.service.metatype.annotations.Designate;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import com.aem.community.core.Config;

@Designate(ocd = Config.class)

@Component(service = MyRequest.class, immediate = true, configurationPid = "com.aem.community.core.services.MyRequest")

public class MyRequestImpl implements MyRequest {

  protected final transient Logger logger = LoggerFactory.getLogger(this.getClass());

  @Activate

  protected void activate(final Config config) {

  logger.info("Calling Activate");

  }

  @Override

  public String myFunction() {

  logger.info("Calling my function");

  return "myfunction";

  }

}

Arun Patidar
akshat_jain09
Level 2
June 18, 2019

Hi Arun

I checked that the service is active. But still On hitting the service, I am not able to see the activate log.

Can you suggest what change made it work for you?

arunpatidar
Community Advisor
Community Advisor
June 18, 2019

Are you getting any exception? Can you try with my code.

I used my config and changed Pid and service in @component annotation.

Arun Patidar
akshat_jain09
Level 2
June 18, 2019

Well I am not getting any exception, and the code is also same. Its just that I am using my custom service and configuration so the pid also differs.

But not getting the desired result.