Java tutorial
/******************************************************************************* * Copyright 2012 Persistent Systems Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. ******************************************************************************/ package com.persistent.cloudninja.scheduler; import java.net.URISyntaxException; import java.security.InvalidKeyException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.TimeZone; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.util.StopWatch; import com.microsoft.windowsazure.services.core.storage.StorageException; import com.persistent.cloudninja.dao.MeteringDao; import com.persistent.cloudninja.dao.PartitionStatsAndBWUsageDao; import com.persistent.cloudninja.dao.TaskCompletionDao; import com.persistent.cloudninja.domainobject.BandwidthUsageDirection; import com.persistent.cloudninja.domainobject.DBBandwidthUsageEntity; import com.persistent.cloudninja.domainobject.MeteringEntity; import com.persistent.cloudninja.utils.SchedulerSettings; import com.persistent.cloudninja.utils.StorageClientUtility; /** * Processor for tenant DB bandwidth usage task. It processes the * messages generated by generator. */ @Component public class TenantDBBandwidthProcessor extends TaskActivityBase { @Autowired private TaskCompletionDao taskCompletionDao; private static final Logger LOGGER = Logger.getLogger(TenantDBBandwidthProcessor.class); @Autowired private PartitionStatsAndBWUsageDao partitionStatsAndBWUsageDao; @Autowired private MeteringDao meteringDao; public TenantDBBandwidthProcessor() throws InvalidKeyException, URISyntaxException { //tenantdbbandwidthqueue which is shared between processor and generator. super(new TenantDBBandwidthQueue(SchedulerSettings.DBBandwidthQueue, StorageClientUtility.getCloudStorageAccount().createCloudQueueClient())); } @Override public boolean execute() { boolean retVal = true; int tenantDBBWsCount = 0; try { LOGGER.debug("In Processor"); TenantDBBandwidthQueue queue = (TenantDBBandwidthQueue) getWorkQueue(); String message = queue.dequeue(SchedulerSettings.MessageVisibilityTimeout); if (message == null) { retVal = false; LOGGER.debug("Processor : msg is null"); } else { StopWatch watch = new StopWatch(); watch.start(); LOGGER.debug("Processor : msg is " + message); List<DBBandwidthUsageEntity> listDbBandwidthUsageEntities = partitionStatsAndBWUsageDao .getBandwidthUsage(); tenantDBBWsCount = listDbBandwidthUsageEntities.size(); Map<String, MeteringEntity> map = new HashMap<String, MeteringEntity>(); MeteringEntity meteringEntity = null; String dbName = null; String tenantId = null; for (DBBandwidthUsageEntity dbBandwidthUsageEntity : listDbBandwidthUsageEntities) { dbName = dbBandwidthUsageEntity.getDatabaseName(); if (dbName.startsWith("tnt_")) { tenantId = dbName.substring(4); if (map.containsKey(tenantId)) { meteringEntity = map.get(tenantId); setDatabaseBandwidth(dbBandwidthUsageEntity, meteringEntity); } else { meteringEntity = new MeteringEntity(); meteringEntity.setTenantId(tenantId); setDatabaseBandwidth(dbBandwidthUsageEntity, meteringEntity); map.put(tenantId, meteringEntity); } } } Calendar calendar = Calendar.getInstance(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S z"); dateFormat.setTimeZone(TimeZone.getTimeZone("UTC")); String date = dateFormat.format(calendar.getTime()); for (Iterator<MeteringEntity> iterator = map.values().iterator(); iterator.hasNext();) { meteringEntity = (MeteringEntity) iterator.next(); meteringEntity.setSnapshotTime(dateFormat.parse(date)); meteringDao.add(meteringEntity); } LOGGER.info("Processor : DB bandwidth calculated."); watch.stop(); taskCompletionDao.updateTaskCompletionDetails(watch.getTotalTimeSeconds(), "ProcessMeteringTenantDBBandwidthUse", "Measured the database bandwith use for " + tenantDBBWsCount + " tenants."); } } catch (StorageException e) { retVal = false; LOGGER.error(e.getMessage(), e); } catch (ParseException e) { retVal = false; LOGGER.error(e.getMessage(), e); } return retVal; } /** * Sets the Egress and Ingress in meteringEntity as retrieved from DBBandwidthEntity. * * @param dbBandwidthUsageEntity * @param meteringEntity */ private void setDatabaseBandwidth(DBBandwidthUsageEntity dbBandwidthUsageEntity, MeteringEntity meteringEntity) { BandwidthUsageDirection direction = dbBandwidthUsageEntity.getDirection(); if (direction.compareTo(BandwidthUsageDirection.EGRESS) == 0) { meteringEntity.setDatabaseBandwidth_Egress(dbBandwidthUsageEntity.getKiloBytes()); } else if (direction.compareTo(BandwidthUsageDirection.INGRESS) == 0) { meteringEntity.setDatabaseBandwidth_Ingress(dbBandwidthUsageEntity.getKiloBytes()); } } }