Hi, I have a servlet that performs a function, I have tested it and it works, to make it execute it is necessary to write the domain name followed by bin/serviceexecution.
But now we want it to run only every so often, for that we have created a scheduler.
The problem is that the scheduler is executed but when arriving to the call of the servlet, this fails, mentioning that we do not have permissions to execute the servlet.
I think it is because to execute this servlet I need permissions, which I don't know how to set them.
How could I add them or know what is the error.
this is the code I made
@Component(service = Runnable.class, immediate = true)
@Designate(ocd = SchedulerUseConfig.class)
public class SchedulerUse implements Runnable {
private static final Logger log = LoggerFactory.getLogger(SchedulerUse.class);
@Reference
private Scheduler scheduler;
private int schedulerId;
private SchedulerUseConfig config;
@Activate
@Modified
protected void activate(SchedulerUseConfig config) {
this.config = config;
schedulerId = config.schedulerName().hashCode();
log.info("SchedulerUse: Activating or Modifying");
updateScheduler();
}
@Deactivate
protected void deactivate() {
log.info("SchedulerUse: Deactivating");
removeScheduler();
}
private void removeScheduler() {
log.info("SchedulerUse: Removing Scheduler Job '{}'", schedulerId);
scheduler.unschedule(String.valueOf(schedulerId));
}
private void updateScheduler() {
removeScheduler();
if (config.enabled()) {
log.info("SchedulerUse: Adding scheduler");
ScheduleOptions scheduleOptions = scheduler.EXPR(config.cronExpression());
scheduleOptions.name(String.valueOf(schedulerId));
scheduleOptions.canRunConcurrently(false);
scheduler.schedule(this, scheduleOptions);
log.info("SchedulerUse: Cache Clean Scheduler added successfully with cron expression: {}", config.cronExpression());
} else {
log.info("SchedulerUse: Scheduler is disabled, no scheduler job created");
}
}
@Override
public void run() {
log.info("SchedulerUse: Starting scheduled run");
if (config.enabled()) {
try {
String url = config.baseUrl() + "/bin/serviceexecution";
URL servletUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) servletUrl.openConnection();
connection.setRequestMethod("GET");
int responseCode = connection.getResponseCode();
log.info("SchedulerUse: Response Code: {}", responseCode);
if (responseCode != HttpURLConnection.HTTP_OK) {
log.error("SchedulerUse: Servlet call failed with response code: {}", responseCode);
}
} catch (IOException e) {
log.error("Error: Error while calling servlet", e);
}
} else {
log.info("Message: Scheduler is disabled, skipping run");
}
log.info("Message: Finished scheduled run");
}
}