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 CamelContext camelContext; 048 private Endpoint endpoint; 049 private String endpointUri; 050 private Producer producer; 051 052 @Override 053 public void notify(CamelEvent event) throws Exception { 054 // only notify when we are started 055 if (!isStarted()) { 056 LOG.debug("Cannot publish event as notifier is not started: {}", event); 057 return; 058 } 059 060 // only notify when camel context is running 061 if (!camelContext.getStatus().isStarted()) { 062 LOG.debug("Cannot publish event as CamelContext is not started: {}", event); 063 return; 064 } 065 066 Exchange exchange = producer.getEndpoint().createExchange(); 067 exchange.getIn().setBody(event); 068 069 // make sure we don't send out events for this as well 070 // mark exchange as being published to event, to prevent creating new events 071 // for this as well (causing a endless flood of events) 072 exchange.adapt(ExtendedExchange.class).setNotifyEvent(true); 073 try { 074 producer.process(exchange); 075 } finally { 076 // and remove it when its done 077 exchange.adapt(ExtendedExchange.class).setNotifyEvent(false); 078 } 079 } 080 081 @Override 082 public boolean isEnabled(CamelEvent event) { 083 return true; 084 } 085 086 @Override 087 public CamelContext getCamelContext() { 088 return camelContext; 089 } 090 091 @Override 092 public void setCamelContext(CamelContext camelContext) { 093 this.camelContext = camelContext; 094 } 095 096 public Endpoint getEndpoint() { 097 return endpoint; 098 } 099 100 public void setEndpoint(Endpoint endpoint) { 101 this.endpoint = endpoint; 102 } 103 104 public String getEndpointUri() { 105 return endpointUri; 106 } 107 108 public void setEndpointUri(String endpointUri) { 109 this.endpointUri = endpointUri; 110 } 111 112 @Override 113 protected void doStart() throws Exception { 114 ObjectHelper.notNull(camelContext, "camelContext", this); 115 if (endpoint == null && endpointUri == null) { 116 throw new IllegalArgumentException("Either endpoint or endpointUri must be configured"); 117 } 118 119 if (endpoint == null) { 120 endpoint = camelContext.getEndpoint(endpointUri); 121 } 122 123 producer = endpoint.createProducer(); 124 ServiceHelper.startService(producer); 125 } 126 127 @Override 128 protected void doStop() throws Exception { 129 ServiceHelper.stopService(producer); 130 } 131 132 @Override 133 public String toString() { 134 return "PublishEventNotifier[" + (endpoint != null ? endpoint : URISupport.sanitizeUri(endpointUri)) + "]"; 135 } 136 137}