支持rabbitmq

This commit is contained in:
2024-03-28 23:46:32 +08:00
parent f7328f2498
commit 8f8f0ca83d
6 changed files with 78 additions and 4 deletions

View File

@@ -51,6 +51,10 @@
</pluginRepositories>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.reflections</groupId>

View File

@@ -27,6 +27,9 @@ import org.apache.commons.lang3.StringUtils;
import org.jgrapht.graph.DefaultDirectedGraph;
import org.jgrapht.graph.DefaultEdge;
import org.jgrapht.traverse.TopologicalOrderIterator;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.io.ClassPathResource;
@@ -43,8 +46,8 @@ import java.util.Optional;
@Service
@Slf4j
@Transactional
public
class CoalParameterDefService extends BaseService<CoalParameterDefEntity, CoalParameterDefRepository> {
public class CoalParameterDefService
extends BaseService<CoalParameterDefEntity, CoalParameterDefRepository> {
@Autowired CoalParameterDefRepository repository;
@@ -177,6 +180,22 @@ class CoalParameterDefService extends BaseService<CoalParameterDefEntity, CoalPa
.forEach(organizationEntity -> initDefault(organizationEntity.getId()));
}
@RabbitListener(
bindings = {
@QueueBinding(
value =
@org.springframework.amqp.rabbit.annotation.Queue(
value = "coalParameterDef.init",
durable = "true"),
exchange =
@org.springframework.amqp.rabbit.annotation.Exchange(
value = "sysExchange", declare = Exchange.FALSE),
key = "organization.create")
})
public void onOrganizationCreate(String orgId) {
initDefault(orgId);
}
@SneakyThrows
public void initDefault(String orgId) {

View File

@@ -10,6 +10,7 @@ import cn.lihongjie.coal.organization.dto.UpdateOrganizationDto;
import cn.lihongjie.coal.organization.entity.OrganizationEntity;
import cn.lihongjie.coal.organization.mapper.OrganizationMapper;
import cn.lihongjie.coal.organization.repository.OrganizationRepository;
import cn.lihongjie.coal.rabbitmq.RabbitMQService;
import cn.lihongjie.coal.user.dto.CreateOrgAdminDto;
import cn.lihongjie.coal.user.service.UserService;
@@ -17,6 +18,7 @@ import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.ConversionService;
import org.springframework.data.domain.Page;
@@ -42,10 +44,13 @@ class OrganizationService extends BaseService<OrganizationEntity, OrganizationRe
@Autowired CoalParameterDefService coalParameterDefService;
@Autowired RabbitMQService rabbitMQService;
@PostConstruct
public void init() {}
@Autowired RabbitTemplate rabbitTemplate;
public OrganizationDto create(CreateOrganizationDto request) {
OrganizationEntity entity = mapper.toEntity(request);
@@ -57,7 +62,7 @@ class OrganizationService extends BaseService<OrganizationEntity, OrganizationRe
dto.setUsername(request.getOrgAdminUserName());
dto.setPassword(request.getOrgAdminPassword());
userService.createOrgAdmin(dto);
coalParameterDefService.initDefault(entity.getId());
rabbitMQService.sendToSysExchange("organization.create", entity.getId());
return getById(entity.getId());
}
@@ -66,7 +71,7 @@ class OrganizationService extends BaseService<OrganizationEntity, OrganizationRe
this.mapper.updateEntity(entity, request);
this.repository.save(entity);
rabbitMQService.sendToSysExchange("organization.update", entity.getId());
return getById(entity.getId());
}

View File

@@ -0,0 +1,20 @@
package cn.lihongjie.coal.rabbitmq;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.*;
@EnableRabbit
@Configuration
public class RabbitMQConfiguration {
@Bean
public TopicExchange topicExchange() {
return new TopicExchange("sysExchange", true, false);
}
}

View File

@@ -0,0 +1,20 @@
package cn.lihongjie.coal.rabbitmq;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class RabbitMQService {
@Autowired RabbitTemplate rabbitTemplate;
public void send(String exchange, String routingKey, String data){
rabbitTemplate.convertAndSend(exchange, routingKey, data);
}
public void sendToSysExchange(String routingKey, String data) {
send("sysExchange", routingKey, data);
}
}

View File

@@ -40,6 +40,12 @@ management:
spring:
rabbitmq:
host: ${RABBITMQ_HOST:localhost}
port: ${RABBITMQ_PORT:5672}
username: ${RABBITMQ_USER:coal}
password: ${RABBITMQ_PASSWORD:coal}
virtual-host: ${RABBITMQ_VHOST:/coal}
quartz:
jdbc:
initialize-schema: NEVER