001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.camel.impl; 018 019import java.util.Set; 020import java.util.concurrent.CopyOnWriteArraySet; 021 022import org.apache.camel.CamelContext; 023import org.apache.camel.Service; 024import org.apache.camel.StartupListener; 025import org.apache.camel.util.ServiceHelper; 026 027/** 028 * A {@link org.apache.camel.StartupListener} that defers starting {@link Service}s. 029 */ 030public class DeferServiceStartupListener implements StartupListener { 031 032 private final Set<Service> services = new CopyOnWriteArraySet<Service>(); 033 034 public void addService(Service service) { 035 services.add(service); 036 } 037 038 @Override 039 public void onCamelContextStarted(CamelContext context, boolean alreadyStarted) throws Exception { 040 for (Service service : services) { 041 ServiceHelper.startService(service); 042 } 043 services.clear(); 044 } 045}