Expand my Community achievements bar.

SOLVED

Created scheduler but not able to see log output in the project log

Avatar

Level 2

Below is the code for configuring the scheduler. You can refer to the screenshot of the configuration

 

@ObjectClassDefinition(
name = "AEM: SlingSchedulerConfiguration",
description = "Sling scheduler configuration"
)
public @interface SchedulerConfig
{

@AttributeDefinition(name = "Cron-job expression")
String scheduler_expression() default "*/10 * * * * ?";

@AttributeDefinition(name = "A parameter",
description = "Can be configured in /system/console/configMgr")
String myParameter() default "";
}

 

 

@Designate(ocd=SchedulerConfig.class)
@component(service=Runnable.class, immediate = true)
public class MyScheduler implements Runnable
{
private final Logger logger = LoggerFactory.getLogger(getClass());

private String myParameter;
@Override
public void run() {
logger.debug("MyScheduler is now running, myParameter='{}'", myParameter);
}

@activate
protected void activate(final SchedulerConfig config) {
myParameter = config.myParameter();
}
}

 

 

 

 

 

ashwinikhaple_1-1618470974589.png

 

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @ashwinikhaple 

 

Please update the below line of code:

 

@component(service=MyScheduler.class, immediate = true)

@Designate(ocd=SchedulerConfig.class)

public class MyScheduler implements Runnable

 

Service class should be your component class name.

 

Thanks!

 

View solution in original post

6 Replies

Avatar

Correct answer by
Community Advisor

Hi @ashwinikhaple 

 

Please update the below line of code:

 

@component(service=MyScheduler.class, immediate = true)

@Designate(ocd=SchedulerConfig.class)

public class MyScheduler implements Runnable

 

Service class should be your component class name.

 

Thanks!

 

Avatar

Level 2
Hello @Asutosh_Jena_, I modified service class to service name but still it is not working

Avatar

Community Advisor

HI @ashwinikhaple 

 

Please check the log level for your OSGi config.

As per code, it is set as DEBUG so in your logger configuration also it should be set as DEBUG.

if it's configured as INFO, DEBUG statements will not be logged into the log file.

logger.debug("MyScheduler is now running, myParameter='{}'", myParameter);

 

Either change the log level in the OSGi config to DEBUG, else change the log level in code to logger.info if OSGI has already configured with INFO as the level.

 

Thanks!

Avatar

Level 2

I am getting the below error

 

Caused by: org.apache.sling.api.scripting.ScriptEvaluationException:
at org.apache.sling.scripting.core.impl.DefaultSlingScript.call(DefaultSlingScript.java:416) [org.apache.sling.scripting.core:2.0.56]
at org.apache.sling.scripting.core.impl.DefaultSlingScript.eval(DefaultSlingScript.java:184) [org.apache.sling.scripting.core:2.0.56]
at org.apache.sling.scripting.core.impl.DefaultSlingScript.service(DefaultSlingScript.java:491) [org.apache.sling.scripting.core:2.0.56]
at org.apache.sling.servlets.resolver.internal.SlingServletResolver.handleError(SlingServletResolver.java:621) [org.apache.sling.servlets.resolver:2.4.24]
... 68 common frames omitted
Caused by: org.apache.sling.api.SlingException:
at org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper.handleJspExceptionInternal(JspServletWrapper.java:691) [org.apache.sling.scripting.jsp:2.3.4]
at org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:608) [org.apache.sling.scripting.jsp:2.3.4]
at org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:533) [org.apache.sling.scripting.jsp:2.3.4]
at org.apache.sling.scripting.jsp.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:449) [org.apache.sling.scripting.jsp:2.3.4]
at org.apache.sling.scripting.jsp.JspScriptEngineFactory.callJsp(JspScriptEngineFactory.java:342) [org.apache.sling.scripting.jsp:2.3.4]
at org.apache.sling.scripting.jsp.JspScriptEngineFactory.access$100(JspScriptEngineFactory.java:97) [org.apache.sling.scripting.jsp:2.3.4]
at org.apache.sling.scripting.jsp.JspScriptEngineFactory$JspScriptEngine.eval(JspScriptEngineFactory.java:603) [org.apache.sling.scripting.jsp:2.3.4]
at org.apache.sling.scripting.core.impl.DefaultSlingScript.call(DefaultSlingScript.java:388) [org.apache.sling.scripting.core:2.0.56]
... 71 common frames omitted
Caused by: org.eclipse.jetty.io.RuntimeIOException: org.eclipse.jetty.io.EofException
at org.eclipse.jetty.server.ResponseWriter.isOpen(ResponseWriter.java:133) [org.apache.felix.http.jetty:4.0.8]
at org.eclipse.jetty.server.ResponseWriter.write(ResponseWriter.java:202) [org.apache.felix.http.jetty:4.0.8]
at java.io.PrintWriter.write(Unknown Source)

Avatar

Community Advisor

@ashwinikhaple I do not see any issue with the code. This error is for some other code.

Can you please put the complete error message to know where this error is coming exactly?

 

Also for the Scheduler, please use the below CRON expression. This will ensure the scheduler is running every 1 min and it will print the log. 

0 0/1 * 1/1 * ? *

 

Else you can add a logger statement in the @activate method which will print he log when you deploy the code.

 

Do let me know if this does not work.

 

@Activate // This will execute each time the bundle get's deployed into the system i.e. each deployment
protected void activate(final SchedulerConfig schedulerConfig) {
log.info("***** :: Activate :: *****");
myParameter = schedulerConfig.myParameter();
}
@Override
public void run() { // This will execute each time the scheduler runs.
log.info("***** :: run :: Start :: *****");
log.info("***** :: My Scheduler is Running :: {}", myParameter);
log.info("***** :: run :: End :: *****");
}

Thanks!

Avatar

Community Advisor

Hi @ashwinikhaple,

Code that you have shared in your query should work as is. (Written following the code that comes with Sample Scheduler in AEM Maven archetype project)

Only reason for not seeing the lop output is the log level. 

Note : In general, Service name should always be the interface that the component implements. (service attribute in @Component annotation)