📄 entitysynccontext.java
字号:
/* * $Id: EntitySyncContext.java 5462 2005-08-05 18:35:48Z jonesde $ * * Copyright (c) 2001-2005 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.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.LinkedList;import java.util.List;import java.util.Map;import java.util.Set;import javax.xml.parsers.ParserConfigurationException;import org.ofbiz.base.util.Debug;import org.ofbiz.base.util.UtilDateTime;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.transaction.GenericTransactionException;import org.ofbiz.entity.transaction.TransactionUtil;import org.ofbiz.entity.util.EntityListIterator;import org.ofbiz.service.DispatchContext;import org.ofbiz.service.GeneralServiceException;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> * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a> * @version $Rev: 5462 $ * @since 3.0 */public class EntitySyncContext { public static final String module = EntitySyncContext.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 offline split is 30 minutes public static final long defaultOfflineSyncSplitMillis = 1800000; // default to 5 minutes public static final long defaultSyncEndBufferMillis = 300000; // default to 2 hours, 120m, 7200s public static final long defaultMaxRunningNoUpdateMillis = 7200000; public GenericDelegator delegator; public LocalDispatcher dispatcher; public Map context; public GenericValue userLogin; public boolean isOfflineSync = false; public String entitySyncId; public GenericValue entitySync; public String targetServiceName; public String targetDelegatorName; public Timestamp syncEndStamp; public long offlineSyncSplitMillis = defaultOfflineSyncSplitMillis; public long syncSplitMillis = defaultSyncSplitMillis; public long syncEndBufferMillis = defaultSyncEndBufferMillis; public long maxRunningNoUpdateMillis = defaultMaxRunningNoUpdateMillis; public Timestamp lastSuccessfulSynchTime; public List entityModelToUseList; public Set entityNameToUseSet; public Timestamp currentRunStartTime; public Timestamp currentRunEndTime; // these values are used to make this more efficient; if we run into an entity that has 0 //results for a given time block, we will do a query to find the next create/update/remove //time for that entity, and also keep track of a global next with the lowest future next value; //using these we can skip a lot of queries and speed this up significantly public Map nextEntityCreateTxTime = new HashMap(); public Map nextEntityUpdateTxTime = new HashMap(); public Timestamp nextCreateTxTime = null; public Timestamp nextUpdateTxTime = null; public Timestamp nextRemoveTxTime = null; // this is the other part of the history PK, leave null until we create the history object public Timestamp startDate = null; 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 totalRowsExported = 0; long totalRowsToCreate = 0; long totalRowsToStore = 0; long totalRowsToRemove = 0; long totalRowsPerSplit = 0; long totalStoreCalls = 0; long totalSplits = 0; long perSplitMinMillis = Long.MAX_VALUE; long perSplitMaxMillis = 0; long perSplitMinItems = Long.MAX_VALUE; long perSplitMaxItems = 0; long splitStartTime = 0; public EntitySyncContext(DispatchContext dctx, Map context) throws SyncDataErrorException, SyncAbortException { this.context = context; this.dispatcher = dctx.getDispatcher(); this.delegator = dctx.getDelegator(); // what to do with the delegatorName? this is the delegatorName to use in this service... String delegatorName = (String) context.get("delegatorName"); if (UtilValidate.isNotEmpty(delegatorName)) { this.delegator = GenericDelegator.getGenericDelegator(delegatorName); } this.userLogin = (GenericValue) context.get("userLogin"); this.entitySyncId = (String) context.get("entitySyncId"); Debug.logInfo("Creating EntitySyncContext with entitySyncId=" + entitySyncId, module); boolean beganTransaction = false; try { beganTransaction = TransactionUtil.begin(7200); } catch (GenericTransactionException e) { throw new SyncDataErrorException("Unable to begin JTA transaction", e); } try { this.entitySync = delegator.findByPrimaryKey("EntitySync", UtilMisc.toMap("entitySyncId", this.entitySyncId)); if (this.entitySync == null) { throw new SyncAbortException("Not running EntitySync [" + entitySyncId + "], no record found with that ID."); } targetServiceName = entitySync.getString("targetServiceName"); targetDelegatorName = entitySync.getString("targetDelegatorName"); // 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 syncEndStamp = new Timestamp(System.currentTimeMillis() - syncEndBufferMillis); this.offlineSyncSplitMillis = getOfflineSyncSplitMillis(entitySync); this.syncSplitMillis = getSyncSplitMillis(entitySync); this.syncEndBufferMillis = getSyncEndBufferMillis(entitySync); this.maxRunningNoUpdateMillis = getMaxRunningNoUpdateMillis(entitySync); this.lastSuccessfulSynchTime = entitySync.getTimestamp("lastSuccessfulSynchTime"); this.entityModelToUseList = this.makeEntityModelToUseList(); this.entityNameToUseSet = this.makeEntityNameToUseSet(); // set start and end times for the first/current pass this.currentRunStartTime = getCurrentRunStartTime(lastSuccessfulSynchTime, entityModelToUseList, delegator); this.setCurrentRunEndTime(); // this is mostly for the pull side... will always be null for at the beginning of a push process, to be filled in later this.startDate = (Timestamp) context.get("startDate"); } catch (GenericEntityException e) { try { TransactionUtil.rollback(beganTransaction, "Entity Engine error while getting Entity Sync init information", e); } catch (GenericTransactionException e2) { Debug.logWarning(e2, "Unable to call rollback()", module); } throw new SyncDataErrorException("Error initializing EntitySync Context", e); } try { TransactionUtil.commit(beganTransaction); } catch (GenericTransactionException e) { throw new SyncDataErrorException("Unable to commit transaction", e); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -