Search in sources :

Example 1 with SmscStatProvider

use of org.mobicents.smsc.domain.SmscStatProvider in project smscgateway by RestComm.

the class SchedulerResourceAdaptor method onTimerTick.

// /////////////////
// Helper methods //
// /////////////////
protected void onTimerTick() {
    // checking if all SBBs are already running
    if (!SbbStates.isAllServicesUp()) {
        String s1 = SbbStates.getServicesDownList();
        if (!s1.equals(shownServicesDownList) || System.currentTimeMillis() - shownServicesDownTime > 10000) {
            // we are displaying this message every 10 seconds or if ServicesDownList has changed
            this.tracer.info("Not all SBB are running now: " + s1);
            shownServicesDownList = s1;
            shownServicesDownTime = System.currentTimeMillis();
        }
        return;
    }
    try {
        // garbageCollectionTime
        Date current = new Date(new Date().getTime() - 1000 * 60);
        if (garbageCollectionTime.before(current)) {
            garbageCollectionTime = new Date();
            SmsSetCache.getInstance().garbadeCollectProcessingSmsSet();
        }
        // checking if cassandra database is available
        if (!dbOperations_C2.isDatabaseAvailable())
            return;
        // checking if we need to advance dueSlot
        SmscPropertiesManagement smscPropertiesManagement = SmscPropertiesManagement.getInstance();
        int val = smscPropertiesManagement.getSkipUnsentMessages();
        if (val >= 0) {
            smscPropertiesManagement.setSkipUnsentMessages(-1);
            // if (smscPropertiesManagement.getDatabaseType() == DatabaseType.Cassandra_1) {
            // } else {
            long processedDueSlot = dbOperations_C2.c2_getCurrentDueSlot();
            Date processedTime = dbOperations_C2.c2_getTimeForDueSlot(processedDueSlot);
            long possibleDueSlot = dbOperations_C2.c2_getIntimeDueSlot();
            // we do a possible time 10 seconds before the time that is possible for running
            Date possibleTime = new Date(dbOperations_C2.c2_getTimeForDueSlot(possibleDueSlot).getTime() - 10000);
            Date newTime = new Date(possibleTime.getTime() - val * 1000);
            if (newTime.getTime() > processedTime.getTime()) {
                long newDueSlot = dbOperations_C2.c2_getDueSlotForTime(newTime);
                dbOperations_C2.c2_setCurrentDueSlot(newDueSlot);
                this.tracer.warning("currentDueSlot has been changed after a user request: from " + processedTime + " to " + newTime);
            } else {
                this.tracer.warning("There was a user request to change currentDueSlot: from " + processedTime + " to " + newTime + ". But the new time must be after the current one and the request was rejected.");
            }
        // }
        }
        // checking if SmsRouteManagement is already started
        SmsRouteManagement smsRouteManagement = SmsRouteManagement.getInstance();
        if (smsRouteManagement.getSmsRoutingRule() == null)
            return;
        OneWaySmsSetCollection schedulableSms;
        int maxCnt;
        int fetchMaxRows = smscPropertiesManagement.getFetchMaxRows();
        int activityCount = SmsSetCache.getInstance().getProcessingSmsSetSize();
        int fetchAvailRows = smscPropertiesManagement.getMaxActivityCount() - activityCount;
        maxCnt = Math.min(fetchMaxRows, fetchAvailRows);
        if (fetchAvailRows <= 0) {
            smscCongestionControl.registerMaxActivityCount1_0Threshold();
        } else {
            smscCongestionControl.registerMaxActivityCount1_0BackToNormal();
        }
        if (savedOneWaySmsSetCollection != null && savedOneWaySmsSetCollection.size() > 0) {
            schedulableSms = savedOneWaySmsSetCollection;
        } else {
            // checking if SMSC is inactivated by SLEE
            if (smscPropertiesManagement.isSmscStopped())
                return;
            // checking if SMSC is paused by a used
            if (smscPropertiesManagement.isDeliveryPause())
                return;
            try {
                if (this.tracer.isFineEnabled())
                    this.tracer.fine("Fetching: Starting fetching messages from database: fetchMaxRows=" + fetchMaxRows + ", activityCount=" + activityCount + ", fetchAvailRows=" + fetchAvailRows);
                if (maxCnt <= 0)
                    return;
                int readTryCount = 0;
                while (true) {
                    schedulableSms = this.fetchSchedulable(maxCnt);
                    int cnt = 0;
                    if (schedulableSms != null)
                        cnt = schedulableSms.size();
                    readTryCount++;
                    if (cnt == 0 && readTryCount < 100)
                        // we will 100 times reread new empty due_Slot
                        continue;
                    if (this.tracer.isFineEnabled()) {
                        String s1 = "Fetching: Fetched " + cnt + " messages (max requested messages=" + maxCnt + ", fetched messages=" + cnt + ")";
                        this.tracer.fine(s1);
                    }
                    break;
                }
            } catch (PersistenceException e1) {
                this.tracer.severe("PersistenceException when fetching SmsSet list from a database: " + e1.getMessage(), e1);
                Throwable ex = e1.getCause();
                if (ex != null)
                    this.tracer.severe("Original error: " + ex.getMessage(), ex);
                return;
            }
        }
        int count = 0;
        Date curDate = new Date();
        try {
            while (true) {
                SmsSet smsSet = schedulableSms.next();
                if (smsSet == null)
                    break;
                try {
                    if (!smsSet.isProcessingStarted()) {
                        smsSet.setProcessingStarted();
                        if (!doInjectSmsDatabase(smsSet, curDate, false)) {
                            return;
                        }
                    }
                } catch (Exception e) {
                    this.tracer.severe("Exception when injectSms: " + e.getMessage(), e);
                }
                count++;
                if (count >= maxCnt) {
                    savedOneWaySmsSetCollection = schedulableSms;
                    break;
                }
            }
        } finally {
            if (count > 0) {
                SmscStatProvider smscStatProvider = SmscStatProvider.getInstance();
                smscStatProvider.setMessageScheduledTotal(smscStatProvider.getMessageScheduledTotal() + count);
                if (this.tracer.isInfoEnabled()) {
                    String s2 = "Fetching: Scheduled '" + count + "' out of '" + schedulableSms.size() + ", fetchMaxRows=" + fetchMaxRows + ", activityCount=" + activityCount + ", fetchAvailRows=" + fetchAvailRows + "'.";
                    this.tracer.info(s2);
                }
            } else {
                if (this.tracer.isFineEnabled()) {
                    String s2 = "Fetching: Scheduled '" + count + "' out of '" + schedulableSms.size() + ", fetchMaxRows=" + fetchMaxRows + ", activityCount=" + activityCount + ", fetchAvailRows=" + fetchAvailRows + "'.";
                    this.tracer.fine(s2);
                }
            }
        }
    } catch (Throwable e1) {
        this.tracer.severe("Exception in SchedulerResourceAdaptor when fetching records and issuing events: " + e1.getMessage(), e1);
    }
    // stat update
    SmscPropertiesManagement smscPropertiesManagement = SmscPropertiesManagement.getInstance();
    // if (smscPropertiesManagement.getDatabaseType() == DatabaseType.Cassandra_1) {
    // } else {
    long processedDueSlot = dbOperations_C2.c2_getCurrentDueSlot();
    long possibleDueSlot = dbOperations_C2.c2_getIntimeDueSlot();
    Date processedDate = dbOperations_C2.c2_getTimeForDueSlot(processedDueSlot);
    Date possibleDate = dbOperations_C2.c2_getTimeForDueSlot(possibleDueSlot);
    int lag = (int) ((possibleDate.getTime() - processedDate.getTime()) / 1000);
    smscStatAggregator.updateSmscDeliveringLag(lag);
// }
}
Also used : SmscPropertiesManagement(org.mobicents.smsc.domain.SmscPropertiesManagement) PersistenceException(org.mobicents.smsc.cassandra.PersistenceException) SmscStatProvider(org.mobicents.smsc.domain.SmscStatProvider) Date(java.util.Date) SleeEndpoint(javax.slee.resource.SleeEndpoint) SmsSet(org.mobicents.smsc.library.SmsSet) RollbackException(javax.transaction.RollbackException) PersistenceException(org.mobicents.smsc.cassandra.PersistenceException) HeuristicRollbackException(javax.transaction.HeuristicRollbackException) NotSupportedException(javax.transaction.NotSupportedException) SystemException(javax.transaction.SystemException) InvalidConfigurationException(javax.slee.resource.InvalidConfigurationException) HeuristicMixedException(javax.transaction.HeuristicMixedException) SmsRouteManagement(org.mobicents.smsc.domain.SmsRouteManagement)

Aggregations

Date (java.util.Date)1 InvalidConfigurationException (javax.slee.resource.InvalidConfigurationException)1 SleeEndpoint (javax.slee.resource.SleeEndpoint)1 HeuristicMixedException (javax.transaction.HeuristicMixedException)1 HeuristicRollbackException (javax.transaction.HeuristicRollbackException)1 NotSupportedException (javax.transaction.NotSupportedException)1 RollbackException (javax.transaction.RollbackException)1 SystemException (javax.transaction.SystemException)1 PersistenceException (org.mobicents.smsc.cassandra.PersistenceException)1 SmsRouteManagement (org.mobicents.smsc.domain.SmsRouteManagement)1 SmscPropertiesManagement (org.mobicents.smsc.domain.SmscPropertiesManagement)1 SmscStatProvider (org.mobicents.smsc.domain.SmscStatProvider)1 SmsSet (org.mobicents.smsc.library.SmsSet)1