Skip to contentMethod: MessageBusAdapterFactory(MessageBus)
1: /*
2: * *********************************************************************************************************************
3: *
4: * TheseFoolishThings: Miscellaneous utilities
5: * http://tidalwave.it/projects/thesefoolishthings
6: *
7: * Copyright (C) 2009 - 2023 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
23: * git clone https://github.com/tidalwave-it/thesefoolishthings-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.messagebus.impl.spring;
28:
29: import java.lang.reflect.Method;
30: import javax.annotation.Nonnull;
31: import it.tidalwave.util.annotation.VisibleForTesting;
32: import it.tidalwave.messagebus.MessageBus;
33: import it.tidalwave.messagebus.MessageBusHelper;
34: import it.tidalwave.messagebus.MessageBusHelper.MethodAdapter;
35: import lombok.Getter;
36: import lombok.RequiredArgsConstructor;
37: import lombok.ToString;
38: import lombok.extern.slf4j.Slf4j;
39:
40: /***********************************************************************************************************************
41: *
42: * @author Fabrizio Giudici
43: *
44: **********************************************************************************************************************/
45:•@RequiredArgsConstructor @Slf4j
46: public class MessageBusAdapterFactory implements MessageBusHelper.Adapter
47: {
48: @Nonnull
49: private final MessageBus messageBus;
50:
51: /*******************************************************************************************************************
52: *
53: *
54: *
55: ******************************************************************************************************************/
56: @Getter @VisibleForTesting @ToString(of = "method")
57: class MessageBusListenerAdapter<TOPIC> implements MethodAdapter<TOPIC>, MessageBus.Listener<TOPIC>
58: {
59: @Nonnull
60: private final Object owner;
61:
62: @Nonnull
63: private final Method method;
64:
65: @Nonnull
66: private final Class<TOPIC> topic;
67:
68: public MessageBusListenerAdapter (@Nonnull final Object owner,
69: @Nonnull final Method method,
70: @Nonnull final Class<TOPIC> topic)
71: {
72: this.owner = owner;
73: this.method = method;
74: this.topic = topic;
75: method.setAccessible(true);
76: }
77:
78: @Override
79: public void notify (@Nonnull final TOPIC message)
80: {
81: log.trace("notify({})", message);
82:
83: try
84: {
85: method.invoke(owner, message);
86: }
87: catch (Throwable t)
88: {
89: log.error("Error calling {} with {}", method, message.getClass());
90: log.error("", t);
91: }
92: }
93:
94: @Override
95: public void subscribe()
96: {
97: messageBus.subscribe(topic, this);
98: }
99:
100: @Override
101: public void unsubscribe()
102: {
103: messageBus.unsubscribe(this);
104: }
105: }
106:
107: /*******************************************************************************************************************
108: *
109: * {@inheritDoc}
110: *
111: ******************************************************************************************************************/
112: @Override @Nonnull
113: public <TOPIC> MethodAdapter<TOPIC> createMethodAdapter (@Nonnull final Object owner,
114: @Nonnull final Method method,
115: @Nonnull final Class<TOPIC> topic)
116: {
117: return new MessageBusListenerAdapter<>(owner, method, topic);
118: }
119:
120: /*******************************************************************************************************************
121: *
122: * {@inheritDoc}
123: *
124: ******************************************************************************************************************/
125: @Override
126: public void publish (@Nonnull final Object message)
127: {
128: messageBus.publish(message);
129: }
130:
131: /*******************************************************************************************************************
132: *
133: * {@inheritDoc}
134: *
135: ******************************************************************************************************************/
136: @Override
137: public <TOPIC> void publish (@Nonnull final Class<TOPIC> topic, @Nonnull final TOPIC message)
138: {
139: messageBus.publish(topic, message);
140: }
141: }