copy and paste service-a for add cqrs design
This commit is contained in:
@ -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) {
|
||||
|
@ -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;
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -23,7 +23,7 @@ public class PersonController {
|
||||
@PostMapping("/list")
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
public String listPosts() {
|
||||
return "bbb";
|
||||
return "aaa";
|
||||
}
|
||||
|
||||
@CrossOrigin(origins = "http://localhost:3001")
|
||||
|
35
src/main/java/com/example/mmad/testapp/controller/UserController.java
Executable file
35
src/main/java/com/example/mmad/testapp/controller/UserController.java
Executable 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);
|
||||
}
|
||||
}
|
25
src/main/java/com/example/mmad/testapp/entity/UserEntity.java
Executable file
25
src/main/java/com/example/mmad/testapp/entity/UserEntity.java
Executable 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;
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
@ -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());
|
||||
}
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
package com.example.mmad.testapp.query;
|
||||
|
||||
public class UserReadModel {
|
||||
}
|
@ -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> {
|
||||
}
|
10
src/main/java/com/example/mmad/testapp/repository/UserRepository.java
Executable file
10
src/main/java/com/example/mmad/testapp/repository/UserRepository.java
Executable 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> {
|
||||
|
||||
}
|
@ -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
|
||||
|
Reference in New Issue
Block a user