ElasticSearchHighLevelClient springboot Autoconfiguration not working - spring-boot

I am trying to autoconfigure elasticsearch high-level rest client using this documentation
https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-elasticsearch
I added the following properties to my application.properties:
spring.elasticsearch.rest.uris=https://hostname.com
spring.elasticsearch.rest.read-timeout=10s
spring.elasticsearch.rest.username= user
spring.elasticsearch.rest.password= password
I added these dependencies in my pom.xml
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
and a property
<elasticsearch.version>7.2.1</elasticsearch.version>
In my application I did this
#Configuration
#EnableAutoConfiguration
#SpringBootApplication
#ComponentScan
public class MainClass {
public static void main(String[] args) {
SpringApplication.run(MainClass.class, args);
}
}
But when I try to use the HighLevelClient it gives me NullPointerException
#Autowired
private RestHighLevelClient client;
Am I missing something?

You should add the stacktrace for the NullPointerException.
You did not write which versions you use (Spring Boot, Spring Data Elasticsearch). But the current Spring Boot 2.2.2 targets Spring Data Elasticsearch 3.2.x which uses Elasticsearch 6.8.5. So this won't work with an Elasticsearch 7 cluster. Support for Elasticsearch 7 is coming with the next version (Spring Data Elasticsearch 4.0)

I hope you are using spring-boot-autoconfigure dependency because RestHighLevelClient bean is initialized by this library based on the presence of RestHighLevelClient in the classpath. Check the dependency tree for the presence of spring-boot-autoconfigure.

Related

camel springboot and hawtio integration

I am working on integrating the camel springboot component with hawtio. I can't see the camel tab in the hawtio UI. I am using below dependencies of springboot camel and hawtio
<dependency>
<groupId>org.apache.camel.springboot</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>io.hawt</groupId>
<artifactId>hawtio-springboot</artifactId>
<version>2.10.1</version>
</dependency>
This the sample route i configured, route is working as expected
#Component
public class SampleRoute extends org.apache.camel.builder.RouteBuilder{
#Override
public void configure() throws Exception {
from("kafka:test123?brokers=localhost:9092&allowManualCommit=true&autoCommitEnable=false&breakOnFirstError=true&maxPollRecords=1&synchronous=true")
.process(exchange -> {
exchange.getIn().setBody("test1234");
}).to("stream:out");
}
}
After reading many articles , i added below set of properties and it seems to have any effect.
management.endpoints.web.exposure.include=hawtio,jolokia
management.endpoints.jolokia.sensitive=false
endpoints.jolokia.sensitive = false
hawtio.authenticationEnabled=false
spring.jmx.enabled=true
camel.springboot.endpoint-runtime-statistics-enabled=true
management.endpoint.camelroutes.enabled=true
management.info.camel.enabled=true
management.endpoint.camelroutes.enabled=true
management.endpoint.camelroutes.read-only=true
camel.springboot.jmx-enabled=true
One of the solution worked for me is by downgrading to camel 2.X , but no luck with camel 3.X
I got it working, following the link as suggested. I had to remove the conflicting dependencies and adding below dependency worked.
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-management</artifactId>
<version>3.2.0</version>
</dependency>

Spring boot 2 with apache camel application start error

I have a requirement where I need to listen to a active mq and call rest post api for the data I have recieved as body, for this I have created simple spring boot 2.x application and trying to start a router, but application is failing to start.
My understanding about registering a Router is to anottate a class with #Component and it should implement camel RouteBuilder
#Component
public class Router extends RouteBuilder{
#Override
public void configure() throws Exception {
from("timer:foo").to("log:bar");
}
}
JDK 1.8
Spring boot version 2.2.6.RELEASE
Camel version 2.24.0
Pom xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
<version>2.24.0</version>
</dependency>
</dependencies>
The error I am getting is
Caused by: java.lang.ClassNotFoundException: org.apache.camel.spring.spi.XmlCamelContextConfigurer
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581) ~[na:na]
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178) ~[na:na]
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521) ~[na:na]
Try removing the camel-spring dependency. This should be pulled transitively through camel-spring-boot-starter.
The error message could be a problem with different Camel versions. For example if ${camel.version} in your POM is not defined or not equal to 2.24.0 you could have mixed Camel versions for camel-spring and camel-core.
If there will be any resource can't able to downloaded in your system so for this you have to uninstall the application and delete all the files from c/program-file/ and then install it again. Hope it will help you out from this issue.

Tracing SQL Queries with X-Ray and Spring Boot 2

The current X-Ray SQL tracing interceptor uses Tomcat JDBC Pool but Spring Boot 2 uses HikariCP as default pool, is it possible to configure the jdbc tracing in HikariCP instead?
Here (https://forums.aws.amazon.com/thread.jspa?threadID=254847) they suggest to use both Datasources:
DataSource dataSource = new org.apache.tomcat.jdbc.pool.DataSource();
HikariDataSource hikariDataSource = new HikariDataSource();
... // data source configuration
dataSource.setJdbcInterceptors("com.amazonaws.xray.sql.postgres.TracingInterceptor;");
hikariDataSource.setDataSource(dataSource);
But if I have the HikariCP library in the classpath spring will configure that as datasource.
I've tried with a DatasourceBuilder and also forcing the type using the parameter spring.datasource.type
Any hint?
In Spring boot , you can use still use Tomcat over HikariCP as connection pool:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<exclusions>
<exclusion>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</dependency>
We are currently investigating a solution that will work with both Tomcat JDBC and HikariCP. We are aware that there are currently no work arounds without having Tomcat JDBC as a dependency. Please stay tuned.
Resolved the same using TracingDataSource, while still using HikariCP as the connection pool
Reference https://github.com/aws/aws-xray-sdk-java/issues/88#issuecomment-570328275
Code: (Note, I am using AWS Secrets Manager JDBC Library aws-secretsmanager-jdbc to connect to the database using secrets stored in AWS Secrets Manager)
import com.amazonaws.xray.sql.TracingDataSource;
...
...
#Bean
#ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() {
return TracingDataSource
.decorate(DataSourceBuilder.create()
.driverClassName("com.amazonaws.secretsmanager.sql.AWSSecretsManagerPostgreSQLDriver")
.url("jdbc-secretsmanager:postgresql://" + System.getenv("PGHOST") + ":"
+ System.getenv("PGPORT") + "/" + System.getenv("PGDATABASE"))
.username(System.getenv("SECRET_NAME")).build());
}
Dependency:
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-xray-recorder-sdk-sql</artifactId>
</dependency>

using embedded amqp swith spring boot and camel

I want to integration test a custom camel component and therefor need an embedded/in memory messanging I can easily use to test from/to endpoints.
I am hoping that I can achieve this via spring-boot-amqp-starter.
I used this example for a start, which has the dependencies:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-broker</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
</dependency>
and the config:
spring:
activemq:
broker-url: vm://embedded?broker.persistent=false,useShutdownHook=false
// ...
This is working, when I use regular Listener annotations in spring, I have a sender using the template and a consumer logging the messages.
Now I go one step further and use camel, but it does not recognize the vm:embedded broker but tries to connect to tcp://localhost, which is not running.
return new RouteBuilder() {
#Override
public void configure() throws Exception {
from("activemq:foo").to("log:sample");
from("timer:bar").setBody(constant("Hello from Camel")).to("activemq:foo");
}
};
How can I configure activemq-camel to use the embedded broker?
Update:
I use dependency management imports for spring-boot-dependencies (1.5.9)
and camel-spring-boot-dependencies (2.20.1).
This has been fixed in newer versions of activemq-camel when you use that with Spring Boot. Now the activemq-camel component will honor the spring boot configuration of the spring.activemq.* settings.

Cannot get spring-data-neo4j to connect to remote database

I am building a small proof of concept Spring Boot app which is supposed to connect to a Neo4j instance and perform some basic operations on a couple of different Nodes. If I have the main application class wired to create an embedded Neo4j service using the following code, everything works fine. (this is based on the working example https://spring.io/guides/gs/accessing-neo4j-data-rest/)
#Bean(destroyMethod = "shutdown")
public GraphDatabaseService graphDatabaseService() {
return new GraphDatabaseFactory().newEmbeddedDatabase("target/hello.db");
}
This is the only code sample I can find though for connecting to a Neo4j server from spring boot. If I try connecting to a remote server, the code fails to start with the exception at the end of this question. Our plan is to run a centralized Neo4j instance which is obviously a common production requirement.
How can or should I configure my bean to connect to a remote Neo4j database or is anyone aware of a solid working example of this kind of setup?
Thanks!
My pom.xml includes the following:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-neo4j</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-neo4j-rest</artifactId>
<version>3.3.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
I have seen several references to using SpringCypherRestGraphDatabase so I have this being handled in my Main application class is as follows:
import org.neo4j.graphdb.GraphDatabaseService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.neo4j.config.EnableNeo4jRepositories;
import org.springframework.data.neo4j.config.Neo4jConfiguration;
import org.springframework.data.neo4j.rest.SpringCypherRestGraphDatabase;
#SpringBootApplication
#EnableNeo4jRepositories
public class ProfileServiceApplication extends Neo4jConfiguration {
public ProfileServiceApplication() {
setBasePackage("profile");
}
#Bean
public GraphDatabaseService graphDatabaseService() {
return new SpringCypherRestGraphDatabase("http://localhost:7474/db/data/","neo4j","password");
}
public static void main(String[] args) {
SpringApplication.run(ProfileServiceApplication.class, args);
}
}
When I try to run with this configuration, I get the following error:
nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.neo4j.graphdb.GraphDatabaseService]: Circular reference involving containing bean 'profileServiceApplication' - consider declaring the factory method as static for independence from its containing instance. Factory method 'graphDatabaseService' threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/data/neo4j/core/UpdateableState
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1210)
Please share your application as github-project for testing. Perhaps it is also a dependency issue of the spring-boot-starter? Which boot version are you using??
Perhaps you can check mvn dependency:tree if there is any older version of SDN pulled in and if so, update the neo4-version-property for the spring-boot-starter.
Example application is here:
http://neo4j.com/developer/java/#_using_spring_data_neo4j
and it is also in the docs as you correctly saw:
http://docs.spring.io/spring-data/data-neo4j/docs/3.3.0.RELEASE/reference/html/#using_spring_data_neo4j_as_a_neo4j_server_client
The reference which you are giving doesn't contain Neo4j URL.
Hence Here is Application.properties file to connect to your Neo4j.
spring.data.neo4j.uri=bolt://localhost
spring.data.neo4j.username=neo4j
spring.data.neo4j.password=Your_Password
Start your Neo4j Database as a console application which will run on localhost:7474 port most probably.
command to start:
neo4j console
Also check the dependency. As the latest version work with only
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-neo4j</artifactId>
</dependency>

Resources