View Javadoc
1   /*
2    * *************************************************************************************************************************************************************
3    *
4    * TheseFoolishThings: Miscellaneous utilities
5    * http://tidalwave.it/projects/thesefoolishthings
6    *
7    * Copyright (C) 2009 - 2025 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 the License.
12   * 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 an "AS IS" BASIS, WITHOUT WARRANTIES OR
17   * CONDITIONS OF ANY KIND, either express or implied.  See the License for the specific language governing permissions and limitations under the License.
18   *
19   * *************************************************************************************************************************************************************
20   *
21   * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
22   * git clone https://github.com/tidalwave-it/thesefoolishthings-src
23   *
24   * *************************************************************************************************************************************************************
25   */
26  package it.tidalwave.actor.impl;
27  
28  import java.lang.reflect.Method;
29  import javax.annotation.Nonnull;
30  import it.tidalwave.actor.Collaboration;
31  import it.tidalwave.actor.spi.ActorActivatorStats;
32  import it.tidalwave.messagebus.MessageBus;
33  import lombok.RequiredArgsConstructor;
34  import lombok.extern.slf4j.Slf4j;
35  
36  /***************************************************************************************************************************************************************
37   *
38   * @author  Fabrizio Giudici
39   *
40   **************************************************************************************************************************************************************/
41  @RequiredArgsConstructor @Slf4j
42  class CollaborationMessageListenerAdapter<T extends Collaboration.Provider> implements MessageBus.Listener<T>
43    {
44      @Nonnull
45      private final Object owner;
46  
47      @Nonnull
48      private final Method method;
49  
50      @Nonnull
51      private final ExecutorWithPriority executor;
52  
53      @Nonnull
54      private final Class<?> messageType;
55  
56      @Nonnull
57      private final ActorActivatorStats stats;
58  
59      @Override
60      public void notify (@Nonnull final T message)
61        {
62          log.trace("notify({})", message);
63          final var collaboration = (DefaultCollaboration)message.getCollaboration();
64          collaboration.registerPendingMessage(message);
65          stats.changePendingMessageCount(+1);
66          executor.execute(() ->
67            {
68              collaboration.unregisterPendingMessage(message);
69              stats.changePendingMessageCount(-1);
70  
71              if (collaboration.getOriginatingMessage().getClass().equals(messageType))
72                {
73                  collaboration.bindToCurrentThread();
74  
75                  try
76                    {
77                      stats.incrementInvocationCount();
78                      method.invoke(owner, message, collaboration.getOriginatingMessage());
79                      stats.incrementSuccessfulInvocationCount();
80                    }
81                  catch (Throwable t)
82                    {
83                      stats.incrementInvocationErrorCount();
84                      log.error("Error calling {} with {}", method, message.getClass());
85                      log.error("", t);
86                    }
87                  finally
88                    {
89                      collaboration.unbindFromCurrentThread();
90                    }
91                }
92            });
93        }
94    }
95