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 */ 017 package org.apache.camel.component.event; 018 019 import org.apache.camel.Exchange; 020 import org.apache.camel.NoTypeConversionAvailableException; 021 import org.apache.camel.Processor; 022 import org.apache.camel.Producer; 023 import org.apache.camel.impl.DefaultEndpoint; 024 import org.apache.camel.impl.DefaultProducer; 025 import org.apache.camel.processor.loadbalancer.LoadBalancer; 026 import org.apache.camel.processor.loadbalancer.TopicLoadBalancer; 027 import org.apache.camel.util.ObjectHelper; 028 import org.springframework.beans.BeansException; 029 import org.springframework.context.ApplicationContext; 030 import org.springframework.context.ApplicationContextAware; 031 import org.springframework.context.ApplicationEvent; 032 033 import static org.apache.camel.util.ObjectHelper.wrapRuntimeCamelException; 034 035 036 /** 037 * An <a href="http://activemq.apache.org/camel/event.html">Event Endpoint</a> 038 * for working with Spring ApplicationEvents 039 * 040 * @version $Revision: 1459 $ 041 */ 042 public class EventEndpoint extends DefaultEndpoint<Exchange> implements ApplicationContextAware { 043 private LoadBalancer loadBalancer; 044 private ApplicationContext applicationContext; 045 046 public EventEndpoint(String endpointUri, EventComponent component) { 047 super(endpointUri, component); 048 this.applicationContext = component.getApplicationContext(); 049 } 050 051 public EventEndpoint(String endpointUri) { 052 super(endpointUri); 053 } 054 055 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { 056 this.applicationContext = applicationContext; 057 } 058 059 public ApplicationContext getApplicationContext() { 060 return applicationContext; 061 } 062 063 public boolean isSingleton() { 064 return true; 065 } 066 067 public Producer<Exchange> createProducer() throws Exception { 068 ObjectHelper.notNull(getApplicationContext(), "applicationContext"); 069 return new DefaultProducer<Exchange>(this) { 070 public void process(Exchange exchange) throws Exception { 071 ApplicationEvent event = toApplicationEvent(exchange); 072 getApplicationContext().publishEvent(event); 073 } 074 }; 075 } 076 077 public EventConsumer createConsumer(Processor processor) throws Exception { 078 return new EventConsumer(this, processor); 079 } 080 081 public void onApplicationEvent(ApplicationEvent event) { 082 Exchange exchange = createExchange(); 083 exchange.getIn().setBody(event); 084 try { 085 getLoadBalancer().process(exchange); 086 } catch (Exception e) { 087 throw wrapRuntimeCamelException(e); 088 } 089 } 090 091 public LoadBalancer getLoadBalancer() { 092 if (loadBalancer == null) { 093 loadBalancer = createLoadBalancer(); 094 } 095 return loadBalancer; 096 } 097 098 public void setLoadBalancer(LoadBalancer loadBalancer) { 099 this.loadBalancer = loadBalancer; 100 } 101 102 // Implementation methods 103 // ------------------------------------------------------------------------- 104 public synchronized void consumerStarted(EventConsumer consumer) { 105 getLoadBalancer().addProcessor(consumer.getProcessor()); 106 } 107 108 public synchronized void consumerStopped(EventConsumer consumer) { 109 getLoadBalancer().removeProcessor(consumer.getProcessor()); 110 } 111 112 protected LoadBalancer createLoadBalancer() { 113 return new TopicLoadBalancer(); 114 } 115 116 protected ApplicationEvent toApplicationEvent(Exchange exchange) { 117 try { 118 ApplicationEvent event = exchange.getIn().getBody(ApplicationEvent.class); 119 if (event != null) { 120 return event; 121 } 122 } catch (NoTypeConversionAvailableException ex) { 123 // ignore, handled below 124 } 125 return new CamelEvent(this, exchange); 126 } 127 }