/**
* Copyright 2013, Big Switch Networks, Inc.
*
* 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 net.floodlightcontroller.threadpool;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
import net.floodlightcontroller.core.module.FloodlightModuleContext;
import net.floodlightcontroller.core.module.FloodlightModuleException;
import net.floodlightcontroller.core.module.IFloodlightModule;
import net.floodlightcontroller.core.module.IFloodlightService;
public class ThreadPool implements IThreadPoolService, IFloodlightModule {
protected ScheduledExecutorService executor = null;
// IThreadPoolService
@Override
public ScheduledExecutorService getScheduledExecutor() {
return executor;
}
// IFloodlightModule
@Override
public Collection<Class<? extends IFloodlightService>> getModuleServices() {
Collection<Class<? extends IFloodlightService>> l =
new ArrayList<Class<? extends IFloodlightService>>();
l.add(IThreadPoolService.class);
return l;
}
@Override
public Map<Class<? extends IFloodlightService>, IFloodlightService>
getServiceImpls() {
Map<Class<? extends IFloodlightService>,
IFloodlightService> m =
new HashMap<Class<? extends IFloodlightService>,
IFloodlightService>();
m.put(IThreadPoolService.class, this);
// We are the class that implements the service
return m;
}
@Override
public Collection<Class<? extends IFloodlightService>>
getModuleDependencies() {
// No dependencies
return null;
}
@Override
public void init(FloodlightModuleContext context)
throws FloodlightModuleException {
final ThreadGroup tg = new ThreadGroup("Scheduled Task Threads");
ThreadFactory f = new ThreadFactory() {
AtomicInteger id = new AtomicInteger();
@Override
public Thread newThread(Runnable runnable) {
return new Thread(tg, runnable,
"Scheduled-" + id.getAndIncrement());
}
};
executor = Executors.newScheduledThreadPool(5, f);
}
@Override
public void startUp(FloodlightModuleContext context) {
// no-op
}
}