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 @Override 054 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { 055 this.applicationContext = applicationContext; 056 } 057 058 public ApplicationContext getApplicationContext() { 059 return applicationContext; 060 } 061 062 public String getName() { 063 return name; 064 } 065 066 public void setName(String name) { 067 this.name = name; 068 } 069 070 @Override 071 public Producer createProducer() throws Exception { 072 ObjectHelper.notNull(applicationContext, "applicationContext"); 073 return new DefaultProducer(this) { 074 public void process(Exchange exchange) throws Exception { 075 ApplicationEvent event = toApplicationEvent(exchange); 076 applicationContext.publishEvent(event); 077 } 078 }; 079 } 080 081 @Override 082 public EventConsumer createConsumer(Processor processor) throws Exception { 083 ObjectHelper.notNull(applicationContext, "applicationContext"); 084 EventConsumer answer = new EventConsumer(this, processor); 085 configureConsumer(answer); 086 return answer; 087 } 088 089 public void onApplicationEvent(ApplicationEvent event) { 090 Exchange exchange = createExchange(); 091 exchange.getIn().setBody(event); 092 try { 093 getLoadBalancer().process(exchange); 094 } catch (Exception e) { 095 throw wrapRuntimeCamelException(e); 096 } 097 } 098 099 public LoadBalancer getLoadBalancer() { 100 if (loadBalancer == null) { 101 loadBalancer = createLoadBalancer(); 102 } 103 return loadBalancer; 104 } 105 106 public void setLoadBalancer(LoadBalancer loadBalancer) { 107 this.loadBalancer = loadBalancer; 108 } 109 110 @Override 111 public EventComponent getComponent() { 112 return (EventComponent) super.getComponent(); 113 } 114 115 // Implementation methods 116 // ------------------------------------------------------------------------- 117 public synchronized void consumerStarted(EventConsumer consumer) { 118 getComponent().consumerStarted(this); 119 getLoadBalancer().addProcessor(consumer.getAsyncProcessor()); 120 } 121 122 public synchronized void consumerStopped(EventConsumer consumer) { 123 getComponent().consumerStopped(this); 124 getLoadBalancer().removeProcessor(consumer.getAsyncProcessor()); 125 } 126 127 protected LoadBalancer createLoadBalancer() { 128 return new TopicLoadBalancer(); 129 } 130 131 protected ApplicationEvent toApplicationEvent(Exchange exchange) { 132 ApplicationEvent event = exchange.getIn().getBody(ApplicationEvent.class); 133 if (event != null) { 134 return event; 135 } 136 return new CamelEvent(this, exchange); 137 } 138}