spring全注解方式使用RabbitMQ

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * @author Wang pengfei
 * @date 2019/9/2 15:10
 * @ClassName: hi-rabbit
 * @Description:
 */
@Component
public class RabbitMQConfig {
    @Bean
    public ConnectionFactory connectionFactory(){
        CachingConnectionFactory connectionFactory =new CachingConnectionFactory("localhost",5672);
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");
        connectionFactory.setVirtualHost("/");
        connectionFactory.setPublisherConfirms(true);
        return connectionFactory;
    }

    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        return rabbitTemplate;
    }
    @Bean
    public Queue myQueue() {
        return new Queue(MQConstants.QUEUE_NAME);
    }


    /**
     * SimpleMessageListenerContainer这个bean是实现消息监听
     * @return
     */
    @Bean
    public SimpleMessageListenerContainer container() {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(connectionFactory());
        container.setQueueNames(new String[]{MQConstants.QUEUE_NAME});
        container.setChannelTransacted(false);
        container.setConcurrentConsumers(10);
        container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        container.setMessageListener(new MessageListenerAdapter(new ChannelAwareMessageListener(){
            @Override
            public void onMessage(Message message, Channel channel) throws Exception {
                long deliveryTag = message.getMessageProperties().getDeliveryTag();
                String content = new String(message.getBody());
                String routingKey = message.getMessageProperties().getReceivedRoutingKey();
                System.out.println(content);
                System.out.println(routingKey);
                //根据对不同的routingKey进行你的业务处理实现异步
                channel.basicAck(deliveryTag, false);
            }
        }));
        return container;
    }

    /**
     *RabbitListenerContainerFactory这个Bean
     * 配合MyMessageListener类以及Application类开启@EnableRabbit
     * 可以实现SimpleMessageListenerContainer bean一样的消息监听
     */

//    @Bean
//    public RabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory){
//        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
//        factory.setConnectionFactory(connectionFactory);
//        return factory;
//    }


    @Bean
    public DirectExchange exchange() {
        return new DirectExchange(MQConstants.EXCHANGE_NAME);
    }

    @Bean
    public Binding binding() {
        return BindingBuilder.bind(this.myQueue()).to(this.exchange()).with(MQConstants.ROUTING_KEY);
    }
}
public class MQConstants {
    public final static  String EXCHANGE_NAME = "TEST_EX";
    public final static String QUEUE_NAME = "QUEUE_TEST";
    public final static String ROUTING_KEY ="ROUTING_KEY";
}
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import java.io.IOException;

/**
 * @author Wang pengfei
 * @date 2019/9/2 15:17
 * @ClassName: hi-rabbit
 * @Description:
 */
@Component
public class MyMessageListener  {

    @RabbitListener(queues = MQConstants.QUEUE_NAME)
    public void processss(Message message, Channel channel) throws IOException {
        String routingKey = message.getMessageProperties().getReceivedRoutingKey();
        String content = new String(message.getBody());
        System.out.println(routingKey);
        System.out.println("******************");
        System.out.println(content);
        //根据对不同的routingKey进行你的业务处理实现异步
        channel.basicAck( message.getMessageProperties().getDeliveryTag(), false);
    }
}
import com.spring.service.Produce;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;

/**
 * @author Wang pengfei
 * @date 2019/9/2 15:09
 * @ClassName: hi-rabbit
 * @Description:
 */
//@EnableRabbit   //使用 RabbitListenerContainerFactory时开启
@ComponentScan("com.spring")
public class Application {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context =new AnnotationConfigApplicationContext(Application.class);
        Produce prod = context.getBean(Produce.class);
        prod.send("hi rabbit mq");
    }
}

项目地址:https://github.com/wpf008/hi-rabbit

你可能感兴趣的:(消息中间件,RabbitMQ)