add oracle datasource and tables for test cqrs and event driven read model now call rest from gateway and save in command db in srivce a and save on query db serivce a then send to kafka message queue and save on snapshot tabel on service b

This commit is contained in:
esmailian
2025-04-29 12:25:54 +03:30
parent 1f0e42de4b
commit d32b4d1381
11 changed files with 111 additions and 12 deletions

View File

@ -54,6 +54,11 @@
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>19.8.0.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>

View File

@ -22,7 +22,7 @@ public class UserCommandHandler {
userEntity.setId(createUserCommand.getUserId());
userEntity.setUserName(createUserCommand.getUserName());
userEntity.setEmail(createUserCommand.getEmail());
// userRepository.save(userEntity);
userRepository.save(userEntity);
UserCreatedEvent event = new UserCreatedEvent(
userEntity.getId(),
userEntity.getUserName(),

View File

@ -8,18 +8,18 @@ import static jakarta.persistence.GenerationType.SEQUENCE;
@Entity
@Getter
@Setter
@Table(schema = "testapp", name = "user")
@Table(schema = "TEST", name = "USERS")
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class UserEntity {
@Id
@SequenceGenerator(name = "seqTest", sequenceName = "testapp.person_seq", allocationSize = 1)
@SequenceGenerator(name = "seqTest", sequenceName = "USER_SEQ", allocationSize = 1)
@GeneratedValue(strategy = SEQUENCE, generator = "seqTest")
private Long id;
@Column(name = "user_name")
private String userName;
@Column(name = "email")
private String email;
}
}

View File

@ -0,0 +1,26 @@
package com.example.mmad.testapp.entity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.*;
@Entity
@Getter
@Setter
@Table(schema = "TEST", name = "USERS_QUERY")
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class UserQueryEntity {
@Id
// @SequenceGenerator(name = "seqTest", sequenceName = "USER_SEQ", allocationSize = 1)
// @GeneratedValue(strategy = SEQUENCE, generator = "seqTest")
private Long id;
@Column(name = "user_name")
private String userName;
@Column(name = "email")
private String email;
}

View File

@ -1,27 +1,34 @@
package com.example.mmad.testapp.query;
import com.example.mmad.testapp.entity.UserQueryEntity;
import com.example.mmad.testapp.event.UserCreatedEvent;
import com.example.mmad.testapp.kafkaProducerConfig.KafkaProducerConfig;
import com.example.mmad.testapp.repository.UserReadRepository;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class UserQueryHandler {
private static final String kafkaTopic = "user-created";
private final KafkaProducerConfig kafkaProducer;
private final UserReadRepository userReadRepository;
public UserQueryHandler(KafkaProducerConfig kafkaProducer) {
public UserQueryHandler(KafkaProducerConfig kafkaProducer, UserReadRepository userReadRepository) {
this.kafkaProducer = kafkaProducer;
this.userReadRepository = userReadRepository;
}
@Async("taskExecutor")
@EventListener
public void handelUserCreated(UserCreatedEvent event) {
kafkaProducer.kafkaTemplate().send(kafkaTopic, event);
// persist in query data base
UserQueryEntity userQueryEntity = new UserQueryEntity();
userQueryEntity.setId(event.getUserId());
userQueryEntity.setUserName(event.getUsername());
userQueryEntity.setEmail(event.getEmail());
//TODO mapper needed
userReadRepository.save(userQueryEntity);
System.out.println("Received user created event" + event.getUserId());
}
}

View File

@ -1,9 +1,9 @@
package com.example.mmad.testapp.repository;
import com.example.mmad.testapp.entity.PersonEntity;
import com.example.mmad.testapp.entity.UserQueryEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserReadRepository extends JpaRepository<PersonEntity, Long> {
public interface UserReadRepository extends JpaRepository<UserQueryEntity, Long> {
}

View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">
<property name="now" value="UNIX_TIMESTAMP()" dbms="mysql"/>
<property name="now" value="sysdate" dbms="oracle"/>
<property name="now" value="now()" dbms="postgresql"/>
<changeSet id="1.1-1-mmad" author="mmad">
<insert schemaName="test" tableName="person">
<column name="id" value="1"/>
<column name="first_name" value="mohammad"/>
<column name="last_name" value="Reza"/>
</insert>
</changeSet>
<changeSet id="1.1-2-sequence" author="mmad">
<createSequence sequenceName="USER_SEQ" startValue="1" incrementBy="1"/>
</changeSet>
</databaseChangeLog>

View File

@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">
<!-- Set correct time function for Oracle -->
<property name="now" value="SYSDATE" dbms="oracle"/>
<changeSet id="1.2-1-mmad" author="mmad">
<createTable schemaName="test" tableName="PERSON" remarks="اشخاص">
<column name="ID" type="NUMBER(19)" remarks="شناسه اشخاص">
<constraints nullable="false" primaryKey="true" primaryKeyName="PK_PERSON_ID"/>
</column>
<column name="FIRST_NAME" type="VARCHAR2(255)" remarks="نام"/>
<column name="LAST_NAME" type="VARCHAR2(255)" remarks="نام خانوادگی"/>
</createTable>
</changeSet>
<changeSet id="1.2-2-mrmad" author="mmad">
<createTable schemaName="test" tableName="USERS" remarks="کاربر">
<column name="ID" type="NUMBER(19)" remarks="شناسه کاربر">
<constraints nullable="false" primaryKey="true" primaryKeyName="PK_USER_ID"/>
</column>
<column name="user_name" type="VARCHAR2(255)" remarks="نام کاربری"/>
<column name="email" type="VARCHAR2(255)" remarks="ایمیل"/>
</createTable>
</changeSet>
<changeSet id="1.2-3-mrmad" author="mmad">
<createTable schemaName="test" tableName="USERS_QUERY" remarks="کاربر">
<column name="ID" type="NUMBER(19)" remarks="شناسه کاربر">
<constraints nullable="false" primaryKey="true" primaryKeyName="PK_USER_QUERY_ID"/>
</column>
<column name="user_name" type="VARCHAR2(255)" remarks="نام کاربری"/>
<column name="email" type="VARCHAR2(255)" remarks="ایمیل"/>
</createTable>
</changeSet>
</databaseChangeLog>

View File

@ -4,7 +4,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">
<include file="changes/liquibase-1.2.xml" relativeToChangelogFile="true"/>
<include file="changes/liquibase-1.1.xml" relativeToChangelogFile="true"/>
<include file="changes/oracleLiquibases/liquibase-1.2.xml" relativeToChangelogFile="true"/>
<include file="changes/oracleLiquibases/liquibase-1.1.xml" relativeToChangelogFile="true"/>
</databaseChangeLog>