|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package dispatcher |
| 19 | + |
| 20 | +import ( |
| 21 | + "reflect" |
| 22 | +) |
| 23 | + |
| 24 | +import ( |
| 25 | + "github.com/apache/dubbo-go/common/extension" |
| 26 | + "github.com/apache/dubbo-go/common/logger" |
| 27 | + "github.com/apache/dubbo-go/common/observer" |
| 28 | +) |
| 29 | + |
| 30 | +func init() { |
| 31 | + extension.SetEventDispatcher("direct", NewDirectEventDispatcher) |
| 32 | +} |
| 33 | + |
| 34 | +// DirectEventDispatcher is align with DirectEventDispatcher interface in Java. |
| 35 | +// it's the top abstraction |
| 36 | +// Align with 2.7.5 |
| 37 | +// Dispatcher event to listener direct |
| 38 | +type DirectEventDispatcher struct { |
| 39 | + observer.BaseListenable |
| 40 | +} |
| 41 | + |
| 42 | +// NewDirectEventDispatcher ac constructor of DirectEventDispatcher |
| 43 | +func NewDirectEventDispatcher() observer.EventDispatcher { |
| 44 | + return &DirectEventDispatcher{} |
| 45 | +} |
| 46 | + |
| 47 | +// Dispatch event directly |
| 48 | +func (ded *DirectEventDispatcher) Dispatch(event observer.Event) { |
| 49 | + if event == nil { |
| 50 | + logger.Warnf("[DirectEventDispatcher] dispatch event nil") |
| 51 | + return |
| 52 | + } |
| 53 | + eventType := reflect.TypeOf(event).Elem() |
| 54 | + value, loaded := ded.ListenersCache.Load(eventType) |
| 55 | + if !loaded { |
| 56 | + return |
| 57 | + } |
| 58 | + listenersSlice := value.([]observer.EventListener) |
| 59 | + for _, listener := range listenersSlice { |
| 60 | + if err := listener.OnEvent(event); err != nil { |
| 61 | + logger.Warnf("[DirectEventDispatcher] dispatch event error:%v", err) |
| 62 | + } |
| 63 | + } |
| 64 | +} |
0 commit comments