rabbitmq 集成spring boot

引入包

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>

配置文件
application.properties

spring.rabbitmq.port=5672
spring.rabbitmq.virtualHost=/
spring.rabbitmq.host=192.168.74.164
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

配置类


import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;


@Configuration
public class RabbitMqConfig {

    public static final String GET_TICKET = "test";

    @Bean
    public Queue getTicket() {
        return new Queue(GET_TICKET, true); // 队列持久
    }

    @Bean
    public TopicExchange topicExchange(){
        return new TopicExchange("ticket_default_exchange");
    }

    @Bean
    Binding bindingExchangeMessage(Queue getTicket, TopicExchange topicExchange) {
        return BindingBuilder.bind(getTicket).to(topicExchange).with("#");
    }

    /**
     * Rabbit监听容器工厂
     * @param configurer
     * @param connectionFactory
     * @return
     */
    @Bean("ticketRabbitmqContainerFactory")
    public SimpleRabbitListenerContainerFactory rabbitmqContainerFactory(
            SimpleRabbitListenerContainerFactoryConfigurer configurer, ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setPrefetchCount(1);//一次去几个消息
        factory.setConcurrentConsumers(1);//几个消费者
        // 线程的线程池
        ExecutorService executorService = Executors.newFixedThreadPool(1);//消费者线程池(只能大于等于消费者,最好等于)
        factory.setTaskExecutor(executorService);
        configurer.configure(factory, connectionFactory);
        return factory;
    }


}

消费者



import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


import com.hhly.ticket.service.ticket.rabbitmq.RabbitMqConfig;

@Component
public class TicketListenter implements MessageListener {

    @RabbitListener(queues=RabbitMqConfig.GET_TICKET, containerFactory="rabbitmqContainerFactory")
    public void onMessage(Message message) {
        String result  = new String(message.getBody(), "UTF-8");
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值