quarkusio / quarkus

Quarkus: Supersonic Subatomic Java.
https://quarkus.io
Apache License 2.0
13.7k stars 2.66k forks source link

Is Quarkus supposed to be supported by AWS CDK ? #40748

Closed nicolasduminil closed 4 months ago

nicolasduminil commented 4 months ago

Describe the bug

Hello,

I'm trying to use Quarkus 3.10.0 in an AWS CDK service. To simplify, I need to use MP Config in CDK apps, for example:

@ApplicationScoped
public class MyStack extends Stack
{
  @Inject
  @ConfigProperty(defaultValue = "function.zip", name="zip-location")
  private String zipLocation;
  ...
  public MyStack()
  {
      ...
      System.out.println (">>> zipLocation: " + zipLocation);
      ...
  }
  ...
}

There is no any application.properties file and, hence, I expect that the defaultValue be assigned. But the line which is printed out here looks as follows:

    >>> zipLocation: null

meaning that the injection hasn't been done. I'm not sure why ?

Doing the same in a Quarkus test, as shown below, works as expected:

@QuarkusTest
public class MyStackIT
{
  @Inject
  @ConfigProperty(defaultValue = "function.zip", name="zip-location")
  private String zipLocation;

  @Test
  public void test123()
  {
    assertThat(zipLocation).isEqualTo("function.zip");
  }
}

Is that because, in the first case, the injection isn't yet effective in the constructor ? Or does it have anything to do with AWS CDK ? To resume, is Quarkus supposed to work with CDK at all ? I don't see any reason it doesn't but you never know.

Many thanks in advance.

Kind regards,

Nicolas

Expected behavior

I'd expect that the config injection be effective in an AWS Stack having the ApplicationScoped.

Actual behavior

Actually, the injected property has the value null.

How to Reproduce?

Don't have a reproducer

Output of uname -a or ver

Linux nicolas-XPS-15-9570 5.15.0-107-generic #117-Ubuntu SMP Fri Apr 26 12:26:49 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux

Output of java -version

java version "21.0.3" 2024-04-16 LTS Java(TM) SE Runtime Environment (build 21.0.3+7-LTS-152) Java HotSpot(TM) 64-Bit Server VM (build 21.0.3+7-LTS-152, mixed mode, sharing)

Quarkus version or git rev

3.10.0

Build tool (ie. output of mvnw --version or gradlew --version)

Apache Maven 3.9.5 (57804ffe001d7215b5e7bcb531cf83df38f93546) Maven home: /opt/apache-maven-3.9.5 Java version: 21.0.3, vendor: Oracle Corporation, runtime: /usr/lib/jvm/jdk-21-oracle-x64 Default locale: en_US, platform encoding: UTF-8 OS name: "linux", version: "5.15.0-107-generic", arch: "amd64", family: "unix"

Additional information

N/A

geoand commented 4 months ago

Hi,

I am not sure. but I assume that this one should be reported at https://docs.quarkiverse.io/quarkus-amazon-services/dev/index.html, no?

gsmet commented 4 months ago

I think in this very case, you need to use constructor injection. I'm not 100% sure this works with @ConfigProperty but I believe so, so maybe start with trying that.

nicolasduminil commented 4 months ago

@gsmet : Thank you for your suggestion. I'm not sure what exactly makes this "very case" a special one but I think that the constructor injection is never used with MP Config. Perhaps, I need to mention that AWS CDK apps are executed via cdk synth and cdk deploy commands. These commands rely on the presence of a cdk.json file which contains, besides others, the Maven command required to run the application. This file is generated by the cdk init java and the generated Maven command is:

mvn -e -q compile exec:java

The associated POM uses the exec-maven-plugin as shown below:

      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <configuration>
          <mainClass>...MyCdkApp</mainClass>
        </configuration>
      </plugin>

In this example, MyCdkApp has a main() method looking as follows:

public class MyCdkApp
{
  public static void main(String... args) 
  {
    App app = new App();
    new MyStack(app, "QuarkusApiGatewayStack", "QuarkusApiGatewayLambda");
    app.synth();
  }
}

Here, MyStack is the class which is listed above, in my initial post and which tries to use MP Config to inject properties. The app.synth() statement is creating the associated CloudFormation stack.

So, the idea is to run a Java class having a main() method with the exec-maven-plugin in which to instantiate an @ApplicationScoped bean that, in turn, uses @ConfigProperty. I've seen the documentation explaining how to write Quarkus apps having a main() method but I don't think that this would be the case here. Or is it ?

nicolasduminil commented 4 months ago

@geoand : the link you've provided leads to the page attached below where it doesn't seem possible to report anything. Screenshot_20240522_143347

geoand commented 4 months ago

https://github.com/quarkiverse/quarkus-amazon-services

nicolasduminil commented 4 months ago

@geoand : yes, this one is fine, thanks.

nicolasduminil commented 4 months ago

Basically, my problem comes from this statement:

new MyStack(app, "QuarkusApiGatewayStack", "QuarkusApiGatewayLambda");

Given that the MyStack class is created by invoking its constructor, Quarkus doesn't inject the properties. I tried to inject this class instead of calling its constructor but than CDK doesn't seem to be able to "see" it and returns the error message The app contains no stack. I need to find a way to either manually instantiate the class while being handled by Quarkus (probably impossible) or to inject it. Waiting for that, I'm closing the case.