📄 entitysyncservices.java
字号:
/*
* $Id: EntitySyncServices.java,v 1.25 2004/02/09 22:48:43 jonesde Exp $
*
* Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
* OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package org.ofbiz.entityext.synchronization;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.ParserConfigurationException;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.UtilMisc;
import org.ofbiz.base.util.UtilValidate;
import org.ofbiz.entity.GenericDelegator;
import org.ofbiz.entity.GenericEntity;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.entity.condition.EntityCondition;
import org.ofbiz.entity.condition.EntityConditionList;
import org.ofbiz.entity.condition.EntityExpr;
import org.ofbiz.entity.condition.EntityOperator;
import org.ofbiz.entity.model.ModelEntity;
import org.ofbiz.entity.model.ModelViewEntity;
import org.ofbiz.entity.serialize.SerializeException;
import org.ofbiz.entity.serialize.XmlSerializer;
import org.ofbiz.entity.util.EntityListIterator;
import org.ofbiz.service.DispatchContext;
import org.ofbiz.service.GenericServiceException;
import org.ofbiz.service.LocalDispatcher;
import org.ofbiz.service.ModelService;
import org.ofbiz.service.ServiceUtil;
import org.xml.sax.SAXException;
/**
* Entity Engine Sync Services
*
* @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
* @version $Revision: 1.25 $
* @since 3.0
*/
public class EntitySyncServices {
public static final String module = EntitySyncServices.class.getName();
// set default split to 10 seconds, ie try not to get too much data moving over at once
public static final long defaultSyncSplitMillis = 10000;
// default to 5 minutes
public static final long syncEndBufferMillis = 300000;
/**
* Run an Entity Sync (checks to see if other already running, etc)
*@param dctx The DispatchContext that this service is operating in
*@param context Map containing the input parameters
*@return Map with the result of the service, the output parameters
*/
public static Map runEntitySync(DispatchContext dctx, Map context) {
GenericDelegator delegator = dctx.getDelegator();
LocalDispatcher dispatcher = dctx.getDispatcher();
GenericValue userLogin = (GenericValue) context.get("userLogin");
// make the last time to sync X minutes before the current time so that if this machines clock is up to that amount of time
//ahead of another machine writing to the DB it will still work fine and not lose any data
Timestamp syncEndStamp = new Timestamp(System.currentTimeMillis() - syncEndBufferMillis);
String entitySyncId = (String) context.get("entitySyncId");
Debug.logInfo("Running runEntitySync with entitySyncId=" + entitySyncId, module);
// this is the other part of the history PK, leave null until we create the history object
Timestamp startDate = null;
try {
GenericValue entitySync = delegator.findByPrimaryKey("EntitySync", UtilMisc.toMap("entitySyncId", entitySyncId));
if (entitySync == null) {
return ServiceUtil.returnError("Not running EntitySync [" + entitySyncId + "], no record found with that ID.");
}
String targetServiceName = entitySync.getString("targetServiceName");
if (UtilValidate.isEmpty(targetServiceName)) {
return ServiceUtil.returnError("Not running EntitySync [" + entitySyncId + "], no targetServiceName is specified, where do we send the data?");
}
String targetDelegatorName = entitySync.getString("targetDelegatorName");
// check to see if this sync is already running, if so return error
if ("ESR_RUNNING".equals(entitySync.getString("runStatusId"))) {
return ServiceUtil.returnError("Not running EntitySync [" + entitySyncId + "], an instance is already running.");
}
// not running, get started NOW
// set running status on entity sync, run in its own tx
Map startEntitySyncRes = dispatcher.runSync("updateEntitySyncRunning", UtilMisc.toMap("entitySyncId", entitySyncId, "runStatusId", "ESR_RUNNING", "userLogin", userLogin));
if (ModelService.RESPOND_ERROR.equals(startEntitySyncRes.get(ModelService.RESPONSE_MESSAGE))) {
return ServiceUtil.returnError("Could not start Entity Sync service, could not mark as running", null, null, startEntitySyncRes);
}
Timestamp lastSuccessfulSynchTime = entitySync.getTimestamp("lastSuccessfulSynchTime");
Timestamp currentRunStartTime = lastSuccessfulSynchTime;
Long syncSplitMillis = entitySync.getLong("syncSplitMillis");
long splitMillis = defaultSyncSplitMillis;
if (syncSplitMillis != null) {
splitMillis = syncSplitMillis.longValue();
}
List entityModelToUseList = makeEntityModelToUseList(delegator, entitySync);
// if currentRunStartTime is null, what to do? I guess iterate through all entities and find earliest tx stamp
if (currentRunStartTime == null) {
Iterator entityModelToUseIter = entityModelToUseList.iterator();
while (entityModelToUseIter.hasNext()) {
ModelEntity modelEntity = (ModelEntity) entityModelToUseIter.next();
// fields to select will be PK and the STAMP_TX_FIELD, slimmed down so we don't get a ton of data back
List fieldsToSelect = new LinkedList(modelEntity.getPkFieldNames());
// 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
fieldsToSelect.add(ModelEntity.STAMP_TX_FIELD);
EntityListIterator eli = delegator.findListIteratorByCondition(modelEntity.getEntityName(), new EntityExpr(ModelEntity.STAMP_TX_FIELD, EntityOperator.NOT_EQUAL, null), fieldsToSelect, UtilMisc.toList(ModelEntity.STAMP_TX_FIELD));
GenericValue nextValue = (GenericValue) eli.next();
eli.close();
if (nextValue != null) {
Timestamp candidateTime = nextValue.getTimestamp(ModelEntity.STAMP_TX_FIELD);
if (currentRunStartTime == null || candidateTime.before(currentRunStartTime)) {
currentRunStartTime = candidateTime;
}
}
}
if (Debug.infoOn()) Debug.logInfo("No currentRunStartTime was stored on the EntitySync record, so searched for the earliest value and got: " + currentRunStartTime, module);
}
// create history record, should run in own tx
Map initialHistoryRes = dispatcher.runSync("createEntitySyncHistory", UtilMisc.toMap("entitySyncId", entitySyncId, "runStatusId", "ESR_RUNNING", "beginningSynchTime", currentRunStartTime, "userLogin", userLogin));
if (ServiceUtil.isError(initialHistoryRes)) {
String errorMsg = "Not running EntitySync [" + entitySyncId + "], could not create EntitySyncHistory";
List errorList = new LinkedList();
saveSyncErrorInfo(entitySyncId, startDate, "ESR_DATA_ERROR", errorList, dispatcher, userLogin);
return ServiceUtil.returnError(errorMsg, errorList, null, initialHistoryRes);
}
startDate = (Timestamp) initialHistoryRes.get("startDate");
long toCreateInserted = 0;
long toCreateUpdated = 0;
long toCreateNotUpdated = 0;
long toStoreInserted = 0;
long toStoreUpdated = 0;
long toStoreNotUpdated = 0;
long toRemoveDeleted = 0;
long toRemoveAlreadyDeleted = 0;
long totalRowsToCreate = 0;
long totalRowsToStore = 0;
long totalRowsToRemove = 0;
long totalStoreCalls = 0;
long totalSplits = 0;
long perSplitMinMillis = Long.MAX_VALUE;
long perSplitMaxMillis = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -