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

org.killbill.queue.retry.RetryableSubscriber Maven / Gradle / Ivy

There is a newer version: 0.40.13
Show newest version
/*
 * Copyright 2014-2017 Groupon, Inc
 * Copyright 2014-2017 The Billing Project, LLC
 *
 * The Billing Project licenses this file to you 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 org.killbill.queue.retry;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.joda.time.DateTime;
import org.killbill.bus.api.BusEvent;
import org.killbill.clock.Clock;
import org.killbill.notificationq.api.NotificationEvent;
import org.killbill.notificationq.api.NotificationQueueService.NotificationQueueHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableSet;
import com.google.common.reflect.TypeToken;

public class RetryableSubscriber extends RetryableHandler {

    private static final Logger log = LoggerFactory.getLogger(RetryableSubscriber.class);

    public RetryableSubscriber(final Clock clock,
                               final RetryableService retryableService,
                               final NotificationQueueHandler handlerDelegate) {
        super(clock, retryableService, handlerDelegate);
    }

    public void handleEvent(final BusEvent event) {
        handleReadyNotification(new SubscriberNotificationEvent(event, event.getClass()),
                                clock.getUTCNow(),
                                event.getUserToken(),
                                event.getSearchKey1(),
                                event.getSearchKey2());
    }

    public interface SubscriberAction {

        void run(T event);
    }

    public static final class SubscriberQueueHandler implements NotificationQueueHandler {

        // Similar to com.google.common.eventbus.SubscriberRegistry
        private static final LoadingCache, ImmutableSet>> FLATTEN_HIERARCHY_CACHE =
                CacheBuilder.newBuilder()
                            .build(
                                    new CacheLoader, ImmutableSet>>() {
                                        @Override
                                        public ImmutableSet> load(final Class concreteClass) {
                                            return ImmutableSet.>copyOf(TypeToken.of(concreteClass).getTypes().rawTypes());
                                        }
                                    });

        private final Map, SubscriberAction> actions = new HashMap, SubscriberAction>();

        public SubscriberQueueHandler() {
        }

        public  void subscribe(final Class busEventClass, final SubscriberAction action) {
            actions.put(busEventClass, action);
        }

        @Override
        public void handleReadyNotification(final NotificationEvent eventJson, final DateTime eventDateTime, final UUID userToken, final Long searchKey1, final Long searchKey2) {
            if (!(eventJson instanceof SubscriberNotificationEvent)) {
                log.error("SubscriberQueueHandler received an unexpected event className='{}'", eventJson.getClass());
            } else {
                final BusEvent busEvent = ((SubscriberNotificationEvent) eventJson).getBusEvent();

                final ImmutableSet> eventTypes = FLATTEN_HIERARCHY_CACHE.getUnchecked(busEvent.getClass());
                for (final Class eventType : eventTypes) {
                    final SubscriberAction next = (SubscriberAction) actions.get(eventType);
                    if (next != null) {
                        next.run(busEvent);
                    }
                }
            }
        }
    }
}