Custom Cron Job in Bonita

1
0
-1

I am currently working submitting a custom cron job in Bonita. I actually managed to create a job and submit it. I can see the Job details in the Bonita Engine Database. But it fails when it is going to trigger. Anyone who came across the same situation or anyone knows what is the error? Please see my code below.

Following is the exception I am getting.

2018-03-05 13:57:00.052 +1300 org.quartz.core.ErrorLogger org.quartz.core.ErrorLogger schedulerError
SEVERE: An error occured instantiating job to be executed. job= '401.USER_SYNC'
org.quartz.SchedulerException: unable to create the BOS job [See nested exception: org.bonitasoft.engine.scheduler.exception.SSchedulerException: The job class couldn't be instantiated]
at org.bonitasoft.engine.scheduler.impl.TransactionalSimpleJobFactory.newJob(TransactionalSimpleJobFactory.java:65)
at org.quartz.core.JobRunShell.initialize(JobRunShell.java:127)
at org.quartz.core.QuartzSchedulerThread.run(QuartzSchedulerThread.java:375)
Caused by: org.bonitasoft.engine.scheduler.exception.SSchedulerException: The job class couldn't be instantiated
at org.bonitasoft.engine.scheduler.impl.SchedulerServiceImpl.getPersistedJob(SchedulerServiceImpl.java:323)
at org.bonitasoft.engine.scheduler.impl.TransactionalSimpleJobFactory.newJob(TransactionalSimpleJobFactory.java:59)
... 2 more
Caused by: java.lang.ClassNotFoundException: nz.ac.auckland.bpm.rest.job.UserSyncJob
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1856)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1705)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:195)

`

SJobDescriptorBuilderFactory jobDescriptorBuilder = org.bonitasoft.engine.builder.BuilderFactory.get(SJobDescriptorBuilderFactory.class)
        Class<UserSyncJob> loadedClass = null;
        try
        {
            loadedClass = UserSyncJob.getClassLoader().loadClass("nz.ac.auckland.bpm.rest.job.UserSyncJob");
        }
        catch(Exception ex)
        {
            LOGGER.error("Error occurred while loading the class UserSynJob", e);
        }

        SJobDescriptor jobDescriptor = jobDescriptorBuilder.createNewInstance(loadedClass.getName(), JOB_NAME , true).done();

        //"nz.ac.auckland.bpm.rest.job.UserSyncJob"
        final List<SJobParameter> syncJobParameters = new ArrayList<>();
        syncJobParameters.add(org.bonitasoft.engine.builder.BuilderFactory.get(SJobParameterBuilderFactory.class).createNewInstance(DATA_MAP_LDAP_HOST, ldapHost).done());
        syncJobParameters.add(org.bonitasoft.engine.builder.BuilderFactory.get(SJobParameterBuilderFactory.class).createNewInstance(DATA_MAP_SECURITY_PRINCIPAL, securityPrincipal).done());
        syncJobParameters.add(org.bonitasoft.engine.builder.BuilderFactory.get(SJobParameterBuilderFactory.class).createNewInstance(DATA_MAP_SECURITY_CREDENTIAL, securityCredential).done());
        syncJobParameters.add(org.bonitasoft.engine.builder.BuilderFactory.get(SJobParameterBuilderFactory.class).createNewInstance(DATA_MAP_CONFIG, jobConfigurationList).done());
        syncJobParameters.add(org.bonitasoft.engine.builder.BuilderFactory.get(SJobParameterBuilderFactory.class).createNewInstance(DATA_MAP_EXEC_ENV, executionEnv).done());
        syncJobParameters.add(org.bonitasoft.engine.builder.BuilderFactory.get(SJobParameterBuilderFactory.class).createNewInstance(DATA_MAP_CRON, cronString).done());


        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity(JOB_BASE_TRIGGER, JOB_GROUP)
                .withSchedule(CronScheduleBuilder.cronSchedule(cronString))
                .build();

        //scheduler = getScheduler();
        final org.bonitasoft.engine.scheduler.trigger.Trigger syncJobTrigger = new UnixCronTrigger("UnixCronTrigger" + UUID.randomUUID().getLeastSignificantBits(), new Date(), cronString,
                org.bonitasoft.engine.scheduler.trigger.Trigger.MisfireRestartPolicy.NONE);

        bonitaScheduler = getBonitaSchedulerService();

        LOGGER.info("Job :'User Synchronization with LDAP' for Bonita is going to schedule [" + new Date() + "]. Cron value [" + cronString + "]")



        try
        {
            ServiceAccessorFactory.getInstance().createPlatformServiceAccessor().getTransactionService().begin();
            if(bonitaScheduler.isExistingJob(jobDescriptor.jobName) || bonitaScheduler.isStillScheduled(jobDescriptor))
            {
                bonitaScheduler.delete(jobDescriptor.jobName);
            }

            bonitaScheduler.schedule(jobDescriptor, syncJobParameters, syncJobTrigger);
            LOGGER.info("Job Scheduled. Job [" + jobDescriptor.description + "]")

        }
        catch (Exception e)
        {
            LOGGER.error("Error occurred while scheduling the Job.", e)
            ServiceAccessorFactory.getInstance().createPlatformServiceAccessor().getTransactionService().setRollbackOnly();

        }
        finally
        {
            ServiceAccessorFactory.getInstance().createPlatformServiceAccessor().getTransactionService().complete();
        }

`

Following is Job class.

`package nz.ac.auckland.bpm.rest.job;

import nz.ac.auckland.bpm.rest.config.AffiliationMapping;
import nz.ac.auckland.bpm.rest.config.JobConfiguration;
import nz.ac.auckland.bpm.rest.config.ProfileMapping;
import nz.ac.auckland.bpm.rest.util.BonitaUtil;
import nz.ac.auckland.bpm.rest.util.EnvSyncUtil;
import nz.ac.auckland.bpm.rest.util.LDAPUtil;
import org.bonitasoft.engine.events.model.SFireEventException;
import org.bonitasoft.engine.identity.Group;
import org.bonitasoft.engine.identity.MemberType;
import org.bonitasoft.engine.identity.Role;
import org.bonitasoft.engine.profile.Profile;
import org.bonitasoft.engine.scheduler.StatelessJob;
import org.bonitasoft.engine.scheduler.exception.SJobConfigurationException;
import org.bonitasoft.engine.scheduler.exception.SJobExecutionException;
import org.bonitasoft.engine.session.APISession;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class UserSyncJob implements StatelessJob
{
public static final Logger LOGGER = LoggerFactory.getLogger("nz.ac.auckland.bpm");

public List jobConfigurationList;

public UserSyncJob()
{

}

@Override
public String getName()
{
return JobManager.JOB_NAME;
}

@Override
public String getDescription()
{
return JobManager.JOB_NAME + ":LDAP - Bonita Sync";
}

@Override
public void execute() throws SJobExecutionException, SFireEventException
{
LOGGER.info("Executing the Job....");
}

@Override
public void setAttributes(Map<String, Serializable> attributes) throws SJobConfigurationException
{
LOGGER.info("Setting Attributes..... ["+ attributes +"]");
}
}

`

No answers yet.
Notifications