Expand my Community achievements bar.

SOLVED

Activate method not getting executed

Avatar

Level 3

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.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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

View solution in original post

4 Replies

Avatar

Correct answer by
Community Advisor

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

Avatar

Level 2

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?

Avatar

Community Advisor

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

Avatar

Level 2

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.