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.component.vm; 018 019import java.util.HashMap; 020import java.util.Map; 021import java.util.concurrent.BlockingQueue; 022import java.util.concurrent.atomic.AtomicInteger; 023 024import org.apache.camel.Component; 025import org.apache.camel.Endpoint; 026import org.apache.camel.Exchange; 027import org.apache.camel.component.seda.BlockingQueueFactory; 028import org.apache.camel.component.seda.QueueReference; 029import org.apache.camel.component.seda.SedaComponent; 030 031/** 032 * The <a href="http://camel.apache.org/vm.html">VM component</a> is for asynchronous SEDA exchanges on a {@link BlockingQueue} 033 * within the classloader tree containing the camel-core.jar. 034 * 035 * i.e. to handle communicating across CamelContext instances and possibly across web application contexts, providing that camel-core.jar is on the system classpath. 036 * 037 * @version 038 */ 039public class VmComponent extends SedaComponent { 040 protected static final Map<String, QueueReference> QUEUES = new HashMap<>(); 041 protected static final Map<String, VmEndpoint> ENDPOINTS = new HashMap<>(); 042 private static final AtomicInteger START_COUNTER = new AtomicInteger(); 043 044 public VmComponent() { 045 super(VmEndpoint.class); 046 } 047 048 public VmComponent(Class<? extends Endpoint> endpointClass) { 049 super(endpointClass); 050 } 051 052 @Override 053 public Map<String, QueueReference> getQueues() { 054 return QUEUES; 055 } 056 057 @Override 058 public QueueReference getQueueReference(String key) { 059 return QUEUES.get(key); 060 } 061 062 @Override 063 protected void doStart() throws Exception { 064 super.doStart(); 065 START_COUNTER.incrementAndGet(); 066 } 067 068 @Override 069 protected void doStop() throws Exception { 070 if (START_COUNTER.decrementAndGet() <= 0) { 071 // clear queues when no more vm components in use 072 getQueues().clear(); 073 // also clear endpoints 074 ENDPOINTS.clear(); 075 } 076 } 077 078 @Override 079 protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception { 080 if (ENDPOINTS.containsKey(uri)) { 081 return ENDPOINTS.get(uri); 082 } 083 084 VmEndpoint answer = (VmEndpoint) super.createEndpoint(uri, remaining, parameters); 085 086 ENDPOINTS.put(uri, answer); 087 return answer; 088 } 089 090 @Override 091 protected VmEndpoint createEndpoint(String endpointUri, Component component, BlockingQueueFactory<Exchange> queueFactory, int concurrentConsumers) { 092 return new VmEndpoint(endpointUri, component, queueFactory, concurrentConsumers); 093 } 094 095 @Override 096 protected VmEndpoint createEndpoint(String endpointUri, Component component, BlockingQueue<Exchange> queue, int concurrentConsumers) { 097 return new VmEndpoint(endpointUri, component, queue, concurrentConsumers); 098 } 099}