Skip to content

Method: JavaFXSpringAnnotationApplication.MessageBusBeans()

1: /*
2: * *************************************************************************************************************************************************************
3: *
4: * SteelBlue: DCI User Interfaces
5: * http://tidalwave.it/projects/steelblue
6: *
7: * Copyright (C) 2015 - 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/steelblue-src
22: * git clone https://github.com/tidalwave-it/steelblue-src
23: *
24: * *************************************************************************************************************************************************************
25: */
26: package it.tidalwave.ui.javafx;
27:
28: import jakarta.annotation.Nonnull;
29: import java.util.ArrayList;
30: import java.util.List;
31: import org.springframework.beans.factory.BeanFactory;
32: import org.springframework.context.ConfigurableApplicationContext;
33: import org.springframework.context.annotation.AnnotationConfigApplicationContext;
34: import org.springframework.context.annotation.Bean;
35: import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
36: import it.tidalwave.ui.javafx.impl.DefaultJavaFXPanelGroupControl;
37: import it.tidalwave.ui.javafx.impl.util.JavaFXSafeComponentBuilder;
38: import it.tidalwave.ui.javafx.spi.AbstractJavaFXSpringApplication;
39: import org.slf4j.Logger;
40: import org.slf4j.LoggerFactory;
41: import it.tidalwave.util.PreferencesHandler;
42: import it.tidalwave.ui.core.annotation.EnableMessageBus;
43: import it.tidalwave.messagebus.MessageBus;
44: import it.tidalwave.messagebus.spi.SimpleMessageBus;
45:
46: /***************************************************************************************************************************************************************
47: *
48: * A base class for JavaFX applications with use Spring annotation scanning.
49: *
50: * @author Fabrizio Giudici
51: * @since 1.1-ALPHA-4
52: *
53: **************************************************************************************************************************************************************/
54: public class JavaFXSpringAnnotationApplication extends AbstractJavaFXSpringApplication
55: {
56: static class Beans
57: {
58: @Bean
59: public ThreadPoolTaskExecutor javafxBinderExecutor()
60: {
61: return JavaFXSafeComponentBuilder.getExecutor();
62: }
63:
64: @Bean
65: public PreferencesHandler preferencesHandler()
66: {
67: return PreferencesHandler.getInstance();
68: }
69:
70: @Bean
71: public JavaFXToolBarControl toolBarControl()
72: {
73: return JavaFXSafeComponentBuilder.getToolBarControl();
74: }
75:
76: @Bean
77: public JavaFXMenuBarControl menuBarControl()
78: {
79: return JavaFXSafeComponentBuilder.getMenuBarControl();
80: }
81:
82: @Bean
83: public JavaFXPanelGroupControl panelGroupControl (@Nonnull final BeanFactory beanFactory)
84: {
85: return new DefaultJavaFXPanelGroupControl(beanFactory);
86: }
87: }
88:
89: static class MessageBusBeans
90: {
91: @Bean(name = APPLICATION_MESSAGE_BUS_BEAN_NAME)
92: public MessageBus applicationMessageBus()
93: {
94: final var executor = new ThreadPoolTaskExecutor();
95: executor.setWaitForTasksToCompleteOnShutdown(false);
96: executor.setThreadNamePrefix("messageBus-");
97: executor.setCorePoolSize(10);
98: executor.setMaxPoolSize(10);
99: executor.setQueueCapacity(10);
100: executor.afterPropertiesSet();
101: return new SimpleMessageBus(executor);
102: }
103: }
104:
105: public static final String APPLICATION_MESSAGE_BUS_BEAN_NAME = "applicationMessageBus";
106:
107: // Don't use Slf4j and its static logger - give Main a chance to initialize things
108: private final Logger log = LoggerFactory.getLogger(JavaFXSpringAnnotationApplication.class);
109:
110: @Override @Nonnull
111: protected ConfigurableApplicationContext createApplicationContext()
112: {
113: final var mainClass = getClass();
114: final var componentClasses = new ArrayList<>(List.of(mainClass, Beans.class));
115:
116: if (getClass().isAnnotationPresent(EnableMessageBus.class))
117: {
118: log.info("Detected @{}, enabling message bus", EnableMessageBus.class.getSimpleName());
119: componentClasses.add(MessageBusBeans.class);
120: }
121:
122: log.info("Scanning beans from {}", mainClass);
123: return new AnnotationConfigApplicationContext(componentClasses.toArray(new Class<?>[0]));
124: }
125: }