void addMissingBeans()

in safeguard-impl/src/main/java/org/apache/safeguard/impl/cdi/SafeguardExtension.java [166:206]


    void addMissingBeans(@Observes final AfterBeanDiscovery afterBeanDiscovery) {
        final GeronimoFaultToleranceConfig config = GeronimoFaultToleranceConfig.create();
        afterBeanDiscovery.addBean()
                .id("geronimo_safeguard#configuration")
                .types(GeronimoFaultToleranceConfig.class, Object.class)
                .beanClass(GeronimoFaultToleranceConfig.class)
                .qualifiers(Default.Literal.INSTANCE, Any.Literal.INSTANCE)
                .scope(ApplicationScoped.class)
                .createWith(c -> config);

        afterBeanDiscovery.addBean()
                .id("geronimo_safeguard#metrics")
                .types(FaultToleranceMetrics.class, Object.class)
                .beanClass(FaultToleranceMetrics.class)
                .qualifiers(Default.Literal.INSTANCE, Any.Literal.INSTANCE)
                .scope(ApplicationScoped.class)
                .createWith(c -> FaultToleranceMetrics.create(config));

        if (!foundExecutor) {
            afterBeanDiscovery.addBean()
                    .id("geronimo_safeguard#executor")
                    .types(Executor.class, Object.class)
                    .beanClass(Executor.class)
                    .qualifiers(Safeguard.Literal.INSTANCE, Any.Literal.INSTANCE)
                    .createWith(c -> Executors.newCachedThreadPool(new ThreadFactory() {
                        private final ThreadGroup group = ofNullable(System.getSecurityManager())
                                .map(SecurityManager::getThreadGroup)
                                .orElseGet(() -> Thread.currentThread().getThreadGroup());
                        private final String prefix = "org.apache.geronimo.safeguard.asynchronous@" +
                                System.identityHashCode(this);
                        private final AtomicLong counter = new AtomicLong();

                        @Override
                        public Thread newThread(final Runnable r) {
                            return new Thread(group, r, prefix + counter.incrementAndGet());
                        }
                    }))
                    .scope(ApplicationScoped.class)
                    .destroyWith((e, c) -> ExecutorService.class.cast(e).shutdownNow());
        }
    }