Springboot整合RabbitMq简单使用

Springboot整合RabbitMq简单使用,第1张

Springboot整合RabbitMq简单使用
@Autowired
    RabbitTemplate rabbitTemplate;
    @Autowired
    AmqpAdmin amqpAdmin;

    @Test
    void contextLoads() {

        rabbitTemplate.convertAndSend("amq.direct", "amq.direct", "消息内容");
    }

    @Test
    void contextLoads1() {
        rabbitTemplate.receiveAndConvert("amq.direct");
    }

    @Test
    void creatExchange() {
        amqpAdmin.declareExchange(new DirectExchange("交换机名称"));
        System.out.println("创建完成");
        amqpAdmin.declareQueue(new Queue("队列名称",true));
        System.out.println("创建完成");
        amqpAdmin.declareBinding(new Binding("目的地", Binding.DestinationType.QUEUE,"交换机名称","路由键",null));
    }

监听队列

@SpringBootApplication
@EnableRabbit
public class AmqpApplication {

    public static void main(String[] args) {
        SpringApplication.run(AmqpApplication.class, args);
    }

}
@Service
public class BookService {

    @RabbitListener(queues = "队列名称")
    void Selectbook(Book book) {
        System.out.println("book = " + book);
    }

    @RabbitListener(queues = "队列名称")
    void SelectMessage(Message message) {
        System.out.println("message.getBody() = " + message.getBody());
        System.out.println("message.getMessageProperties() = " + message.getMessageProperties());
    }

}

序列化:

@Configuration
public class amqpConfig {

    @Bean
    public MessageConverter messageConverter() {
       return new Jackson2JsonMessageConverter();
    }

}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5706075.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存