rabbitmq 初试

This commit is contained in:
Dftre 2024-10-06 19:57:13 +08:00
parent e3b3edaacf
commit 2c412b32a7
3 changed files with 37 additions and 7 deletions

View File

@ -27,7 +27,6 @@ spring:
name: ruoyi
#配置rabbitMq 服务器
rabbitmq:
enable: false
host: 127.0.0.1
port: 5672
username: guest

View File

@ -4,12 +4,17 @@ import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListenerConfigurer;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistrar;
import org.springframework.amqp.support.converter.DefaultClassMapper;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.converter.MappingJackson2MessageConverter;
import org.springframework.messaging.handler.annotation.support.DefaultMessageHandlerMethodFactory;
/**
* @Author : JCccc
@ -17,8 +22,7 @@ import org.springframework.context.annotation.Configuration;
* @Description :
**/
@Configuration
@ConditionalOnProperty(prefix = "spring.rabbitmq", name = { "enable" }, havingValue = "true", matchIfMissing = false)
public class DirectRabbitConfig {
public class DirectRabbitConfig implements RabbitListenerConfigurer{
// 队列 起名TestDirectQueue
@Bean
@ -54,4 +58,23 @@ public class DirectRabbitConfig {
jackson2JsonMessageConverter.setClassMapper(defaultClassMapper);
return jackson2JsonMessageConverter;
}
//以下配置RabbitMQ消息服务
@Autowired
public ConnectionFactory connectionFactory;
@Bean
public DefaultMessageHandlerMethodFactory myHandlerMethodFactory() {
DefaultMessageHandlerMethodFactory factory = new DefaultMessageHandlerMethodFactory();
// 设置转换器
factory.setMessageConverter(new MappingJackson2MessageConverter());
return factory;
}
@Override
public void configureRabbitListeners(RabbitListenerEndpointRegistrar rabbitListenerEndpointRegistrar) {
rabbitListenerEndpointRegistrar.setMessageHandlerMethodFactory(myHandlerMethodFactory());
}
}

View File

@ -4,17 +4,25 @@ import java.util.Map;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
@Component
@ConditionalOnProperty(prefix = "spring.rabbitmq", name = { "enable" }, havingValue = "true", matchIfMissing = false)
@RabbitListener(queues = "TestDirectQueue") // 监听的队列名称 TestDirectQueue
public class DirectReceiver {
@RabbitHandler
public void process(Map map) {
System.out.println("DirectReceiver消费者收到消息 : " + map.toString());
System.out.println("DirectReceiver m消费者收到消息 : " + map.toString());
}
@RabbitHandler
public void process(String map) {
System.out.println("DirectReceiver s消费者收到消息 : " + map.toString());
}
@RabbitHandler
public void process(Byte[] b) {
System.out.println("DirectReceiver s消费者收到消息 : " + b.toString());
}
}