Search in sources :

Example 1 with JobManagerException

use of org.apache.ofbiz.service.job.JobManagerException in project ofbiz-framework by apache.

the class ServiceDispatcher method runStartupServices.

// run startup services
private synchronized int runStartupServices() {
    if (!enableSvcs || jm == null) {
        return 0;
    }
    int servicesScheduled = 0;
    List<StartupService> startupServices = null;
    try {
        startupServices = ServiceConfigUtil.getServiceEngine().getStartupServices();
    } catch (GenericConfigException e) {
        Debug.logWarning(e, "Exception thrown while getting service config: ", module);
        return 0;
    }
    for (StartupService startupService : startupServices) {
        String serviceName = startupService.getName();
        String runtimeDataId = startupService.getRuntimeDataId();
        int runtimeDelay = startupService.getRuntimeDelay();
        String sendToPool = startupService.getRunInPool();
        if (UtilValidate.isEmpty(sendToPool)) {
            try {
                sendToPool = ServiceConfigUtil.getServiceEngine().getThreadPool().getSendToPool();
            } catch (GenericConfigException e) {
                Debug.logError(e, "Unable to get send pool in service [" + serviceName + "]: ", module);
            }
        }
        // current time + 1 sec delay + extended delay
        long runtime = System.currentTimeMillis() + 1000 + runtimeDelay;
        try {
            jm.schedule(sendToPool, serviceName, runtimeDataId, runtime);
        } catch (JobManagerException e) {
            Debug.logError(e, "Unable to schedule service [" + serviceName + "]", module);
        }
    }
    return servicesScheduled;
}
Also used : GenericConfigException(org.apache.ofbiz.base.config.GenericConfigException) StartupService(org.apache.ofbiz.service.config.model.StartupService) JobManagerException(org.apache.ofbiz.service.job.JobManagerException)

Example 2 with JobManagerException

use of org.apache.ofbiz.service.job.JobManagerException in project ofbiz-framework by apache.

the class GenericAsyncEngine method runAsync.

/**
 * @see org.apache.ofbiz.service.engine.GenericEngine#runAsync(java.lang.String, org.apache.ofbiz.service.ModelService, java.util.Map, org.apache.ofbiz.service.GenericRequester, boolean)
 */
public void runAsync(String localName, ModelService modelService, Map<String, Object> context, GenericRequester requester, boolean persist) throws GenericServiceException {
    DispatchContext dctx = dispatcher.getLocalContext(localName);
    Job job = null;
    if (persist) {
        // check for a delegator
        if (dispatcher.getDelegator() == null) {
            throw new GenericServiceException("No reference to delegator; cannot run persisted services.");
        }
        GenericValue jobV = null;
        // Build the value object(s).
        try {
            // Create the runtime data
            String dataId = dispatcher.getDelegator().getNextSeqId("RuntimeData");
            GenericValue runtimeData = dispatcher.getDelegator().makeValue("RuntimeData", "runtimeDataId", dataId);
            runtimeData.set("runtimeInfo", XmlSerializer.serialize(context));
            runtimeData.create();
            // Get the userLoginId out of the context
            String authUserLoginId = null;
            if (context.get("userLogin") != null) {
                GenericValue userLogin = (GenericValue) context.get("userLogin");
                authUserLoginId = userLogin.getString("userLoginId");
            }
            // Create the job info
            String jobId = dispatcher.getDelegator().getNextSeqId("JobSandbox");
            String jobName = Long.toString(System.currentTimeMillis());
            Map<String, Object> jFields = UtilMisc.toMap("jobId", jobId, "jobName", jobName, "runTime", UtilDateTime.nowTimestamp());
            jFields.put("poolId", ServiceConfigUtil.getServiceEngine().getThreadPool().getSendToPool());
            jFields.put("statusId", "SERVICE_PENDING");
            jFields.put("serviceName", modelService.name);
            jFields.put("loaderName", localName);
            jFields.put("maxRetry", Long.valueOf(modelService.maxRetry));
            jFields.put("runtimeDataId", dataId);
            if (UtilValidate.isNotEmpty(authUserLoginId)) {
                jFields.put("authUserLoginId", authUserLoginId);
            }
            jobV = dispatcher.getDelegator().makeValue("JobSandbox", jFields);
            jobV.create();
        } catch (GenericEntityException e) {
            throw new GenericServiceException("Unable to create persisted job", e);
        } catch (SerializeException e) {
            throw new GenericServiceException("Problem serializing service attributes", e);
        } catch (FileNotFoundException e) {
            throw new GenericServiceException("Problem serializing service attributes", e);
        } catch (IOException e) {
            throw new GenericServiceException("Problem serializing service attributes", e);
        } catch (GenericConfigException e) {
            throw new GenericServiceException("Problem serializing service attributes", e);
        }
        Debug.logInfo("Persisted job queued : " + jobV.getString("jobName"), module);
    } else {
        JobManager jMgr = dispatcher.getJobManager();
        if (jMgr != null) {
            String name = Long.toString(System.currentTimeMillis());
            String jobId = modelService.name + "." + name;
            job = new GenericServiceJob(dctx, jobId, name, modelService.name, context, requester);
            try {
                dispatcher.getJobManager().runJob(job);
            } catch (JobManagerException jse) {
                throw new GenericServiceException("Cannot run job.", jse);
            }
        } else {
            throw new GenericServiceException("Cannot get JobManager instance to invoke the job");
        }
    }
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) GenericServiceJob(org.apache.ofbiz.service.job.GenericServiceJob) SerializeException(org.apache.ofbiz.entity.serialize.SerializeException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) JobManager(org.apache.ofbiz.service.job.JobManager) DispatchContext(org.apache.ofbiz.service.DispatchContext) GenericConfigException(org.apache.ofbiz.base.config.GenericConfigException) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) JobManagerException(org.apache.ofbiz.service.job.JobManagerException) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) Job(org.apache.ofbiz.service.job.Job) GenericServiceJob(org.apache.ofbiz.service.job.GenericServiceJob)

Example 3 with JobManagerException

use of org.apache.ofbiz.service.job.JobManagerException in project ofbiz-framework by apache.

the class GenericAbstractDispatcher method schedule.

/**
 * @see org.apache.ofbiz.service.LocalDispatcher#schedule(java.lang.String, java.lang.String, java.lang.String, java.util.Map, long, int, int, int, long, int)
 */
public void schedule(String jobName, String poolName, String serviceName, Map<String, ? extends Object> context, long startTime, int frequency, int interval, int count, long endTime, int maxRetry) throws GenericServiceException {
    Transaction suspendedTransaction = null;
    try {
        boolean beganTransaction = false;
        suspendedTransaction = TransactionUtil.suspend();
        try {
            beganTransaction = TransactionUtil.begin();
            try {
                getJobManager().schedule(jobName, poolName, serviceName, context, startTime, frequency, interval, count, endTime, maxRetry);
                if (Debug.verboseOn()) {
                    Debug.logVerbose("[LocalDispatcher.schedule] : Current time : " + (new Date()).getTime(), module);
                    Debug.logVerbose("[LocalDispatcher.schedule] : Runtime      : " + startTime, module);
                    Debug.logVerbose("[LocalDispatcher.schedule] : Frequency    : " + frequency, module);
                    Debug.logVerbose("[LocalDispatcher.schedule] : Interval     : " + interval, module);
                    Debug.logVerbose("[LocalDispatcher.schedule] : Count        : " + count, module);
                    Debug.logVerbose("[LocalDispatcher.schedule] : EndTime      : " + endTime, module);
                    Debug.logVerbose("[LocalDispatcher.schedule] : MazRetry     : " + maxRetry, module);
                }
            } catch (JobManagerException jme) {
                throw new GenericServiceException(jme.getMessage(), jme);
            }
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            String errMsg = "General error while scheduling job";
            Debug.logError(e, errMsg, module);
            try {
                TransactionUtil.rollback(beganTransaction, errMsg, e);
            } catch (GenericTransactionException gte1) {
                Debug.logError(gte1, "Unable to rollback transaction", module);
            }
        } finally {
            try {
                TransactionUtil.commit(beganTransaction);
            } catch (GenericTransactionException gte2) {
                Debug.logError(gte2, "Unable to commit scheduled job", module);
            }
        }
    } catch (GenericTransactionException gte) {
        Debug.logError(gte, "Error suspending transaction while scheduling job", module);
    } finally {
        if (suspendedTransaction != null) {
            try {
                TransactionUtil.resume(suspendedTransaction);
            } catch (GenericTransactionException gte3) {
                Debug.logError(gte3, "Error resuming suspended transaction after scheduling job", module);
            }
        }
    }
}
Also used : Transaction(javax.transaction.Transaction) JobManagerException(org.apache.ofbiz.service.job.JobManagerException) GenericTransactionException(org.apache.ofbiz.entity.transaction.GenericTransactionException) Date(java.util.Date) GenericTransactionException(org.apache.ofbiz.entity.transaction.GenericTransactionException) JobManagerException(org.apache.ofbiz.service.job.JobManagerException)

Aggregations

JobManagerException (org.apache.ofbiz.service.job.JobManagerException)3 GenericConfigException (org.apache.ofbiz.base.config.GenericConfigException)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 Date (java.util.Date)1 Transaction (javax.transaction.Transaction)1 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)1 GenericValue (org.apache.ofbiz.entity.GenericValue)1 SerializeException (org.apache.ofbiz.entity.serialize.SerializeException)1 GenericTransactionException (org.apache.ofbiz.entity.transaction.GenericTransactionException)1 DispatchContext (org.apache.ofbiz.service.DispatchContext)1 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)1 StartupService (org.apache.ofbiz.service.config.model.StartupService)1 GenericServiceJob (org.apache.ofbiz.service.job.GenericServiceJob)1 Job (org.apache.ofbiz.service.job.Job)1 JobManager (org.apache.ofbiz.service.job.JobManager)1