All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.threewks.thundr.bigmetrics.service.PushQueueEventDispatcher Maven / Gradle / Ivy

The newest version!
/*
 * This file is a component of thundr, a software library from 3wks.
 * Read more: http://www.3wks.com.au/thundr
 * Copyright (C) 2014 3wks, 
 *
 * 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.threewks.thundr.bigmetrics.service;

import java.util.Map;

import com.google.appengine.api.taskqueue.Queue;
import com.google.appengine.api.taskqueue.TaskOptions;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.threewks.thundr.bigmetrics.BigMetricsService;

/**
 * Dispatches events by putting the data required on the push queue, and then allowing an endpoint (and ultimately a service), to
 * process the event
 */
public class PushQueueEventDispatcher implements EventDispatcher {

    private final Gson gson;
    private final Queue bigMetricsQueue;
    private final String bigmetricsPushQueueUrl;
    private final String bigmetricsViewEnsureAllUrl;
    private final String bigmetricsTableEnsureAllUrl;

    public PushQueueEventDispatcher(Queue bigMetricsQueue, String bigmetricsPushQueueUrl, String bigmetricsViewEnsureAllUrl, String bigmetricsTableEnsureAllUrl) {
        this.bigMetricsQueue = bigMetricsQueue;
        this.bigmetricsPushQueueUrl = bigmetricsPushQueueUrl;
        this.bigmetricsViewEnsureAllUrl = bigmetricsViewEnsureAllUrl;
        this.bigmetricsTableEnsureAllUrl = bigmetricsTableEnsureAllUrl;
        this.gson = new GsonBuilder().create();
    }

    @Override
    public void handleEvent(String tableId, Map data) {
        // put the request on a push queue
        TaskOptions taskOptions = TaskOptions.Builder.withDefaults();
        taskOptions.method(TaskOptions.Method.POST);
        taskOptions.url(bigmetricsPushQueueUrl.replace("{tableId}", tableId));
        taskOptions.payload(gson.toJson(data)
                                .getBytes(),
                "application/json");

        bigMetricsQueue.add(taskOptions);
    }

    @Override
    public void ensureTablesExist(BigMetricsService bigMetricsService) {
        TaskOptions taskOptions = TaskOptions.Builder.withDefaults();
        taskOptions.method(TaskOptions.Method.POST);
        taskOptions.url(bigmetricsTableEnsureAllUrl);
        bigMetricsQueue.add(taskOptions);
    }

    @Override
    public void ensureViewsExist(BigMetricsService bigMetricsService) {
        TaskOptions taskOptions = TaskOptions.Builder.withDefaults();
        taskOptions.method(TaskOptions.Method.POST);
        taskOptions.url(bigmetricsViewEnsureAllUrl);
        bigMetricsQueue.add(taskOptions);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy