Repository: quarkusio/quarkus
Issue: NPE in ArtemisJmsProducer.connectionFactory()

**Describe the bug**
A minimal application using `quarkus-artemis-jms` fails to start with strange NPE. See full code below.

**Expected behavior**
The application should start and initialize JMS ConnectionFactory without any exceptions.

**Actual behavior**
NPE immediately after startup:
```
2021-01-15 21:49:53,977 ERROR [io.qua.run.Application] (Quarkus Main Thread) Failed to start application (with profile dev): java.lang.NullPointerException
	at io.quarkus.artemis.jms.runtime.ArtemisJmsProducer.connectionFactory(ArtemisJmsProducer.java:21)
	at io.quarkus.artemis.jms.runtime.ArtemisJmsProducer_ProducerMethod_connectionFactory_77384d97dd5fab56fb7d89e045176235cbda65cd_Bean.create(ArtemisJmsProducer_ProducerMethod_connectionFactory_77384d97dd5fab56fb7d89e045176235cbda65cd_Bean.zig:166)
	at io.quarkus.artemis.jms.runtime.ArtemisJmsProducer_ProducerMethod_connectionFactory_77384d97dd5fab56fb7d89e045176235cbda65cd_Bean.create(ArtemisJmsProducer_ProducerMethod_connectionFactory_77384d97dd5fab56fb7d89e045176235cbda65cd_Bean.zig:197)
	at io.quarkus.arc.impl.AbstractSharedContext.createInstanceHandle(AbstractSharedContext.java:96)
	at io.quarkus.arc.impl.AbstractSharedContext.access$000(AbstractSharedContext.java:14)
	at io.quarkus.arc.impl.AbstractSharedContext$1.get(AbstractSharedContext.java:29)
	at io.quarkus.arc.impl.AbstractSharedContext$1.get(AbstractSharedContext.java:26)
	at io.quarkus.arc.impl.LazyValue.get(LazyValue.java:26)
	at io.quarkus.arc.impl.ComputingCache.computeIfAbsent(ComputingCache.java:69)
	at io.quarkus.arc.impl.AbstractSharedContext.get(AbstractSharedContext.java:26)
	at io.quarkus.arc.impl.ClientProxies.getApplicationScopedDelegate(ClientProxies.java:17)
	at io.quarkus.artemis.jms.runtime.ArtemisJmsProducer_ProducerMethod_connectionFactory_77384d97dd5fab56fb7d89e045176235cbda65cd_ClientProxy.arc$delegate(ArtemisJmsProducer_ProducerMethod_connectionFactory_77384d97dd5fab56fb7d89e045176235cbda65cd_ClientProxy.zig:68)
	at io.quarkus.artemis.jms.runtime.ArtemisJmsProducer_ProducerMethod_connectionFactory_77384d97dd5fab56fb7d89e045176235cbda65cd_ClientProxy.createContext(ArtemisJmsProducer_ProducerMethod_connectionFactory_77384d97dd5fab56fb7d89e045176235cbda65cd_ClientProxy.zig:146)
	at Bean2.doSomething(Bean2.java:16)
	at Bean2_ClientProxy.doSomething(Bean2_ClientProxy.zig:155)
	at Bean1.onStart(Bean1.java:10)
	at Bean1_Observer_onStart_31a1bd3f2053c759a740ca423787158a0219f934.notify(Bean1_Observer_onStart_31a1bd3f2053c759a740ca423787158a0219f934.zig:179)
	at io.quarkus.arc.impl.EventImpl$Notifier.notifyObservers(EventImpl.java:282)
	at io.quarkus.arc.impl.EventImpl$Notifier.notify(EventImpl.java:267)
	at io.quarkus.arc.impl.EventImpl.fire(EventImpl.java:69)
	at io.quarkus.arc.runtime.LifecycleEventRunner.fireStartupEvent(LifecycleEventRunner.java:23)
	at io.quarkus.arc.runtime.ArcRecorder.handleLifecycleEvents(ArcRecorder.java:60)
	at io.quarkus.deployment.steps.LifecycleEventsBuildStep$startupEvent-858218658.deploy_0(LifecycleEventsBuildStep$startupEvent-858218658.zig:81)
	at io.quarkus.deployment.steps.LifecycleEventsBuildStep$startupEvent-858218658.deploy(LifecycleEventsBuildStep$startupEvent-858218658.zig:40)
	at io.quarkus.runner.ApplicationImpl.doStart(ApplicationImpl.zig:400)
	at io.quarkus.runtime.Application.start(Application.java:90)
	at io.quarkus.runtime.ApplicationLifecycleManager.run(ApplicationLifecycleManager.java:97)
	at io.quarkus.runtime.Quarkus.run(Quarkus.java:62)
	at io.quarkus.runtime.Quarkus.run(Quarkus.java:38)
	at io.quarkus.runtime.Quarkus.run(Quarkus.java:104)
	at io.quarkus.runner.GeneratedMain.main(GeneratedMain.zig:29)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at io.quarkus.runner.bootstrap.StartupActionImpl$3.run(StartupActionImpl.java:134)
	at java.base/java.lang.Thread.run(Thread.java:834)
```


**To Reproduce**

Create a project consisting of the following two Java source files and a pom.xml, then run `mvn quarkus:dev`.

```java
import io.quarkus.runtime.StartupEvent;

import javax.enterprise.event.Observes;
import javax.inject.Singleton;

@Singleton
public class Bean1 {

  public void onStart(@Observes StartupEvent ev, Bean2 bean2) {
    bean2.doSomething();
  }
}
```

```java
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.jms.ConnectionFactory;

@ApplicationScoped
public class Bean2 {

  private final ConnectionFactory connectionFactory;

  @Inject
  public Bean2(ConnectionFactory connectionFactory) {
    this.connectionFactory = connectionFactory;
  }

  public void doSomething() {
    connectionFactory.createContext();
  }
}
```

```xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>reproducer</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <quarkus.version>1.10.5.Final</quarkus.version>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>io.quarkus</groupId>
                <artifactId>quarkus-maven-plugin</artifactId>
                <version>${quarkus.version}</version>
                <executions>
                    <execution>
                        <id>quarkus-build</id>
                        <goals>
                            <goal>build</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>io.quarkus</groupId>
                <artifactId>quarkus-bom</artifactId>
                <version>${quarkus.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-artemis-jms</artifactId>
        </dependency>
    </dependencies>

</project>
```

**Configuration**
```properties
quarkus.artemis.url=tcp://localhost:61616
```

**Environment (please complete the following information):**
 - Output of `uname -a`: `Linux 5.4.48-gentoo #2 SMP Wed Oct 7 20:15:36 MSK 2020 x86_64 Intel(R) Core(TM) i7-8850H CPU @ 2.60GHz GenuineIntel GNU/Linux`
 - Output of `java -version`: `openjdk version "11.0.9.1" 2020-11-04 LTS`
 - Quarkus version or git rev: `1.10.5.Final`
 - Build tool (output of `mvnw --version`): `Apache Maven 3.6.3`


Changed files:
extensions/artemis-jms/deployment/src/main/java/io/quarkus/artemis/jms/deployment/ArtemisJmsProcessor.java
extensions/artemis-jms/runtime/src/main/java/io/quarkus/artemis/jms/runtime/ArtemisJmsProducer.java
extensions/artemis-jms/runtime/src/main/java/io/quarkus/artemis/jms/runtime/ArtemisJmsRecorder.java

diff --git a/extensions/artemis-jms/deployment/src/main/java/io/quarkus/artemis/jms/deployment/ArtemisJmsProcessor.java b/extensions/artemis-jms/deployment/src/main/java/io/quarkus/artemis/jms/deployment/ArtemisJmsProcessor.java
index a2343bbc064..f8621d8fd8f 100644
--- a/extensions/artemis-jms/deployment/src/main/java/io/quarkus/artemis/jms/deployment/ArtemisJmsProcessor.java
+++ b/extensions/artemis-jms/deployment/src/main/java/io/quarkus/artemis/jms/deployment/ArtemisJmsProcessor.java
@@ -1,11 +1,12 @@
 package io.quarkus.artemis.jms.deployment;
 
-import io.quarkus.arc.deployment.AdditionalBeanBuildItem;
-import io.quarkus.arc.deployment.BeanContainerBuildItem;
+import javax.enterprise.context.ApplicationScoped;
+import javax.jms.ConnectionFactory;
+
+import io.quarkus.arc.deployment.SyntheticBeanBuildItem;
 import io.quarkus.artemis.core.deployment.ArtemisBuildTimeConfig;
 import io.quarkus.artemis.core.deployment.ArtemisJmsBuildItem;
 import io.quarkus.artemis.core.runtime.ArtemisRuntimeConfig;
-import io.quarkus.artemis.jms.runtime.ArtemisJmsProducer;
 import io.quarkus.artemis.jms.runtime.ArtemisJmsRecorder;
 import io.quarkus.deployment.Feature;
 import io.quarkus.deployment.annotations.BuildProducer;
@@ -18,12 +19,10 @@
 public class ArtemisJmsProcessor {
 
     @BuildStep
-    void load(BuildProducer<AdditionalBeanBuildItem> additionalBean, BuildProducer<FeatureBuildItem> feature,
-            BuildProducer<ArtemisJmsBuildItem> artemisJms) {
+    void load(BuildProducer<FeatureBuildItem> feature, BuildProducer<ArtemisJmsBuildItem> artemisJms) {
 
         artemisJms.produce(new ArtemisJmsBuildItem());
         feature.produce(new FeatureBuildItem(Feature.ARTEMIS_JMS));
-        additionalBean.produce(AdditionalBeanBuildItem.unremovableOf(ArtemisJmsProducer.class));
     }
 
     @BuildStep
@@ -36,9 +35,17 @@ HealthBuildItem health(ArtemisBuildTimeConfig buildConfig) {
     @Record(ExecutionTime.RUNTIME_INIT)
     @BuildStep
     ArtemisJmsConfiguredBuildItem configure(ArtemisJmsRecorder recorder, ArtemisRuntimeConfig runtimeConfig,
-            BeanContainerBuildItem beanContainer) {
+            BuildProducer<SyntheticBeanBuildItem> syntheticBeanProducer) {
+
+        SyntheticBeanBuildItem connectionFactory = SyntheticBeanBuildItem.configure(ConnectionFactory.class)
+                .supplier(recorder.getConnectionFactorySupplier(runtimeConfig))
+                .scope(ApplicationScoped.class)
+                .defaultBean()
+                .unremovable()
+                .setRuntimeInit()
+                .done();
+        syntheticBeanProducer.produce(connectionFactory);
 
-        recorder.setConfig(runtimeConfig, beanContainer.getValue());
         return new ArtemisJmsConfiguredBuildItem();
     }
 }
diff --git a/extensions/artemis-jms/runtime/src/main/java/io/quarkus/artemis/jms/runtime/ArtemisJmsProducer.java b/extensions/artemis-jms/runtime/src/main/java/io/quarkus/artemis/jms/runtime/ArtemisJmsProducer.java
deleted file mode 100644
index 7e4748c7a17..00000000000
--- a/extensions/artemis-jms/runtime/src/main/java/io/quarkus/artemis/jms/runtime/ArtemisJmsProducer.java
+++ /dev/null
@@ -1,32 +0,0 @@
-package io.quarkus.artemis.jms.runtime;
-
-import javax.enterprise.context.ApplicationScoped;
-import javax.enterprise.inject.Produces;
-import javax.jms.ConnectionFactory;
-
-import org.apache.activemq.artemis.jms.client.ActiveMQJMSConnectionFactory;
-
-import io.quarkus.arc.DefaultBean;
-import io.quarkus.artemis.core.runtime.ArtemisRuntimeConfig;
-
-@ApplicationScoped
-public class ArtemisJmsProducer {
-
-    private ArtemisRuntimeConfig config;
-
-    @Produces
-    @ApplicationScoped
-    @DefaultBean
-    public ConnectionFactory connectionFactory() {
-        return new ActiveMQJMSConnectionFactory(config.url,
-                config.username.orElse(null), config.password.orElse(null));
-    }
-
-    public ArtemisRuntimeConfig getConfig() {
-        return config;
-    }
-
-    public void setConfig(ArtemisRuntimeConfig config) {
-        this.config = config;
-    }
-}
diff --git a/extensions/artemis-jms/runtime/src/main/java/io/quarkus/artemis/jms/runtime/ArtemisJmsRecorder.java b/extensions/artemis-jms/runtime/src/main/java/io/quarkus/artemis/jms/runtime/ArtemisJmsRecorder.java
index fffef19ee83..8069ced0bba 100644
--- a/extensions/artemis-jms/runtime/src/main/java/io/quarkus/artemis/jms/runtime/ArtemisJmsRecorder.java
+++ b/extensions/artemis-jms/runtime/src/main/java/io/quarkus/artemis/jms/runtime/ArtemisJmsRecorder.java
@@ -1,13 +1,23 @@
 package io.quarkus.artemis.jms.runtime;
 
-import io.quarkus.arc.runtime.BeanContainer;
+import java.util.function.Supplier;
+
+import javax.jms.ConnectionFactory;
+
+import org.apache.activemq.artemis.jms.client.ActiveMQJMSConnectionFactory;
+
 import io.quarkus.artemis.core.runtime.ArtemisRuntimeConfig;
 import io.quarkus.runtime.annotations.Recorder;
 
 @Recorder
 public class ArtemisJmsRecorder {
 
-    public void setConfig(ArtemisRuntimeConfig config, BeanContainer container) {
-        container.instance(ArtemisJmsProducer.class).setConfig(config);
+    public Supplier<ConnectionFactory> getConnectionFactorySupplier(ArtemisRuntimeConfig config) {
+        return new Supplier<ConnectionFactory>() {
+            @Override
+            public ConnectionFactory get() {
+                return new ActiveMQJMSConnectionFactory(config.url, config.username.orElse(null), config.password.orElse(null));
+            }
+        };
     }
 }