Quarkus service to use another Quarkus project service - quarkus

I would like to load one Quarkus project in my other main Quarkus project.
I tried #ApplicationScoped in the subproject and #Inject in the main project however object is not getting initialized.
it throws error javax.enterprise.inject.UnsatisfiedResolutionException: Unsatisfied dependency for type
if Initialize with new it works
Like
#Inject
Foo foo
throws error however
Foo foo = new Foo();
will work but it won't initialize the quarks objects like mongoClinet .. etc.

Related

Spring Boot not able to pick value from yml file

I have a application-local.yml file inside resources folder. I have some properties like
data: https://xyzw/
I am using this property in a different class
#Service
public class Test {
#Value("${data}")
private String data;
}
When I run the Application.java I get the following error:
Application.main(Application.java:13), exception_class=org.springframework.beans.factory.BeanCreationException, exception_message=Error creating bean with name 'Test': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'data' in value "${data}"}","threadID":"main","sourceHost":"H18NPLFI13P0303","logVersion":"1.5","category":"org.springframework.boot.SpringApplication"}
Which is weird. I am not sure why it's not picking the values from the application-local.yml file.
If you start your application with IntelliJ IDEA and want the application-local.yml properties to be used, you need to edit your run configuration so that the local profile is used, else the application-local.yml properties won't be picked up by Spring.

Running 'mvn clean install' on Spring Boot application using env variables in application.properties

Hello I am trying to package my Spring Boot app into a jar.
I want to deploy this app to AWS Beanstalk and so I will be injecting some variables into application.properties using Environment variables.
spring.data.mongodb.uri=${MONGODB_URI}
spring.data.mongodb.auto-index-creation=true
spring.servlet.multipart.max-file-size=-1
spring.servlet.multipart.max-request-size=-1
CLOUDINARY_URL=${CLOUDINARY_URL}
jwt-secret=${JWT_SECRET}
server.port=5000
However when I run the maven command (mvn clean install), during the package process the code is executed and it is failing stating that
BeanCreationException: Error creating bean with name 'customBeansConfig': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'CLOUDINARY_URL' in value "${CLOUDINARY_URL}"
I have a class CustomBeansConfig:
#Configuration
public class CustomBeansConfig {
#Value("${CLOUDINARY_URL}")
private String cloudinaryUrl;
#Bean
public Cloudinary cloudinary(){
System.out.println("cloudinaryUrl = " + cloudinaryUrl);
return new Cloudinary(cloudinaryUrl);
}
}
Please help me to create the jar file
If I have understood you correctly, one approach may be to use different application.properties files for different environments. For example application-dev.properties for the Dev environment and application-prod.properties for the Prod environment. Then your CLOUDINARY_URL may be assigned different literal values appropriate to each.
Then when deploying to each environment, bundle your JAR with the -Denv option, as in
mvn -Denv=dev clean install
OR
mvn -Denv=prod clean install
... and upload the resulting JAR file to the corresponding AWS environment.
Running the Spring Boot application with a such config property, got me the following error:
Caused by: java.lang.IllegalArgumentException: Circular placeholder reference 'CLOUDINARY_URL' in property definitions
Changing the name of your Spring property from CLOUDINARY_URL to, for example, cloudinary.service.url will resolve the issue.
In such case, your config file should look like this:
spring.data.mongodb.uri=${MONGODB_URI}
spring.data.mongodb.auto-index-creation=true
spring.servlet.multipart.max-file-size=-1
spring.servlet.multipart.max-request-size=-1
cloudinary.service.url=${CLOUDINARY_URL}
jwt-secret=${JWT_SECRET}
server.port=5000
And your configuration file like this:
#Configuration
public class CustomBeansConfig {
#Value("${cloudinary.service.url}")
private String cloudinaryUrl;
#Bean
public Cloudinary cloudinary(){
System.out.println("cloudinaryUrl = " + cloudinaryUrl);
return new Cloudinary(cloudinaryUrl);
}
}
Also, I would advise you to avoid creating Spring configuration properties using the underscore format, since it usually used for the environment variables, maybe be confusing and may cause such interesting issues.

Cannot instantiate interface 'ElasticsearchClient'

The official Elasticsearch docs tell to instantiate ElasticsearchClient like this:
ElasticsearchClient client = new ElasticsearchClient(transport);
Once I write this in my Grails 3 application with Gradle build management, I get the following compilation error: Cannot instantiate interface 'ElasticsearchClient'
The import statement is: import org.elasticsearch.client.ElasticsearchClient.
Indeed, ElasticsearchClient gets resolved to:
package org.elasticsearch.client;
// ...
public interface ElasticsearchClient {
The Gradle dependency is: compile 'org.elasticsearch:elasticsearch:7.17.2'
Why do they propose to instantiate an interface in their docs?
What can I do to make it compile and use the Elasticsearch client?
The reason for this was: ElasticsearchClient was mis-resolved to an old elasticsearch JAR file that had still been in my local user's .gradle directory. Removing the old JAR from .gradle enabled me to do the correct import.
The correct import is
import co.elastic.clients.elasticsearch.ElasticsearchClient
instead of
import org.elasticsearch.client.ElasticsearchClient

Spring - Run test from IDE - how to load test properties from a file like 'application-test.properties'

How can I load test properties from a file like 'application-test.properties'?
The file is stored in the src/test/resources folder. I put the file also in all possible folders as well. When running the test as part of the Maven test run, all works fine.
When running the new (single) test from the (IntelliJ) IDE, each time I get same the error message:
Caused by: java.io.FileNotFoundException: class path resource
[application-test.properties] cannot be opened because it does not
exist
This is the test class:
#RunWith(SpringRunner.class)
#EnableAutoConfiguration
#ComponentScan(basePackages = {"nl.deholtmans.tjm1706.todolist"})
#PropertySource( "application-test.properties")
public class TodoListServiceTest {
#Autowired
TodoListService todoListService;
#Test
public void testBasic() { ... }
It looks that I have to run the test first time from Maven. Why is that?
Spring Boot will automatically load the correct properties file if the profile is activated. In a test you can use the #ActiveProfiles annotation for that.
Next you would need to make sure that you actually use the proper Spring Boot infrastructure to run your test, using #SpringBootTest. That being said your test header should look something like the following
#RunWith(SpringRunner.class)
#SpringBootTest
#ActiveProfiles("test")
public class TodoListServiceTest { ... }
And ofcourse make sure that your IDE builds the application before running the tests.

Error: Could not initialize class ru.yandex.clickhouse.ClickHouseUtil

I'm using clickhouse-jdbc in my java application. And I'm adding it to pom.xml like this:
<dependency>
<groupId>ru.yandex.clickhouse</groupId>
<artifactId>clickhouse-jdbc</artifactId>
<version>0.1.34</version>
</dependency>
And when I run my java application java -jar myapp.jar. It's throwing:
java.lang.NoClassDefFoundError: Could not initialize class
ru.yandex.clickhouse.ClickHouseUtil
And in my packaged jar file, there is also ClickHouseUtil.class. And I'm using Intellij Idea for packaging jar. How can I fix this issue?
The error 'Could not initialize class ....' means that the JVM has already tried and failed to perform static initialization of the class mentioned.
Static initialization of a class involves assigning values to any static fields and running any static { ... } blocks. The class in question is ru.yandex.clickhouse.ClickHouseUtil, and static initialization of this class consists only of setting up the static final field CLICKHOUSE_ESCAPER. This appears to rely on a couple of Guava escaper classes (com.google.common.escape.Escaper and com.google.common.escape.Escapers).
So I suspect that these Guava classes aren't in your packaged JAR file.
It's also worth pointing out that the exception message 'Could not initialize class ....' means that static initialization has already failed. In other words, when this exception is thrown, it is at least the second time the JVM has failed to load the class. It's possible that your app may have reported a more informative error message when the JVM failed to load this class for the first time.

Resources