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