com.persistent.cloudninja.scheduler.TenantWebBandWidthUsageProcessor.java Source code

Java tutorial

Introduction

Here is the source code for com.persistent.cloudninja.scheduler.TenantWebBandWidthUsageProcessor.java

Source

/*******************************************************************************
 * 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.util.List;

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.TaskCompletionDao;
import com.persistent.cloudninja.domainobject.WebLogMeteringBatch;
import com.persistent.cloudninja.utils.SchedulerSettings;
import com.persistent.cloudninja.utils.StorageClientUtility;
import com.persistent.cloudninja.utils.TenantWebBandWidthUsageProcessUtility;

/**
 * Processor for tenant web band width usage. It processes the
 * messages generated by generator.
 */
@Component
public class TenantWebBandWidthUsageProcessor extends TaskActivityBase {

    private static final Logger LOGGER = Logger.getLogger(TenantWebBandWidthUsageProcessor.class);

    @Autowired
    private TaskCompletionDao taskCompletionDao;

    @Autowired
    private TenantWebBandWidthUsageProcessUtility tenantWebBandWidthUsageProcessUtility;

    public TenantWebBandWidthUsageProcessor() throws InvalidKeyException, URISyntaxException {
        //task-queue which is shared between processor and generator.
        super(new TenantWebBandWidthUsageQueue(SchedulerSettings.WebBandWidthUsageQueue,
                StorageClientUtility.getCloudStorageAccount().createCloudQueueClient()));
    }

    @Override
    public boolean execute() {
        StopWatch watch = new StopWatch();
        LOGGER.info("Start of TenantWebBandWidthUsageProcessor:execute");
        boolean returnFlag = true;
        TenantWebBandWidthUsageQueue queue = (TenantWebBandWidthUsageQueue) getWorkQueue();
        try {
            watch.start();
            List<WebLogMeteringBatch> list = queue.dequeue();
            LOGGER.debug("Queue List Size :" + list.size());
            for (int i = 0; i < list.size(); i++) {
                tenantWebBandWidthUsageProcessUtility.getTenantWebBandWidthUsage(list.get(i));
            }
            watch.stop();
            taskCompletionDao.updateTaskCompletionDetails(watch.getTotalTimeSeconds(),
                    "ProcessMeteringWebAppBandwidth", "Processed a batch of " + list.size() + " Tomcat Logs");
        } catch (StorageException e) {
            returnFlag = false;
            LOGGER.error(e.getMessage(), e);
        } catch (ParseException e) {
            returnFlag = false;
            LOGGER.error(e.getMessage(), e);
        }
        LOGGER.debug("ReturnFlag :" + returnFlag);
        LOGGER.info("End of TenantWebBandWidthUsageProcessor:execute");
        return returnFlag;
    }
}