♻️ feat(新增): module转成kotlin

This commit is contained in:
Bunny 2024-09-13 19:55:37 +08:00
parent 5fc8e1b6fe
commit ddbda4bbef
17 changed files with 181 additions and 132 deletions

View File

@ -11,6 +11,7 @@ import org.springframework.web.bind.annotation.RestControllerAdvice
import java.io.FileNotFoundException import java.io.FileNotFoundException
import java.nio.file.AccessDeniedException import java.nio.file.AccessDeniedException
import java.sql.SQLIntegrityConstraintViolationException import java.sql.SQLIntegrityConstraintViolationException
import java.util.regex.Pattern
@RestControllerAdvice @RestControllerAdvice
@ -31,9 +32,7 @@ class GlobalExceptionHandler {
// 运行时异常信息 // 运行时异常信息
@ExceptionHandler(RuntimeException::class) @ExceptionHandler(RuntimeException::class)
@ResponseBody @ResponseBody
@Throws( @Throws(FileNotFoundException::class)
FileNotFoundException::class
)
fun exceptionHandler(exception: RuntimeException): Result<Any?> { fun exceptionHandler(exception: RuntimeException): Result<Any?> {
logger.error("GlobalExceptionHandler===>运行时异常信息:{}", exception.message) logger.error("GlobalExceptionHandler===>运行时异常信息:{}", exception.message)
exception.printStackTrace() exception.printStackTrace()
@ -46,7 +45,16 @@ class GlobalExceptionHandler {
fun error(exception: Exception): Result<Any?> { fun error(exception: Exception): Result<Any?> {
logger.error("GlobalExceptionHandler===>系统异常信息:{}", exception.message) 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, "系统异常")
}
} }
// 特定异常处理 // 特定异常处理

View File

@ -1,26 +1,26 @@
package cn.bunny.module.minio.properties; package cn.bunny.module.minio.properties
import io.minio.MinioClient; import io.minio.MinioClient
import lombok.Data; import lombok.Data
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration
@Configuration @Configuration
@ConfigurationProperties(prefix = "bunny.minio") @ConfigurationProperties(prefix = "bunny.minio")
@ConditionalOnProperty(name = "bunny.minio.bucket-name")// 当属性有值时这个配置才生效 @ConditionalOnProperty(name = ["bunny.minio.bucket-name"]) // 当属性有值时这个配置才生效
@Data @Data
@Slf4j @Slf4j
public class MinioProperties { open class MinioProperties {
private String endpointUrl; val endpointUrl: String? = null
private String accessKey; val accessKey: String? = null
private String secretKey; val secretKey: String? = null
private String bucketName; val bucketName: String? = null
@Bean @Bean
public MinioClient minioClient() { open fun minioClient(): MinioClient {
return MinioClient.builder().endpoint(endpointUrl).credentials(accessKey, secretKey).build(); return MinioClient.builder().endpoint(endpointUrl).credentials(accessKey, secretKey).build()
} }
} }

View File

@ -1,38 +1,40 @@
package cn.bunny.module.rabbitMQ.config; package cn.bunny.module.rabbitMQ.config
import jakarta.annotation.PostConstruct; import jakarta.annotation.PostConstruct
import lombok.extern.slf4j.Slf4j; import org.apache.logging.log4j.LogManager
import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.amqp.core.ReturnedMessage
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter; import org.springframework.amqp.rabbit.core.RabbitTemplate
import org.springframework.amqp.support.converter.MessageConverter; import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.amqp.support.converter.MessageConverter
import org.springframework.context.annotation.Bean; import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component; import org.springframework.context.annotation.Bean
import org.springframework.stereotype.Component
@Component @Component
@Slf4j class RabbitMqConfig {
public class RabbitMqConfig { private val logger = LogManager.getLogger(RabbitMqConfig::class.java)
@Autowired @Autowired
private RabbitTemplate rabbitTemplate; private val rabbitTemplate: RabbitTemplate? = null
@PostConstruct @PostConstruct
public void init() { fun init() {
rabbitTemplate.setReturnsCallback(returned -> { rabbitTemplate!!.setReturnsCallback { returned: ReturnedMessage ->
log.error("触发return callback,"); logger.error("触发return callback,")
log.debug("exchange: {}", returned.getExchange()); logger.debug("exchange: {}", returned.exchange)
log.debug("routingKey: {}", returned.getRoutingKey()); logger.debug("routingKey: {}", returned.routingKey)
log.debug("message: {}", returned.getMessage()); logger.debug("message: {}", returned.message)
log.debug("replyCode: {}", returned.getReplyCode()); logger.debug("replyCode: {}", returned.replyCode)
log.debug("replyText: {}", returned.getReplyText()); logger.debug("replyText: {}", returned.replyText)
}); }
} }
@Bean @Bean
public MessageConverter messageConverter() { fun messageConverter(): MessageConverter {
// 1.定义消息转换器 // 1.定义消息转换器
Jackson2JsonMessageConverter converter = new Jackson2JsonMessageConverter(); val converter = Jackson2JsonMessageConverter()
// 2.配置自动创建消息id用于识别不同消息也可以在业务中基于ID判断是否是重复消息 // 2.配置自动创建消息id用于识别不同消息也可以在业务中基于ID判断是否是重复消息
converter.setCreateMessageIds(true); converter.setCreateMessageIds(true)
return converter; return converter
} }
} }

View File

@ -1,9 +1,8 @@
package cn.bunny.module.rabbitMQ.consumer; package cn.bunny.module.rabbitMQ.consumer
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component
@Component @Component
@Slf4j @Slf4j
public class DelayConsumer { class DelayConsumer
}

View File

@ -1,24 +1,25 @@
package cn.bunny.module.rabbitMQ.consumer; package cn.bunny.module.rabbitMQ.consumer
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test
import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.amqp.rabbit.core.RabbitTemplate
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component
@Component @Component
@Slf4j @Slf4j
public class DirectConsumer { class DirectConsumer {
@Autowired @Autowired
RabbitTemplate rabbitTemplate; var rabbitTemplate: RabbitTemplate? = null
/** /**
* 发送红色消息 * 发送红色消息
*/ */
@Test @Test
void testSendDirectRed() throws Exception { @Throws(Exception::class)
for (int i = 0; i < 1000; i++) { fun testSendDirectRed() {
rabbitTemplate.convertAndSend("bunny.direct", "red", "发送消息:" + i); for (i in 0..999) {
rabbitTemplate!!.convertAndSend("bunny.direct", "red", "发送消息:$i")
} }
} }
} }

View File

@ -1,9 +1,8 @@
package cn.bunny.module.rabbitMQ.consumer; package cn.bunny.module.rabbitMQ.consumer
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component
@Component @Component
@Slf4j @Slf4j
public class ErrorConsumer { class ErrorConsumer
}

View File

@ -1,9 +1,8 @@
package cn.bunny.module.rabbitMQ.consumer; package cn.bunny.module.rabbitMQ.consumer
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component
@Component @Component
@Slf4j @Slf4j
public class FanoutConsumer { class FanoutConsumer
}

View File

@ -1,9 +1,8 @@
package cn.bunny.module.rabbitMQ.consumer; package cn.bunny.module.rabbitMQ.consumer
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component
@Component @Component
@Slf4j @Slf4j
public class LazyConsumer { class LazyConsumer
}

View File

@ -1,9 +1,8 @@
package cn.bunny.module.rabbitMQ.consumer; package cn.bunny.module.rabbitMQ.consumer
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component
@Component @Component
@Slf4j @Slf4j
public class TopicConsumer { internal class TopicConsumer
}

View File

@ -1,9 +1,8 @@
package cn.bunny.module.rabbitMQ.listener; package cn.bunny.module.rabbitMQ.listener
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component
@Component @Component
@Slf4j @Slf4j
public class DelayListener { class DelayListener
}

View File

@ -1,16 +1,16 @@
package cn.bunny.module.rabbitMQ.listener; package cn.bunny.module.rabbitMQ.listener
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j
import org.springframework.amqp.core.ExchangeTypes; import org.springframework.amqp.core.ExchangeTypes
import org.springframework.amqp.rabbit.annotation.Exchange; import org.springframework.amqp.rabbit.annotation.Exchange
import org.springframework.amqp.rabbit.annotation.Queue; import org.springframework.amqp.rabbit.annotation.Queue
import org.springframework.amqp.rabbit.annotation.QueueBinding; import org.springframework.amqp.rabbit.annotation.QueueBinding
import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.amqp.rabbit.annotation.RabbitListener
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component
@Component @Component
@Slf4j @Slf4j
public class DirectListener { class DirectListener {
/** /**
* * 监听者2 * * 监听者2
* 创建队列 持久化的不自动删除 * 创建队列 持久化的不自动删除
@ -19,12 +19,23 @@ public class DirectListener {
* *
* @param message 接受消息 * @param message 接受消息
*/ */
@RabbitListener(bindings = @QueueBinding( @RabbitListener(
value = @Queue(name = "direct.queue2", durable = "true", autoDelete = "false"), bindings = [QueueBinding(
exchange = @Exchange(name = "bunny.direct", type = ExchangeTypes.DIRECT, durable = "true", autoDelete = "false"), value = Queue(
key = {"red", "yellow"} name = "direct.queue2",
)) durable = "true",
public void listenDirectQueue2(String message) { autoDelete = "false"
System.out.println("消费者2接收到 Direct key 为 {\"red\", \"yellow\"} 消息:【" + message + ""); ),
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")
} }
} }

View File

@ -1,9 +1,8 @@
package cn.bunny.module.rabbitMQ.listener; package cn.bunny.module.rabbitMQ.listener
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component
@Component @Component
@Slf4j @Slf4j
public class ErrorListener { class ErrorListener
}

View File

@ -1,9 +1,8 @@
package cn.bunny.module.rabbitMQ.listener; package cn.bunny.module.rabbitMQ.listener
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component
@Component @Component
@Slf4j @Slf4j
public class FanoutListener { class FanoutListener
}

View File

@ -1,9 +1,8 @@
package cn.bunny.module.rabbitMQ.listener; package cn.bunny.module.rabbitMQ.listener
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component
@Component @Component
@Slf4j @Slf4j
public class LazyListener { class LazyListener
}

View File

@ -1,9 +1,8 @@
package cn.bunny.module.rabbitMQ.listener; package cn.bunny.module.rabbitMQ.listener
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component
@Component @Component
@Slf4j @Slf4j
public class TopicListener { class TopicListener
}

View File

@ -167,6 +167,11 @@
<version>${kotlin.version}</version> <version>${kotlin.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -82,11 +82,21 @@
<artifactId>dynamic-datasource-spring-boot3-starter</artifactId> <artifactId>dynamic-datasource-spring-boot3-starter</artifactId>
<version>4.3.1</version> <version>4.3.1</version>
</dependency> </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> </dependencies>
<build> <build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory> <sourceDirectory>src/main/kotlin</sourceDirectory>
<testSourceDirectory>src/test/kotlin</testSourceDirectory>
<plugins> <plugins>
<plugin> <plugin>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
@ -106,16 +116,10 @@
<executions> <executions>
<execution> <execution>
<id>compile</id> <id>compile</id>
<phase>process-sources</phase> <phase>compile</phase>
<goals> <goals>
<goal>compile</goal> <goal>compile</goal>
</goals> </goals>
<configuration>
<sourceDirs>
<source>src/main/kotlin</source>
<source>target/generated-sources/annotations</source>
</sourceDirs>
</configuration>
</execution> </execution>
<execution> <execution>
<id>test-compile</id> <id>test-compile</id>
@ -132,7 +136,7 @@
<compilerPlugins> <compilerPlugins>
<plugin>spring</plugin> <plugin>spring</plugin>
</compilerPlugins> </compilerPlugins>
<jvmTarget>17</jvmTarget> <jvmTarget>1.8</jvmTarget>
</configuration> </configuration>
<dependencies> <dependencies>
<dependency> <dependency>
@ -142,6 +146,34 @@
</dependency> </dependency>
</dependencies> </dependencies>
</plugin> </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> </plugins>
</build> </build>