/*******************************************************************************
*
* Copyright 2012-2015, the original author or authors.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obta 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.flipkart.aesop.eventconsumer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import com.linkedin.databus.client.pub.DatabusCombinedConsumer;
import com.linkedin.databus.client.pub.DbusClusterConsumerFactory;
import com.linkedin.databus.client.pub.DbusClusterInfo;
import com.linkedin.databus.client.pub.DbusPartitionInfo;
/**
* Class responsible for generating {@link List} of new instances of {@link AbstractEventConsumer} each it is called for
* a particular partition.
* @author Prakhar Jain
*/
public class ConsumerFactory implements DbusClusterConsumerFactory
{
/**
* {@link List} of {@link AbstractEventConsumer} that is generated by
* {@link #createPartitionedConsumers(DbusClusterInfo, DbusPartitionInfo)}. For every call, new instances of Event
* Consumer has to be created.
*/
List<? extends EventConsumerFactoryBean<? extends AbstractEventConsumer>> eventConsumerFactoryList;
/**
* Total Destination groups. Optional. If not set, it is calculated using the group Id set that is defined for each
* of the consumers generated by {@link #createPartitionedConsumers(DbusClusterInfo, DbusPartitionInfo)}.
*/
Integer totalDestinationGroups;
public Collection<DatabusCombinedConsumer> createPartitionedConsumers(DbusClusterInfo clusterInfo,
DbusPartitionInfo partitionInfo)
{
List<AbstractEventConsumer> consumerList = new ArrayList<AbstractEventConsumer>();
for (EventConsumerFactoryBean<? extends AbstractEventConsumer> eventConsumerFactory : eventConsumerFactoryList)
{
AbstractEventConsumer eventConsumer = eventConsumerFactory.getObject();
consumerList.add(eventConsumer);
}
int maxGroupSpecified = 1;
if (totalDestinationGroups == null)
{
for (AbstractEventConsumer eventConsumer : consumerList)
{
maxGroupSpecified = Math.max(maxGroupSpecified, getMaxGroupNo(eventConsumer.getDestinationGroupSet()));
}
totalDestinationGroups = maxGroupSpecified;
}
for (AbstractEventConsumer eventConsumer : consumerList)
{
eventConsumer.setTotalDestinationGroups(totalDestinationGroups);
}
return new ArrayList<DatabusCombinedConsumer>(consumerList);
}
/**
* Get max Group Id for
* @param destinationGroupSet
* @return Max Group No
*/
private int getMaxGroupNo(Set<Integer> destinationGroupSet)
{
int maxDestinationGroupNo = 0;
for (Integer destinationGroupNo : destinationGroupSet)
{
maxDestinationGroupNo = Math.max(maxDestinationGroupNo, destinationGroupNo);
}
return maxDestinationGroupNo;
}
/**
* Sets the Event Consumer Factory List to be used for creating Event Consumer.
* @param eventConsumerFactoryList
*/
public void setEventConsumerFactoryList(
List<? extends EventConsumerFactoryBean<? extends AbstractEventConsumer>> eventConsumerFactoryList)
{
this.eventConsumerFactoryList = eventConsumerFactoryList;
}
/**
* Sets the total number of destination groups.
* @param totalDestinationGroups
*/
public void setTotalDestinationGroups(Integer totalDestinationGroups)
{
this.totalDestinationGroups = totalDestinationGroups;
}
}