Created
August 5, 2020 00:05
-
-
Save pollend/a4dbf5ed6e2fcc65fa64ca166d336aec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Factory | |
| @Requires(property = IgniteAbstractConfiguration.PREFIX + ".enabled", value = "true", defaultValue = "false") | |
| public class IgniteFactory implements AutoCloseable{ | |
| private static final Logger LOG = LoggerFactory.getLogger(IgniteFactory.class); | |
| private final ResourceResolver resourceResolver; | |
| private final IgniteConfig igniteConfiguration; | |
| private final AbstractApplicationContext applicationContext; | |
| private final Map<String, Ignite> clients = new ConcurrentHashMap<>(10); | |
| private BeanContext beanContext; | |
| public IgniteFactory(ResourceResolver resourceResolver, IgniteConfig igniteConfiguration, BeanContext beanContext) { | |
| this.resourceResolver = resourceResolver; | |
| this.igniteConfiguration = igniteConfiguration; | |
| this.beanContext = beanContext; | |
| // Optional<URL> template = resourceResolver.getResource(this.igniteConfiguration.getConfigFile()); | |
| // if (!template.isPresent()) { | |
| // throw new RuntimeException("failed to find configuration: " + this.igniteConfiguration.getConfigFile()); | |
| // } | |
| applicationContext = new FileSystemXmlApplicationContext(this.igniteConfiguration.getConfigFile()); | |
| registerConfig(IgniteConfiguration.class, applicationContext, beanContext); | |
| registerConfig(NearCacheConfiguration.class, applicationContext, beanContext); | |
| registerConfig(CacheConfiguration.class, applicationContext, beanContext); | |
| beanContext.refresh(); | |
| } | |
| private <T> void registerConfig(Class<T> target, AbstractApplicationContext applicationContext, BeanContext beanContext) { | |
| Map<String, T> targets = applicationContext.getBeansOfType(target); | |
| for (Map.Entry<String, T> entry : targets.entrySet()) { | |
| if (entry.getKey().equals("default")) { | |
| beanContext.registerSingleton(target, entry.getValue(), Qualifiers.byStereotype(Primary.class), true); | |
| } else { | |
| beanContext.registerSingleton(target, entry.getValue(), Qualifiers.byName(entry.getKey()), true); | |
| } | |
| } | |
| } | |
| @Singleton | |
| @EachBean(IgniteConfiguration.class) | |
| public Ignite createIgnite(IgniteConfiguration configuration) { | |
| // BeanDefinition<?> beanDefinition = injectionPoint.getDeclaringBean(); | |
| // final Qualifier qualifier; | |
| // final Qualifier declaredQualifier = beanDefinition.getDeclaredQualifier(); | |
| // if (declaredQualifier == null) { | |
| // qualifier = Qualifiers.byQualifiers(Qualifiers.byStereotype(Primary.class)); | |
| // } else { | |
| // qualifier = declaredQualifier; | |
| // } | |
| // IgniteConfiguration configuration = (IgniteConfiguration) beanContext.findBean(IgniteConfiguration.class, qualifier).orElseThrow(() -> new IllegalStateException("can't find configuration:" + qualifier.toString())); | |
| return Ignition.start(configuration); | |
| } | |
| /** | |
| * Create a singleton {@link IgniteClient} client, based on an existing {@link IgniteThinClientConfiguration} bean. | |
| * | |
| * @param configuration the configuration read it as a bean | |
| * @return {@link IgniteClient} | |
| */ | |
| @Singleton | |
| @EachBean(IgniteThinClientConfiguration.class) | |
| @Bean(preDestroy = "close") | |
| public IgniteClient createIgniteThinClient(IgniteThinClientConfiguration configuration) { | |
| try { | |
| return Ignition.startClient(configuration.getConfiguration()); | |
| } catch (Exception e) { | |
| LOG.error("Failed to instantiate Ignite Client: " + e.getMessage(), e); | |
| throw e; | |
| } | |
| } | |
| @Override | |
| public void close() throws Exception { | |
| Ignition.stopAll(true); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment