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.SimpleDateFormat; import java.util.Calendar; import java.util.TimeZone; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Component; import org.springframework.util.StopWatch; import com.persistent.cloudninja.dao.MeteringDao; import com.persistent.cloudninja.dao.TaskCompletionDao; import com.persistent.cloudninja.domainobject.MeteringEntity; import com.persistent.cloudninja.utils.SchedulerSettings; import com.persistent.cloudninja.utils.StorageClientUtility; /** * Processor for tenant blob size calculation task. It processes the * messages generated by generator. */ @Component @Lazy(true) public class TenantBlobSizeProcessor extends TaskActivityBase { private static final Logger LOGGER = Logger.getLogger(TenantBlobSizeProcessor.class); @Autowired private StorageUtility storageUtility; @Autowired private MeteringDao meteringDao; @Autowired private TaskCompletionDao taskCompletionDao; public TenantBlobSizeProcessor() throws InvalidKeyException, URISyntaxException { //task-queue which is shared between processor and generator. super(new TenantBlobSizeQueue(SchedulerSettings.BlobStorageSizeQueue, StorageClientUtility.getCloudStorageAccount().createCloudQueueClient())); } /** * Calculates the blob sizes of private and public container. * */ @Override public boolean execute() { boolean retVal = true; String tenantId = null; long blobSize = 0; try { LOGGER.debug("In Processor"); TenantBlobSizeQueue queue = (TenantBlobSizeQueue) getWorkQueue(); tenantId = queue.dequeue(SchedulerSettings.MessageVisibilityTimeout); if (tenantId == null) { retVal = false; LOGGER.debug("Processor : msg is null"); } else { StopWatch watch = new StopWatch(); watch.start(); //get the size of blobs in private container. blobSize = storageUtility.getContainerSize("tntp-" + tenantId.toLowerCase()); //get the size of blobs in public container. blobSize = blobSize + storageUtility.getContainerSize("tnts-" + tenantId.toLowerCase()); LOGGER.debug("Processor : msg is " + tenantId); MeteringEntity metering = new MeteringEntity(); metering.setTenantId(tenantId); 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()); metering.setSnapshotTime(dateFormat.parse(date)); //set the calculated size blobSize = blobSize / 1024; metering.setBlobStoreUsage(blobSize); meteringDao.add(metering); LOGGER.info("Processor : blobSize is " + blobSize); watch.stop(); taskCompletionDao.updateTaskCompletionDetails(watch.getTotalTimeSeconds(), "ProcessMeteringBlobSizes", "Measured " + blobSize + " kb for tenant " + tenantId); } } catch (Exception e) { retVal = false; LOGGER.error(e.getMessage(), e); } return retVal; } }