copy and paste service-a for add cqrs design

This commit is contained in:
esmailian
2025-04-28 08:06:35 +03:30
parent 59cdcac476
commit f40f78cf8b
13 changed files with 196 additions and 2 deletions

View File

@ -3,9 +3,11 @@ package com.example.mmad.testapp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.scheduling.annotation.EnableAsync;
@SpringBootApplication
@EnableDiscoveryClient
@EnableAsync
public class TestAAppApplication {
public static void main(String[] args) {

View File

@ -0,0 +1,13 @@
package com.example.mmad.testapp.command;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class CreateUserCommand {
private Long userId;
private String userName;
private String email;
}

View File

@ -0,0 +1,33 @@
package com.example.mmad.testapp.command;
import com.example.mmad.testapp.entity.UserEntity;
import com.example.mmad.testapp.event.UserCreatedEvent;
import com.example.mmad.testapp.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
@Service
public class UserCommandHandler {
@Autowired
private ApplicationEventPublisher applicationEventPublisher;
private final UserRepository userRepository;
public UserCommandHandler(UserRepository userRepository) {
this.userRepository = userRepository;
}
public void createUser(CreateUserCommand createUserCommand) {
UserEntity userEntity = new UserEntity();
userEntity.setId(createUserCommand.getUserId());
userEntity.setUserName(createUserCommand.getUserName());
userEntity.setEmail(createUserCommand.getEmail());
// userRepository.save(userEntity);
UserCreatedEvent event = new UserCreatedEvent(
userEntity.getId(),
userEntity.getUserName(),
userEntity.getEmail()
);
applicationEventPublisher.publishEvent(event);
}
}

View File

@ -0,0 +1,23 @@
package com.example.mmad.testapp.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
@Configuration()
@EnableAsync
public class AsyncConfig {
@Bean(name = "taskExecutor")
public Executor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(5);
executor.setMaxPoolSize(10);
executor.setQueueCapacity(500);
executor.setThreadNamePrefix("taskExecutor-");
executor.initialize();
return executor;
}
}

View File

@ -23,7 +23,7 @@ public class PersonController {
@PostMapping("/list")
@ResponseStatus(HttpStatus.OK)
public String listPosts() {
return "bbb";
return "aaa";
}
@CrossOrigin(origins = "http://localhost:3001")

View File

@ -0,0 +1,35 @@
package com.example.mmad.testapp.controller;
import com.example.mmad.testapp.command.CreateUserCommand;
import com.example.mmad.testapp.command.UserCommandHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
@RestController
@RequestMapping("/user")
public class UserController {
private UserCommandHandler userCommandHandler;
@Autowired
public void setPersonService(UserCommandHandler userCommandHandler) {
this.userCommandHandler = userCommandHandler;
}
@PostMapping("/list")
@ResponseStatus(HttpStatus.OK)
public String listPosts() {
return "userHandler";
}
@CrossOrigin(origins = "http://localhost:3001")
@PostMapping("/create")
public ResponseEntity<?> create(@Valid @RequestBody CreateUserCommand userCommand) {
userCommandHandler.createUser(userCommand);
return ResponseEntity.ok(200);
}
}

View File

@ -0,0 +1,25 @@
package com.example.mmad.testapp.entity;
import jakarta.persistence.*;
import lombok.*;
import static jakarta.persistence.GenerationType.SEQUENCE;
@Entity
@Getter
@Setter
@Table(schema = "testapp", name = "user")
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class UserEntity {
@Id
@SequenceGenerator(name = "seqTest", sequenceName = "testapp.person_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,25 @@
package com.example.mmad.testapp.event;
public class UserCreatedEvent {
private final Long userId;
private final String username;
private final String email;
public UserCreatedEvent(Long userId, String username, String email) {
this.userId = userId;
this.username = username;
this.email = email;
}
public Long getUserId() {
return userId;
}
public String getUsername() {
return username;
}
public String getEmail() {
return email;
}
}

View File

@ -0,0 +1,15 @@
package com.example.mmad.testapp.query;
import com.example.mmad.testapp.event.UserCreatedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
@Component
public class UserQueryHandler {
@Async("taskExecutor")
@EventListener
public void handelUserCreated(UserCreatedEvent event) {
System.out.println("Received user created event" + event.getUserId());
}
}

View File

@ -0,0 +1,4 @@
package com.example.mmad.testapp.query;
public class UserReadModel {
}

View File

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

View File

@ -0,0 +1,10 @@
package com.example.mmad.testapp.repository;
import com.example.mmad.testapp.entity.UserEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends JpaRepository<UserEntity, Long> {
}

View File

@ -1,4 +1,4 @@
spring.application.name=service-b
spring.application.name=service-a
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
spring.config.import=configserver:http://localhost:8888/
eureka.client.register-with-eureka=true