How to integrate Spring Boot, Camel and Mybatis - java

Need to Integrate Camel and MyBatis with application using SpringBoot.
SpringBoot provides out-of-box support for Camel and MyBatis. Also provides Camel and MyBatis SpringBoot starters.
However, when i try to integrate Spring Boot application with Camel and MyBatis, it fails.
I am using Java DSL based Camel Route. Also using Mybatis Spring boot dependencies. Annotate mappers, added database properties in the application.properties file. What I was expecting to happen:
1) SpringBoot setup datasource and mappers, sqlsessionfactory on start-up.
2) Next the Camel-MyBatis consumer is called, the setup done in (1) would allow, Camel to successfully make database calls using mybatis.
I created spring annotated configuration class and used it to create/get DataSource bean.
How can i get Camel to use this dataSource bean?
How to tell Camel to use newly build SQL session factory, instead of it trying to build from configuration file?
Created sample appl in github, its using in-memory db (h2)
sample-app
Getting NPE
Consumer[mybatis://getClaimInfo?statementType=SelectOne] failed polling endpoint: Endpoint[mybatis://getClaimInfo?statementType=SelectOne]. Will try again at next poll. Caused by: [org.apache.ibatis.exceptions.PersistenceException -
Error opening session. Cause: java.lang.NullPointerException
Cause: java.lang.NullPointerException]
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30) ~[mybatis-3.4.0.jar:3.4.0]
at org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSessionFromDataSource(DefaultSqlSessionFactory.java:100) ~[mybatis-3.4.0.jar:3.4.0]
at org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSession(DefaultSqlSessionFactory.java:47) ~[mybatis-3.4.0.jar:3.4.0]

I have been able to use Spring Boot 1.3.6, Apache Camel 2.17.2 with Mybatis-Spring-Boot-Starter 1.1.1 successfully:
Key dependencies in maven:
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-mybatis</artifactId>
<exclusions>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring-boot</artifactId>
</dependency>
Key bean to declare
#Bean(name="mybatis")
public MyBatisComponent myBatisComponent( SqlSessionFactory sqlSessionFactory )
{
MyBatisComponent result = new MyBatisComponent();
result.setSqlSessionFactory( sqlSessionFactory );
return result;
}

Related

Httpclient and Zipkin Java

My and project team are looking to add Zipkin logging and tracing to our current project. We are working in an microservice environment using Spring Boot (Java 17) and cloud foundery. For the communication between Microservices we are using HttpClient. From what I've gathered from the documentation Zipkin requires an RestTemplate to function. However we don't have time to change this.
We were able to implement Zipkin in every individual project. However, every call generates their own Trace ID. I think we need to configure the HttpClient to work in tandem with Zipkin, however the documentation is not very clear and I have been unable to find anything that explains how to do this.
What can I try on this? I've included the config and dependencies below.
spring:
application:
name: Application_1
zipkin:
baseUrl: http://localhost:9411
sleuth:
sampler:
probability: 1.0
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-zipkin</artifactId>
<version>3.1.3</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
<version>3.1.3</version>
</dependency>

How can I ensure that one spring-boot-starter is loaded before another?

I'm trying to ensure that one Spring Boot Starter is loaded before another. In this case, I have a custom spring boot starter written by my organization and the spring-boot-starter-data-jpa starter. It's crucial that the custom starter's bean is ran before spring-boot-starter-data-jpa is initialized. An example snippet of the dependencies in pom.xml:
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>spring-boot-starter-my-customer-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
How would one go about this? The solution I'm currently considering is adding a #Primary annotation to the top of the bean initialized in my company's custom starter, not sure if this would work and looking for best practice here. The bean I'm attempting to load in the custom starter before spring-boot-starter-jpa's beans are loaded is responsible for setting proxy settings, therefore must be loaded first so that traffic to the database can be routed via proxy.
Not really sure as to what you are trying to achieve here, but a way of controlling bean creation could be done using the DependsOn annotation.
In you case, since you would like to initialize your custom bean before the actual database connection is made, I think that adding this to your database configuration class should be enough.

SpringBoot configuration with pure Hibernate ORM without JPA

I'm new to SpringBoot development and I want to add Hibernate ORM framework to my SpringBoot project. I know
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
this dependency gives that capability but it's not supporting for every Hibernate features in pure Hibernate framework.
Example:
session.createNativeQuery(query).addEntity(LectureSchedule.class).list();
is there any way to configure Hibernate framework for SpringBoot applications not via JPA dependency and autowire SessionFactory so I can perform basic hibernate coding after that without using JPA methods?
If you don't want to use the spring starter pom file, you can add the hibernate dependencies one by one - depending on your needs.
The minimums are the hibernate-core package and spring-orm.
This tutorial describes how to do it step by step.

Is it necessary to add Spring Web when using Spring Actuator?

I'm trying to in corporate Spring Actuator to my application. I have added the dependency in my pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>1.4.2.RELEASE</version>
</dependency>
But I get a 404 when trying to access the /health endpoint. After looking online, I've read that I need to also have the spring-boot-starter-web dependency in my POM. I was under the assumption that I only need the actuator dependency in order to get it working
Yes web is needed if you want to access via HTTP (otherwise only JMX is available).
The documentation for actuator states
"Click Dependencies and select Spring Web and Spring Boot Actuator."

Camel AMQP - AMQConnectionFactory ClassNotFound

I'm using Camel 2.13.3 and trying to establish a connection via AMQP to a remote ActiveMQ instance.
According to the Camel AMQP docs is should be sufficient to add the following dependency
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-amqp</artifactId>
<version>2.13.1</version>
</dependency>
It then indicates that you should configure the jms component to use a connection factory supplied by the QPID project. The docs page uses org.apache.qpid.amqp_1_0.jms.impl.ConnectionFactoryImpl, and the results of other google searches indicate that org.apache.qpid.client.AMQConnectionFactory could be used.
However, the org.apache.qpid dependencies do not appear to have been added to the project and, unsurprisingly, I get a ClassNotFoundException when I run it.
I considered downloading the qpid dependency separately, but their web site seems to indicate that the qpid client project has been deprecated and replaced by something else ( QPID Messaging API if I remember correctly )
Can anyone point me in the right direction?
should be sufficient
The Camel docs you linked to does not state that. It just says this dependency is needed, doesn't say anything about additional dependencies. Just looked inside the jar you're using, and it does not contain qpid-client classes. You should add that dependency to your pom as well. For AMQP 0.x, there is a good chance you'll need JMS spec dependency as well:
<dependency>
<groupId>org.apache.qpid</groupId>
<artifactId>qpid-client</artifactId>
<version>0.32</version> <!-- replace with appropriate version -->
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-jms_1.1_spec</artifactId>
<version>1.0</version>
</dependency>
If you're using AMQP 1.0,
<dependency>
<groupId>org.apache.qpid</groupId>
<artifactId>qpid-jms-client</artifactId>
<version>0.3.0</version>
</dependency>

Categories