♻️ feat(新增): module转成kotlin
This commit is contained in:
parent
5fc8e1b6fe
commit
ddbda4bbef
|
@ -11,6 +11,7 @@ import org.springframework.web.bind.annotation.RestControllerAdvice
|
|||
import java.io.FileNotFoundException
|
||||
import java.nio.file.AccessDeniedException
|
||||
import java.sql.SQLIntegrityConstraintViolationException
|
||||
import java.util.regex.Pattern
|
||||
|
||||
|
||||
@RestControllerAdvice
|
||||
|
@ -31,9 +32,7 @@ class GlobalExceptionHandler {
|
|||
// 运行时异常信息
|
||||
@ExceptionHandler(RuntimeException::class)
|
||||
@ResponseBody
|
||||
@Throws(
|
||||
FileNotFoundException::class
|
||||
)
|
||||
@Throws(FileNotFoundException::class)
|
||||
fun exceptionHandler(exception: RuntimeException): Result<Any?> {
|
||||
logger.error("GlobalExceptionHandler===>运行时异常信息:{}", exception.message)
|
||||
exception.printStackTrace()
|
||||
|
@ -46,7 +45,16 @@ class GlobalExceptionHandler {
|
|||
fun error(exception: Exception): Result<Any?> {
|
||||
logger.error("GlobalExceptionHandler===>系统异常信息:{}", exception.message)
|
||||
|
||||
return Result.error(null, 500, "系统异常")
|
||||
// 错误消息
|
||||
val message = exception.message ?: ""
|
||||
// 匹配到内容
|
||||
val patternString = "Request method '(\\w+)' is not supported"
|
||||
val matcher = Pattern.compile(patternString).matcher(message)
|
||||
|
||||
return when {
|
||||
matcher.find() -> Result.error(null, 500, "请求方法错误,不是 " + matcher.group(1))
|
||||
else -> Result.error(null, 500, "系统异常")
|
||||
}
|
||||
}
|
||||
|
||||
// 特定异常处理
|
||||
|
|
|
@ -1,26 +1,26 @@
|
|||
package cn.bunny.module.minio.properties;
|
||||
package cn.bunny.module.minio.properties
|
||||
|
||||
import io.minio.MinioClient;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import io.minio.MinioClient
|
||||
import lombok.Data
|
||||
import lombok.extern.slf4j.Slf4j
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties
|
||||
import org.springframework.context.annotation.Bean
|
||||
import org.springframework.context.annotation.Configuration
|
||||
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "bunny.minio")
|
||||
@ConditionalOnProperty(name = "bunny.minio.bucket-name")// 当属性有值时这个配置才生效
|
||||
@ConditionalOnProperty(name = ["bunny.minio.bucket-name"]) // 当属性有值时这个配置才生效
|
||||
@Data
|
||||
@Slf4j
|
||||
public class MinioProperties {
|
||||
private String endpointUrl;
|
||||
private String accessKey;
|
||||
private String secretKey;
|
||||
private String bucketName;
|
||||
open class MinioProperties {
|
||||
val endpointUrl: String? = null
|
||||
val accessKey: String? = null
|
||||
val secretKey: String? = null
|
||||
val bucketName: String? = null
|
||||
|
||||
@Bean
|
||||
public MinioClient minioClient() {
|
||||
return MinioClient.builder().endpoint(endpointUrl).credentials(accessKey, secretKey).build();
|
||||
open fun minioClient(): MinioClient {
|
||||
return MinioClient.builder().endpoint(endpointUrl).credentials(accessKey, secretKey).build()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,38 +1,40 @@
|
|||
package cn.bunny.module.rabbitMQ.config;
|
||||
package cn.bunny.module.rabbitMQ.config
|
||||
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
|
||||
import org.springframework.amqp.support.converter.MessageConverter;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.stereotype.Component;
|
||||
import jakarta.annotation.PostConstruct
|
||||
import org.apache.logging.log4j.LogManager
|
||||
import org.springframework.amqp.core.ReturnedMessage
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate
|
||||
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter
|
||||
import org.springframework.amqp.support.converter.MessageConverter
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.context.annotation.Bean
|
||||
import org.springframework.stereotype.Component
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class RabbitMqConfig {
|
||||
class RabbitMqConfig {
|
||||
private val logger = LogManager.getLogger(RabbitMqConfig::class.java)
|
||||
|
||||
@Autowired
|
||||
private RabbitTemplate rabbitTemplate;
|
||||
private val rabbitTemplate: RabbitTemplate? = null
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
rabbitTemplate.setReturnsCallback(returned -> {
|
||||
log.error("触发return callback,");
|
||||
log.debug("exchange: {}", returned.getExchange());
|
||||
log.debug("routingKey: {}", returned.getRoutingKey());
|
||||
log.debug("message: {}", returned.getMessage());
|
||||
log.debug("replyCode: {}", returned.getReplyCode());
|
||||
log.debug("replyText: {}", returned.getReplyText());
|
||||
});
|
||||
fun init() {
|
||||
rabbitTemplate!!.setReturnsCallback { returned: ReturnedMessage ->
|
||||
logger.error("触发return callback,")
|
||||
logger.debug("exchange: {}", returned.exchange)
|
||||
logger.debug("routingKey: {}", returned.routingKey)
|
||||
logger.debug("message: {}", returned.message)
|
||||
logger.debug("replyCode: {}", returned.replyCode)
|
||||
logger.debug("replyText: {}", returned.replyText)
|
||||
}
|
||||
}
|
||||
|
||||
@Bean
|
||||
public MessageConverter messageConverter() {
|
||||
fun messageConverter(): MessageConverter {
|
||||
// 1.定义消息转换器
|
||||
Jackson2JsonMessageConverter converter = new Jackson2JsonMessageConverter();
|
||||
val converter = Jackson2JsonMessageConverter()
|
||||
// 2.配置自动创建消息id,用于识别不同消息,也可以在业务中基于ID判断是否是重复消息
|
||||
converter.setCreateMessageIds(true);
|
||||
return converter;
|
||||
converter.setCreateMessageIds(true)
|
||||
return converter
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
package cn.bunny.module.rabbitMQ.consumer;
|
||||
package cn.bunny.module.rabbitMQ.consumer
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
import lombok.extern.slf4j.Slf4j
|
||||
import org.springframework.stereotype.Component
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class DelayConsumer {
|
||||
}
|
||||
class DelayConsumer
|
||||
|
|
|
@ -1,24 +1,25 @@
|
|||
package cn.bunny.module.rabbitMQ.consumer;
|
||||
package cn.bunny.module.rabbitMQ.consumer
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import lombok.extern.slf4j.Slf4j
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.stereotype.Component
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class DirectConsumer {
|
||||
class DirectConsumer {
|
||||
@Autowired
|
||||
RabbitTemplate rabbitTemplate;
|
||||
var rabbitTemplate: RabbitTemplate? = null
|
||||
|
||||
/**
|
||||
* 发送红色消息
|
||||
*/
|
||||
@Test
|
||||
void testSendDirectRed() throws Exception {
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
rabbitTemplate.convertAndSend("bunny.direct", "red", "发送消息:" + i);
|
||||
@Throws(Exception::class)
|
||||
fun testSendDirectRed() {
|
||||
for (i in 0..999) {
|
||||
rabbitTemplate!!.convertAndSend("bunny.direct", "red", "发送消息:$i")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
package cn.bunny.module.rabbitMQ.consumer;
|
||||
package cn.bunny.module.rabbitMQ.consumer
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
import lombok.extern.slf4j.Slf4j
|
||||
import org.springframework.stereotype.Component
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class ErrorConsumer {
|
||||
}
|
||||
class ErrorConsumer
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
package cn.bunny.module.rabbitMQ.consumer;
|
||||
package cn.bunny.module.rabbitMQ.consumer
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
import lombok.extern.slf4j.Slf4j
|
||||
import org.springframework.stereotype.Component
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class FanoutConsumer {
|
||||
}
|
||||
class FanoutConsumer
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
package cn.bunny.module.rabbitMQ.consumer;
|
||||
package cn.bunny.module.rabbitMQ.consumer
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
import lombok.extern.slf4j.Slf4j
|
||||
import org.springframework.stereotype.Component
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class LazyConsumer {
|
||||
}
|
||||
class LazyConsumer
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
package cn.bunny.module.rabbitMQ.consumer;
|
||||
package cn.bunny.module.rabbitMQ.consumer
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
import lombok.extern.slf4j.Slf4j
|
||||
import org.springframework.stereotype.Component
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class TopicConsumer {
|
||||
}
|
||||
internal class TopicConsumer
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
package cn.bunny.module.rabbitMQ.listener;
|
||||
package cn.bunny.module.rabbitMQ.listener
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
import lombok.extern.slf4j.Slf4j
|
||||
import org.springframework.stereotype.Component
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class DelayListener {
|
||||
}
|
||||
class DelayListener
|
||||
|
|
|
@ -1,16 +1,16 @@
|
|||
package cn.bunny.module.rabbitMQ.listener;
|
||||
package cn.bunny.module.rabbitMQ.listener
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.amqp.core.ExchangeTypes;
|
||||
import org.springframework.amqp.rabbit.annotation.Exchange;
|
||||
import org.springframework.amqp.rabbit.annotation.Queue;
|
||||
import org.springframework.amqp.rabbit.annotation.QueueBinding;
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
import lombok.extern.slf4j.Slf4j
|
||||
import org.springframework.amqp.core.ExchangeTypes
|
||||
import org.springframework.amqp.rabbit.annotation.Exchange
|
||||
import org.springframework.amqp.rabbit.annotation.Queue
|
||||
import org.springframework.amqp.rabbit.annotation.QueueBinding
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitListener
|
||||
import org.springframework.stereotype.Component
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class DirectListener {
|
||||
class DirectListener {
|
||||
/**
|
||||
* * 监听者2
|
||||
* 创建队列 持久化的、不自动删除
|
||||
|
@ -19,12 +19,23 @@ public class DirectListener {
|
|||
*
|
||||
* @param message 接受消息
|
||||
*/
|
||||
@RabbitListener(bindings = @QueueBinding(
|
||||
value = @Queue(name = "direct.queue2", durable = "true", autoDelete = "false"),
|
||||
exchange = @Exchange(name = "bunny.direct", type = ExchangeTypes.DIRECT, durable = "true", autoDelete = "false"),
|
||||
key = {"red", "yellow"}
|
||||
))
|
||||
public void listenDirectQueue2(String message) {
|
||||
System.out.println("消费者2接收到 Direct key 为 {\"red\", \"yellow\"} 消息:【" + message + "】");
|
||||
@RabbitListener(
|
||||
bindings = [QueueBinding(
|
||||
value = Queue(
|
||||
name = "direct.queue2",
|
||||
durable = "true",
|
||||
autoDelete = "false"
|
||||
),
|
||||
exchange = Exchange(
|
||||
name = "bunny.direct",
|
||||
type = ExchangeTypes.DIRECT,
|
||||
durable = "true",
|
||||
autoDelete = "false"
|
||||
),
|
||||
key = ["red", "yellow"]
|
||||
)]
|
||||
)
|
||||
fun listenDirectQueue2(message: String) {
|
||||
println("消费者2接收到 Direct key 为 {\"red\", \"yellow\"} 消息:【$message】")
|
||||
}
|
||||
}
|
|
@ -1,9 +1,8 @@
|
|||
package cn.bunny.module.rabbitMQ.listener;
|
||||
package cn.bunny.module.rabbitMQ.listener
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
import lombok.extern.slf4j.Slf4j
|
||||
import org.springframework.stereotype.Component
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class ErrorListener {
|
||||
}
|
||||
class ErrorListener
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
package cn.bunny.module.rabbitMQ.listener;
|
||||
package cn.bunny.module.rabbitMQ.listener
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
import lombok.extern.slf4j.Slf4j
|
||||
import org.springframework.stereotype.Component
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class FanoutListener {
|
||||
}
|
||||
class FanoutListener
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
package cn.bunny.module.rabbitMQ.listener;
|
||||
package cn.bunny.module.rabbitMQ.listener
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
import lombok.extern.slf4j.Slf4j
|
||||
import org.springframework.stereotype.Component
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class LazyListener {
|
||||
}
|
||||
class LazyListener
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
package cn.bunny.module.rabbitMQ.listener;
|
||||
package cn.bunny.module.rabbitMQ.listener
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
import lombok.extern.slf4j.Slf4j
|
||||
import org.springframework.stereotype.Component
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class TopicListener {
|
||||
}
|
||||
class TopicListener
|
||||
|
|
5
pom.xml
5
pom.xml
|
@ -167,6 +167,11 @@
|
|||
<version>${kotlin.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-stdlib-jdk8</artifactId>
|
||||
<version>${kotlin.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -82,11 +82,21 @@
|
|||
<artifactId>dynamic-datasource-spring-boot3-starter</artifactId>
|
||||
<version>4.3.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-stdlib-jdk8</artifactId>
|
||||
<version>${kotlin.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-test</artifactId>
|
||||
<version>${kotlin.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
|
||||
<testSourceDirectory>src/test/kotlin</testSourceDirectory>
|
||||
<sourceDirectory>src/main/kotlin</sourceDirectory>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
@ -106,16 +116,10 @@
|
|||
<executions>
|
||||
<execution>
|
||||
<id>compile</id>
|
||||
<phase>process-sources</phase>
|
||||
<phase>compile</phase>
|
||||
<goals>
|
||||
<goal>compile</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<sourceDirs>
|
||||
<source>src/main/kotlin</source>
|
||||
<source>target/generated-sources/annotations</source>
|
||||
</sourceDirs>
|
||||
</configuration>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>test-compile</id>
|
||||
|
@ -132,7 +136,7 @@
|
|||
<compilerPlugins>
|
||||
<plugin>spring</plugin>
|
||||
</compilerPlugins>
|
||||
<jvmTarget>17</jvmTarget>
|
||||
<jvmTarget>1.8</jvmTarget>
|
||||
</configuration>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
|
@ -142,6 +146,34 @@
|
|||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>default-compile</id>
|
||||
<phase>none</phase>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>default-testCompile</id>
|
||||
<phase>none</phase>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>compile</id>
|
||||
<phase>compile</phase>
|
||||
<goals>
|
||||
<goal>compile</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>testCompile</id>
|
||||
<phase>test-compile</phase>
|
||||
<goals>
|
||||
<goal>testCompile</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
|
Loading…
Reference in New Issue