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.event; 018 019import org.apache.camel.Exchange; 020import org.apache.camel.Processor; 021import org.apache.camel.Producer; 022import org.apache.camel.processor.loadbalancer.LoadBalancer; 023import org.apache.camel.processor.loadbalancer.TopicLoadBalancer; 024import org.apache.camel.spi.UriEndpoint; 025import org.apache.camel.spi.UriPath; 026import org.apache.camel.support.DefaultEndpoint; 027import org.apache.camel.support.DefaultProducer; 028import org.apache.camel.util.ObjectHelper; 029import org.springframework.beans.BeansException; 030import org.springframework.context.ApplicationContext; 031import org.springframework.context.ApplicationContextAware; 032import org.springframework.context.ApplicationEvent; 033 034import static org.apache.camel.RuntimeCamelException.wrapRuntimeCamelException; 035 036/** 037 * The spring-event component allows to listen for Spring Application Events. 038 */ 039@UriEndpoint(firstVersion = "1.4.0", scheme = "spring-event", title = "Spring Event", syntax = "spring-event:name", label = "spring,eventbus") 040public class EventEndpoint extends DefaultEndpoint implements ApplicationContextAware { 041 private LoadBalancer loadBalancer; 042 private ApplicationContext applicationContext; 043 044 @UriPath(description = "Name of endpoint") 045 private String name; 046 047 public EventEndpoint(String endpointUri, EventComponent component, String name) { 048 super(endpointUri, component); 049 this.applicationContext = component.getApplicationContext(); 050 this.name = name; 051 } 052 053 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { 054 this.applicationContext = applicationContext; 055 } 056 057 public ApplicationContext getApplicationContext() { 058 return applicationContext; 059 } 060 061 public String getName() { 062 return name; 063 } 064 065 public void setName(String name) { 066 this.name = name; 067 } 068 069 public boolean isSingleton() { 070 return true; 071 } 072 073 public Producer createProducer() throws Exception { 074 ObjectHelper.notNull(applicationContext, "applicationContext"); 075 return new DefaultProducer(this) { 076 public void process(Exchange exchange) throws Exception { 077 ApplicationEvent event = toApplicationEvent(exchange); 078 applicationContext.publishEvent(event); 079 } 080 }; 081 } 082 083 public EventConsumer createConsumer(Processor processor) throws Exception { 084 ObjectHelper.notNull(applicationContext, "applicationContext"); 085 EventConsumer answer = new EventConsumer(this, processor); 086 configureConsumer(answer); 087 return answer; 088 } 089 090 public void onApplicationEvent(ApplicationEvent event) { 091 Exchange exchange = createExchange(); 092 exchange.getIn().setBody(event); 093 try { 094 getLoadBalancer().process(exchange); 095 } catch (Exception e) { 096 throw wrapRuntimeCamelException(e); 097 } 098 } 099 100 public LoadBalancer getLoadBalancer() { 101 if (loadBalancer == null) { 102 loadBalancer = createLoadBalancer(); 103 } 104 return loadBalancer; 105 } 106 107 public void setLoadBalancer(LoadBalancer loadBalancer) { 108 this.loadBalancer = loadBalancer; 109 } 110 111 @Override 112 public EventComponent getComponent() { 113 return (EventComponent) super.getComponent(); 114 } 115 116 // Implementation methods 117 // ------------------------------------------------------------------------- 118 public synchronized void consumerStarted(EventConsumer consumer) { 119 getComponent().consumerStarted(this); 120 getLoadBalancer().addProcessor(consumer.getAsyncProcessor()); 121 } 122 123 public synchronized void consumerStopped(EventConsumer consumer) { 124 getComponent().consumerStopped(this); 125 getLoadBalancer().removeProcessor(consumer.getAsyncProcessor()); 126 } 127 128 protected LoadBalancer createLoadBalancer() { 129 return new TopicLoadBalancer(); 130 } 131 132 protected ApplicationEvent toApplicationEvent(Exchange exchange) { 133 ApplicationEvent event = exchange.getIn().getBody(ApplicationEvent.class); 134 if (event != null) { 135 return event; 136 } 137 return new CamelEvent(this, exchange); 138 } 139}