I found the Solution to you. Everything i wrote is applicable. You need to break the code into a Interface, Service and Model.
Notice the Run method.
//Define RUN
public void run() {
try {
//Is SESSION valid
LOG.info("RUNNING IN THE RUN METHOD");
ResourceResolver resourceResolver = resolverFactory.getAdministrativeResourceResolver(null);
session = resourceResolver.adaptTo(Session.class);
LOG.info("SESSION ID is " +session);
}
catch (Exception e)
{
e.printStackTrace();
}
}
This logged a message showing it was successfully invoked:
20.03.2018 22:56:48.589 *INFO* [Timer-23] com.adobe.community.time.core.TimeIml RUNNING IN THE RUN METHOD
20.03.2018 22:56:48.590 *INFO* [Timer-23] com.adobe.community.time.core.TimeIml SESSION ID is com.adobe.granite.repository.impl.CRX3SessionImpl@1e6230e
This was broken into a Service that implements a Java interface and extends TimerTask
We broke the code into this Interface:
package com.adobe.community.time.core;
public interface Time {
public String getTime() ;
}
This Implementation class
package com.adobe.community.time.core;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import org.apache.felix.scr.annotations.Reference;
import javax.inject.Inject;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import java.util.HashMap;
import java.util.Map;
//Sling Imports
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.Model;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.TimerTask;
@Component
@Service
public class TimeIml extends TimerTask implements Time{
//Inject a Sling ResourceResolverFactory
@Reference
private ResourceResolverFactory resolverFactory;
private static Logger LOG = LoggerFactory.getLogger(TimeIml.class);
private Session session;
//Define RUN
public void run() {
try {
//Is SESSION valid
LOG.info("RUNNING IN THE RUN METHOD");
ResourceResolver resourceResolver = resolverFactory.getAdministrativeResourceResolver(null);
session = resourceResolver.adaptTo(Session.class);
LOG.info("SESSION ID is " +session);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public String getTime()
{
try{
//Invoke the adaptTo method to create a Session
ResourceResolver resourceResolver = resolverFactory.getAdministrativeResourceResolver(null);
session = resourceResolver.adaptTo(Session.class);
LOG.info("Entering getCustData()");
return "LAM22";
}
catch (Exception e)
{
e.printStackTrace();
}
return "Error" ;
}
}
Model Class – we casted the 1st parameter for timer1.schedule – that was the KEY!!!!
timer1.schedule((TimerTask)time, timer);
Code for Model class – THis logges the message in the RUN METHOD – meaning it worked!
20.03.2018 22:56:48.589 *INFO* [Timer-23] com.adobe.community.time.core.TimeIml RUNNING IN THE RUN METHOD
- 20.03.2018 22:56:48.590 *INFO* [Timer-23] com.adobe.community.time.core.TimeIml SESSION ID is com.adobe.granite.repository.impl.CRX3SessionImpl@1e6230e
package com.adobe.community.time.core.models;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Named;
import com.adobe.community.time.core.Time;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.Default;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.settings.SlingSettingsService;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Model(adaptables=Resource.class)
public class HelloWorldModel {
@Inject
private SlingSettingsService settings;
@Inject
private Time time;
@Inject @Named("sling:resourceType") @Default(values="No resourceType")
protected String resourceType;
private String message;
private static Logger LOG = LoggerFactory.getLogger(HelloWorldModel.class);
@PostConstruct
protected void init() {
message = "\tHello World!\n";
message += "\tThis is instance: " + settings.getSlingId() + "\n";
message += "\tTime is: " + time.getTime() + "\n";
SetTimeInfo();
}
public String getMessage() {
return message;
}
public void SetTimeInfo() {
try {
Date timer = new Date();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Timer timer1 = new Timer();
SimpleDateFormat inFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
SimpleDateFormat outFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
LOG.info("Right format is :::: "+outFormat.format(timer));
LOG.info("Current Time: " + df.format( new Date()));
//Date and time at which you want to execute
//Date date = df.parse("2018-03-18 17:08:00");
//MyTimeTask task = new MyTimeTask();
//schedulerTask.setTimer(timer1);
timer1.schedule((TimerTask)time, timer);
//String myVal33 = task.getCustData();
//this.subject = text;
} catch(Exception ex) {
ex.printStackTrace();
}
}
}
ALSO - I tested with ADMIN CALL - to run this - white list the bundle - see - https://forums.adobe.com/thread/2355506