View Javadoc
1   package com.jsql.view.subscriber;
2   
3   import com.jsql.util.LogLevelUtil;
4   import org.apache.logging.log4j.LogManager;
5   import org.apache.logging.log4j.Logger;
6   
7   import javax.swing.*;
8   import java.util.concurrent.Flow.Subscriber;
9   import java.util.concurrent.Flow.Subscription;
10  
11  public abstract class AbstractSubscriber implements Subscriber<Seal> {
12  
13      private static final Logger LOGGER = LogManager.getRootLogger();
14  
15      private Subscription subscription;
16  
17      @Override
18      public void onSubscribe(Subscription subscription) {
19          this.subscription = subscription;
20          subscription.request(1);  // enable receiving items
21      }
22  
23      @Override
24      public void onNext(Seal request) {
25          this.subscription.request(1);
26  
27          // Display model thread name in logs instead of the observer name
28          String nameThread = Thread.currentThread().getName();
29          SwingUtilities.invokeLater(() -> {
30              Thread.currentThread().setName("from " + nameThread);
31              this.execute(request);
32          });
33      }
34  
35      protected abstract void execute(Seal request);
36  
37      @Override
38      public void onError(Throwable e) {
39          LOGGER.log(LogLevelUtil.CONSOLE_JAVA, e, e);
40      }
41  
42      @Override
43      public void onComplete() {
44          // Nothing
45      }
46  
47      /**
48       * Observer pattern.<br>
49       * Receive an update order from the model:<br>
50       * - Use the Request message to get the Interaction class,<br>
51       * - Pass the parameters to that class.
52       */
53      public Subscription getSubscription() {
54          return this.subscription;
55      }
56  }