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.management; 018 019import java.util.EventObject; 020 021import org.apache.camel.CamelContext; 022import org.apache.camel.CamelContextAware; 023import org.apache.camel.Endpoint; 024import org.apache.camel.Exchange; 025import org.apache.camel.ExtendedExchange; 026import org.apache.camel.Producer; 027import org.apache.camel.spi.CamelEvent; 028import org.apache.camel.support.EventNotifierSupport; 029import org.apache.camel.support.service.ServiceHelper; 030import org.apache.camel.util.ObjectHelper; 031import org.apache.camel.util.URISupport; 032import org.slf4j.Logger; 033import org.slf4j.LoggerFactory; 034 035/** 036 * A {@link org.apache.camel.spi.EventNotifier} which publishes the {@link EventObject} to some 037 * {@link org.apache.camel.Endpoint}. 038 * <p/> 039 * This notifier is only enabled when {@link CamelContext} is started. This avoids problems when sending notifications 040 * during start/shutdown of {@link CamelContext} which causes problems by sending those events to Camel routes by this 041 * notifier. 042 */ 043public class PublishEventNotifier extends EventNotifierSupport implements CamelContextAware { 044 045 private static final Logger LOG = LoggerFactory.getLogger(PublishEventNotifier.class); 046 047 private Endpoint endpoint; 048 private String endpointUri; 049 private Producer producer; 050 051 @Override 052 public void notify(CamelEvent event) throws Exception { 053 // only notify when we are started 054 if (!isStarted()) { 055 LOG.debug("Cannot publish event as notifier is not started: {}", event); 056 return; 057 } 058 059 // only notify when camel context is running 060 if (!getCamelContext().getStatus().isStarted()) { 061 LOG.debug("Cannot publish event as CamelContext is not started: {}", event); 062 return; 063 } 064 065 Exchange exchange = producer.getEndpoint().createExchange(); 066 exchange.getIn().setBody(event); 067 068 // make sure we don't send out events for this as well 069 // mark exchange as being published to event, to prevent creating new events 070 // for this as well (causing a endless flood of events) 071 exchange.adapt(ExtendedExchange.class).setNotifyEvent(true); 072 try { 073 producer.process(exchange); 074 } finally { 075 // and remove it when its done 076 exchange.adapt(ExtendedExchange.class).setNotifyEvent(false); 077 } 078 } 079 080 @Override 081 public boolean isEnabled(CamelEvent event) { 082 return true; 083 } 084 085 public Endpoint getEndpoint() { 086 return endpoint; 087 } 088 089 public void setEndpoint(Endpoint endpoint) { 090 this.endpoint = endpoint; 091 } 092 093 public String getEndpointUri() { 094 return endpointUri; 095 } 096 097 public void setEndpointUri(String endpointUri) { 098 this.endpointUri = endpointUri; 099 } 100 101 @Override 102 protected void doStart() throws Exception { 103 ObjectHelper.notNull(getCamelContext(), "camelContext", this); 104 if (endpoint == null && endpointUri == null) { 105 throw new IllegalArgumentException("Either endpoint or endpointUri must be configured"); 106 } 107 108 if (endpoint == null) { 109 endpoint = getCamelContext().getEndpoint(endpointUri); 110 } 111 112 producer = endpoint.createProducer(); 113 ServiceHelper.startService(producer); 114 } 115 116 @Override 117 protected void doStop() throws Exception { 118 ServiceHelper.stopService(producer); 119 } 120 121 @Override 122 public String toString() { 123 return "PublishEventNotifier[" + (endpoint != null ? endpoint : URISupport.sanitizeUri(endpointUri)) + "]"; 124 } 125 126}