Search in sources :

Example 1 with SerializeException

use of org.apache.ofbiz.entity.serialize.SerializeException in project ofbiz-framework by apache.

the class EntitySyncContext method assembleKeysToRemove.

public LinkedList<GenericEntity> assembleKeysToRemove() throws SyncDataErrorException {
    // get all removed items from the given time range, add to list for those
    LinkedList<GenericEntity> keysToRemove = new LinkedList<GenericEntity>();
    if (this.nextRemoveTxTime != null && (this.nextRemoveTxTime.equals(currentRunEndTime) || this.nextRemoveTxTime.after(currentRunEndTime))) {
        // this means that for all entities in this pack we found on the last pass that there would be nothing for this one, so just return nothing...
        return keysToRemove;
    }
    // Debug.logInfo("Getting keys to remove; currentRunStartTime=" + currentRunStartTime + ", currentRunEndTime=" + currentRunEndTime, module);
    boolean beganTransaction = false;
    try {
        beganTransaction = TransactionUtil.begin(7200);
    } catch (GenericTransactionException e) {
        throw new SyncDataErrorException("Unable to begin JTA transaction", e);
    }
    try {
        // find all instances of this entity with the STAMP_TX_FIELD != null, sort ascending to get lowest/oldest value first, then grab first and consider as candidate currentRunStartTime
        EntityCondition findValCondition = EntityCondition.makeCondition(EntityCondition.makeCondition(ModelEntity.STAMP_TX_FIELD, EntityOperator.GREATER_THAN_EQUAL_TO, currentRunStartTime), EntityCondition.makeCondition(ModelEntity.STAMP_TX_FIELD, EntityOperator.LESS_THAN, currentRunEndTime));
        EntityListIterator removeEli = EntityQuery.use(delegator).from("EntitySyncRemove").where(findValCondition).orderBy(ModelEntity.STAMP_TX_FIELD, ModelEntity.STAMP_FIELD).queryIterator();
        GenericValue entitySyncRemove = null;
        while ((entitySyncRemove = removeEli.next()) != null) {
            // pull the PK from the EntitySyncRemove in the primaryKeyRemoved field, de-XML-serialize it
            String primaryKeyRemoved = entitySyncRemove.getString("primaryKeyRemoved");
            GenericEntity pkToRemove = null;
            try {
                pkToRemove = (GenericEntity) XmlSerializer.deserialize(primaryKeyRemoved, delegator);
            } catch (IOException e) {
                String errorMsg = "Error deserializing GenericPK to remove in Entity Sync Data for entitySyncId [" + entitySyncId + "] and entitySyncRemoveId [" + entitySyncRemove.getString("entitySyncRemoveId") + "]: " + e.toString();
                Debug.logError(e, errorMsg, module);
                throw new SyncDataErrorException(errorMsg, e);
            } catch (SAXException e) {
                String errorMsg = "Error deserializing GenericPK to remove in Entity Sync Data for entitySyncId [" + entitySyncId + "] and entitySyncRemoveId [" + entitySyncRemove.getString("entitySyncRemoveId") + "]: " + e.toString();
                Debug.logError(e, errorMsg, module);
                throw new SyncDataErrorException(errorMsg, e);
            } catch (ParserConfigurationException e) {
                String errorMsg = "Error deserializing GenericPK to remove in Entity Sync Data for entitySyncId [" + entitySyncId + "] and entitySyncRemoveId [" + entitySyncRemove.getString("entitySyncRemoveId") + "]: " + e.toString();
                Debug.logError(e, errorMsg, module);
                throw new SyncDataErrorException(errorMsg, e);
            } catch (SerializeException e) {
                String errorMsg = "Error deserializing GenericPK to remove in Entity Sync Data for entitySyncId [" + entitySyncId + "] and entitySyncRemoveId [" + entitySyncRemove.getString("entitySyncRemoveId") + "]: " + e.toString();
                Debug.logError(e, errorMsg, module);
                throw new SyncDataErrorException(errorMsg, e);
            }
            // set the stamp fields for future reference
            pkToRemove.set(ModelEntity.STAMP_TX_FIELD, entitySyncRemove.get(ModelEntity.STAMP_TX_FIELD));
            pkToRemove.set(ModelEntity.STAMP_FIELD, entitySyncRemove.get(ModelEntity.STAMP_FIELD));
            pkToRemove.set(ModelEntity.CREATE_STAMP_TX_FIELD, entitySyncRemove.get(ModelEntity.CREATE_STAMP_TX_FIELD));
            pkToRemove.set(ModelEntity.CREATE_STAMP_FIELD, entitySyncRemove.get(ModelEntity.CREATE_STAMP_FIELD));
            if (this.entityNameToUseSet.contains(pkToRemove.getEntityName())) {
                keysToRemove.add(pkToRemove);
            }
        }
        removeEli.close();
        // if we didn't find anything for this entity, find the next value's Timestamp and keep track of it
        if (keysToRemove.size() == 0) {
            EntityCondition findNextCondition = EntityCondition.makeCondition(ModelEntity.STAMP_TX_FIELD, EntityOperator.GREATER_THAN_EQUAL_TO, currentRunEndTime);
            EntityListIterator eliNext = EntityQuery.use(delegator).from("EntitySyncRemove").where(findNextCondition).orderBy(ModelEntity.STAMP_TX_FIELD).queryIterator();
            // get the first element and it's tx time value...
            GenericValue firstVal = eliNext.next();
            eliNext.close();
            if (firstVal != null) {
                Timestamp nextTxTime = firstVal.getTimestamp(ModelEntity.STAMP_TX_FIELD);
                if (this.nextRemoveTxTime == null || nextTxTime.before(this.nextRemoveTxTime)) {
                    this.nextRemoveTxTime = nextTxTime;
                }
            }
        }
    } catch (GenericEntityException e) {
        try {
            TransactionUtil.rollback(beganTransaction, "Entity Engine error in assembleKeysToRemove", e);
        } catch (GenericTransactionException e2) {
            Debug.logWarning(e2, "Unable to call rollback()", module);
        }
        throw new SyncDataErrorException("Error getting keys to remove from the datasource", e);
    } catch (Throwable t) {
        try {
            TransactionUtil.rollback(beganTransaction, "General error in assembleKeysToRemove", t);
        } catch (GenericTransactionException e2) {
            Debug.logWarning(e2, "Unable to call rollback()", module);
        }
        throw new SyncDataErrorException("Caught runtime error while getting keys to remove", t);
    }
    try {
        TransactionUtil.commit(beganTransaction);
    } catch (GenericTransactionException e) {
        throw new SyncDataErrorException("Commit transaction failed", e);
    }
    // TEST SECTION: leave false for normal use
    boolean logValues = false;
    if (logValues && keysToRemove.size() > 0) {
        StringBuilder toRemoveInfo = new StringBuilder();
        for (GenericEntity keyToRemove : keysToRemove) {
            toRemoveInfo.append("\n-->[");
            toRemoveInfo.append(keyToRemove.get(ModelEntity.STAMP_TX_FIELD));
            toRemoveInfo.append(":");
            toRemoveInfo.append(keyToRemove.get(ModelEntity.STAMP_FIELD));
            toRemoveInfo.append("] ");
            toRemoveInfo.append(keyToRemove);
        }
        Debug.logInfo(toRemoveInfo.toString(), module);
    }
    // this calculation is false, so it needs to be nullified
    if (keysToRemove.size() > 0) {
        this.nextRemoveTxTime = null;
    }
    return keysToRemove;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) SerializeException(org.apache.ofbiz.entity.serialize.SerializeException) EntityCondition(org.apache.ofbiz.entity.condition.EntityCondition) IOException(java.io.IOException) Timestamp(java.sql.Timestamp) LinkedList(java.util.LinkedList) SAXException(org.xml.sax.SAXException) GenericEntity(org.apache.ofbiz.entity.GenericEntity) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) GenericTransactionException(org.apache.ofbiz.entity.transaction.GenericTransactionException) EntityListIterator(org.apache.ofbiz.entity.util.EntityListIterator) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 2 with SerializeException

use of org.apache.ofbiz.entity.serialize.SerializeException 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 SerializeException

use of org.apache.ofbiz.entity.serialize.SerializeException in project ofbiz-framework by apache.

the class JmsServiceEngine method makeMessage.

protected Message makeMessage(Session session, ModelService modelService, Map<String, Object> context) throws GenericServiceException, JMSException {
    List<String> outParams = modelService.getParameterNames(ModelService.OUT_PARAM, false);
    if (UtilValidate.isNotEmpty(outParams))
        throw new GenericServiceException("JMS service cannot have required OUT parameters; no parameters will be returned.");
    String xmlContext = null;
    try {
        if (Debug.verboseOn())
            Debug.logVerbose("Serializing Context --> " + context, module);
        xmlContext = JmsSerializer.serialize(context);
    } catch (SerializeException | IOException e) {
        throw new GenericServiceException("Cannot serialize context.", e);
    }
    MapMessage message = session.createMapMessage();
    message.setString("serviceName", modelService.invoke);
    message.setString("serviceContext", xmlContext);
    return message;
}
Also used : SerializeException(org.apache.ofbiz.entity.serialize.SerializeException) MapMessage(javax.jms.MapMessage) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) IOException(java.io.IOException)

Example 4 with SerializeException

use of org.apache.ofbiz.entity.serialize.SerializeException in project ofbiz-framework by apache.

the class EntitySyncServices method runOfflineEntitySync.

public static Map<String, Object> runOfflineEntitySync(DispatchContext dctx, Map<String, ? extends Object> context) {
    String fileName = (String) context.get("fileName");
    EntitySyncContext esc = null;
    long totalRowsExported = 0;
    try {
        esc = new EntitySyncContext(dctx, context);
        Debug.logInfo("Doing runManualEntitySync for entitySyncId=" + esc.entitySyncId + ", currentRunStartTime=" + esc.currentRunStartTime + ", currentRunEndTime=" + esc.currentRunEndTime, module);
        Document mainDoc = UtilXml.makeEmptyXmlDocument("xml-entity-synchronization");
        Element docElement = mainDoc.getDocumentElement();
        docElement.setAttribute("xml:lang", "en-US");
        esc.runOfflineStartRunning();
        // increment starting time to run until now
        // just run this the first time, will be updated between each loop automatically
        esc.setSplitStartTime();
        while (esc.hasMoreTimeToSync()) {
            esc.totalSplits++;
            ArrayList<GenericValue> valuesToCreate = esc.assembleValuesToCreate();
            ArrayList<GenericValue> valuesToStore = esc.assembleValuesToStore();
            List<GenericEntity> keysToRemove = esc.assembleKeysToRemove();
            long currentRows = esc.setTotalRowCounts(valuesToCreate, valuesToStore, keysToRemove);
            totalRowsExported += currentRows;
            if (currentRows > 0) {
                // create the XML document
                Element syncElement = UtilXml.addChildElement(docElement, "entity-sync", mainDoc);
                syncElement.setAttribute("entitySyncId", esc.entitySyncId);
                syncElement.setAttribute("lastSuccessfulSynchTime", esc.currentRunEndTime.toString());
                // serialize the list data for XML storage
                try {
                    UtilXml.addChildElementValue(syncElement, "values-to-create", XmlSerializer.serialize(valuesToCreate), mainDoc);
                    UtilXml.addChildElementValue(syncElement, "values-to-store", XmlSerializer.serialize(valuesToStore), mainDoc);
                    UtilXml.addChildElementValue(syncElement, "keys-to-remove", XmlSerializer.serialize(keysToRemove), mainDoc);
                } catch (SerializeException e) {
                    throw new EntitySyncContext.SyncOtherErrorException("List serialization problem", e);
                } catch (IOException e) {
                    throw new EntitySyncContext.SyncOtherErrorException("XML writing problem", e);
                }
            }
            // update the result info
            esc.runSaveOfflineSyncInfo(currentRows);
            esc.advanceRunTimes();
        }
        if (totalRowsExported > 0) {
            // check the file name; use a default if none is passed in
            if (UtilValidate.isEmpty(fileName)) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
                fileName = "offline_entitySync-" + esc.entitySyncId + "-" + sdf.format(new Date()) + ".xml";
            }
            // write the XML file
            try {
                UtilXml.writeXmlDocument(fileName, mainDoc);
            } catch (java.io.FileNotFoundException e) {
                throw new EntitySyncContext.SyncOtherErrorException(e);
            } catch (java.io.IOException e) {
                throw new EntitySyncContext.SyncOtherErrorException(e);
            }
        } else {
            Debug.logInfo("No rows to write; no data exported.", module);
        }
        // save the final results
        esc.saveFinalSyncResults();
    } catch (SyncAbortException e) {
        return e.returnError(module);
    } catch (SyncErrorException e) {
        e.saveSyncErrorInfo(esc);
        return e.returnError(module);
    }
    return ServiceUtil.returnSuccess();
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) SerializeException(org.apache.ofbiz.entity.serialize.SerializeException) Element(org.w3c.dom.Element) SyncAbortException(org.apache.ofbiz.entityext.synchronization.EntitySyncContext.SyncAbortException) IOException(java.io.IOException) IOException(java.io.IOException) Document(org.w3c.dom.Document) Date(java.util.Date) GenericEntity(org.apache.ofbiz.entity.GenericEntity) SyncErrorException(org.apache.ofbiz.entityext.synchronization.EntitySyncContext.SyncErrorException) SimpleDateFormat(java.text.SimpleDateFormat)

Example 5 with SerializeException

use of org.apache.ofbiz.entity.serialize.SerializeException in project ofbiz-framework by apache.

the class JobManager method schedule.

/**
 * Schedule a job to start at a specific time with specific recurrence info
 *
 * @param jobName
 *            The name of the job
 *@param poolName
 *            The name of the pool to run the service from
 *@param serviceName
 *            The name of the service to invoke
 *@param context
 *            The context for the service
 *@param startTime
 *            The time in milliseconds the service should run
 *@param frequency
 *            The frequency of the recurrence (HOURLY,DAILY,MONTHLY,etc)
 *@param interval
 *            The interval of the frequency recurrence
 *@param count
 *            The number of times to repeat
 *@param endTime
 *            The time in milliseconds the service should expire
 *@param maxRetry
 *            The max number of retries on failure (-1 for no max)
 */
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 JobManagerException {
    // persist the context
    String dataId = null;
    try {
        GenericValue runtimeData = delegator.makeValue("RuntimeData");
        runtimeData.set("runtimeInfo", XmlSerializer.serialize(context));
        runtimeData = delegator.createSetNextSeqId(runtimeData);
        dataId = runtimeData.getString("runtimeDataId");
    } catch (GenericEntityException | SerializeException | IOException e) {
        throw new JobManagerException(e.getMessage(), e);
    }
    // schedule the job
    schedule(jobName, poolName, serviceName, dataId, startTime, frequency, interval, count, endTime, maxRetry);
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) SerializeException(org.apache.ofbiz.entity.serialize.SerializeException) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)5 SerializeException (org.apache.ofbiz.entity.serialize.SerializeException)5 GenericValue (org.apache.ofbiz.entity.GenericValue)4 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)3 GenericEntity (org.apache.ofbiz.entity.GenericEntity)2 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)2 FileNotFoundException (java.io.FileNotFoundException)1 Timestamp (java.sql.Timestamp)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 LinkedList (java.util.LinkedList)1 MapMessage (javax.jms.MapMessage)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 GenericConfigException (org.apache.ofbiz.base.config.GenericConfigException)1 EntityCondition (org.apache.ofbiz.entity.condition.EntityCondition)1 GenericTransactionException (org.apache.ofbiz.entity.transaction.GenericTransactionException)1 EntityListIterator (org.apache.ofbiz.entity.util.EntityListIterator)1 SyncAbortException (org.apache.ofbiz.entityext.synchronization.EntitySyncContext.SyncAbortException)1 SyncErrorException (org.apache.ofbiz.entityext.synchronization.EntitySyncContext.SyncErrorException)1 DispatchContext (org.apache.ofbiz.service.DispatchContext)1