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