RabbitMQ 第三篇

这一篇的主要是rabbitmq和springboot的整合

导入的依赖:

<!--加入rabbitmq队列-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
配置:

spring.application.name=springboot-rabbitmq
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.publisher-confirms=true
spring.rabbitmq.virtual-host=/
direct模式:

@Configuration
public class SenderConf {
    //声明一个队列  direct模式队列 队列名完全匹配 所以不需要设置exchange之类的
    @Bean
    public Queue direct() {
        return new Queue("queue", true);// true表示持久化该队列
    }
}
生产者:

package com.yjp.rabbitmq;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 发送消息
 *
 * @Author : WenBao
 * Date : 17:24 2017/12/12
 */
@Component
public class HelloSender {
    //直接注入模板 springboot帮我们集成的模板
    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void send() {
        System.out.print("发送消息");
        rabbitTemplate.convertAndSend("queue", "hello,rabbit");
    }
}
消费者:

package com.yjp.rabbitmq;

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class HelloReceive {
    @RabbitListener(queues = "queue")
    public void process(String str) {
        System.out.println("Receive:" + str);
    }
}

测试类:

package springboot;


import com.yjp.rabbitmq.HelloSender;
import com.yjp.LoginApplication;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.jms.Destination;

@RunWith(SpringRunner.class)
/**
 * webEnvironment需要MVC的支持
 * SpringBootTest.WebEnvironment.RANDOM_PORT 启动Servlet环境 端口号随机
 */
@SpringBootTest(classes = LoginApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class LoginApplicationTests {
    @Autowired
    private HelloSender helloSender;

    @Test
    public void contextLoads() {
        helloSender.send();
    }


}
topic模式:

配置bean:

package com.yjp.rabbitmq;

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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 创建队列 队列名为queue
 *
 * @Author : WenBao
 * Date : 17:23 2017/12/12
 */
@Configuration
public class SenderConf {
    //声明一个队列  direct模式队列
    @Bean
    public Queue direct() {
        return new Queue("queue", true);// true表示持久化该队列
    }


    //topic模式队列
    @Bean
    public Queue topic1() {
        return new Queue("topic-1");
    }

    //topic模式队列
    @Bean
    public Queue topic2() {
        return new Queue("topic-2");
    }
//
//    //fanout模式队列
//    @Bean
//    public Queue fanout1() {
//        return new Queue("fanout-1");
//    }
//
//    //fanout模式队列
//    @Bean
//    public Queue fanout2() {
//        return new Queue("fanout-2");
//    }


    //声明交换机
    @Bean
    TopicExchange topicExchange() {
        return new TopicExchange("topicExchange");
    }

    //绑定  将队列和exchange绑定
    @Bean
    public Binding bindingTopic1() {
        return BindingBuilder.bind(topic1()).to(topicExchange()).with("123.#");
    }

    @Bean
    public Binding bindingTopic2() {
        return BindingBuilder.bind(topic2()).to(topicExchange()).with("*.123");
    }
//
//    @Bean
//    public Binding binding2() {
//        return BindingBuilder.bind(queue2()).to(topicExchange()).with("key.#");
//    }


}

生产者:

package com.yjp.rabbitmq;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class TopicSender {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void send() {
        System.err.println("发送消息");
        //需要写上exchange的名字   routingkey  发送的消息
        rabbitTemplate.convertAndSend("topicExchange", "123.123", "hello ni hao");
    }
}
消费者 1:

package com.yjp.rabbitmq;

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class TopicReceive1 {
    @RabbitListener(queues = "topic-1")
    public void getMessage(String message) {
        System.err.println("Topic1" + message);
    }
}
消费者 2:

package com.yjp.rabbitmq;

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class TopicReceive2 {
    @RabbitListener(queues = "topic-2")
    public void getMessage(String message) {
        System.err.println("Topic2" + message);
    }
}

测试类:

@Autowired
private TopicSender topicSender;

@Test
public void topic() {
    topicSender.send();
}
fanout模式:

三种模式的配置都在

package com.yjp.rabbitmq;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 创建队列 队列名为queue
 *
 * @Author : WenBao
 * Date : 17:23 2017/12/12
 */
@Configuration
public class SenderConf {
    //声明一个队列  direct模式队列
    @Bean
    public Queue direct() {
        return new Queue("queue", true);// true表示持久化该队列
    }


    //topic模式队列
    @Bean
    public Queue topic1() {
        return new Queue("topic-1");
    }

    //topic模式队列
    @Bean
    public Queue topic2() {
        return new Queue("topic-2");
    }

    //fanout模式队列
    @Bean
    public Queue fanout1() {
        return new Queue("fanout-1");
    }

    //fanout模式队列
    @Bean
    public Queue fanout2() {
        return new Queue("fanout-2");
    }


    //声明交换机
    @Bean
    TopicExchange topicExchange() {
        return new TopicExchange("topicExchange");
    }

    //绑定  将队列和exchange绑定
    @Bean
    public Binding bindingTopic1() {
        return BindingBuilder.bind(topic1()).to(topicExchange()).with("123.#");
    }

    @Bean
    public Binding bindingTopic2() {
        return BindingBuilder.bind(topic2()).to(topicExchange()).with("*.123");
    }

    @Bean
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange("fanoutExchange");
    }

    @Bean
    public Binding bindingfanout1() {
        return BindingBuilder.bind(fanout1()).to(fanoutExchange());
    }

    @Bean
    public Binding bindingfanout2() {
        return BindingBuilder.bind(fanout2()).to(fanoutExchange());
    }


}
生产者:

package com.yjp.rabbitmq;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class FanoutSender {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void sender() {
        System.err.println("消息发送");
        //如果不写"" 那么fanoutExchange这个exchange会默认成为routingkey
        rabbitTemplate.convertAndSend("fanoutExchange", "", "你好");
    }
}
消费者 1:

package com.yjp.rabbitmq;

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class FanoutReceive1 {
    @RabbitListener(queues = "fanout-1")
    public void get(String message) {
        System.err.println(message);
    }
}
消费者 2:

package com.yjp.rabbitmq;

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class FanoutReceive2 {
    @RabbitListener(queues = "fanout-2")
    public void get(String message) {
        System.err.println(message);
    }
}

测试  所有的测试方法:

package springboot;


import com.yjp.LoginApplication;
import com.yjp.rabbitmq.FanoutSender;
import com.yjp.rabbitmq.HelloSender;
import com.yjp.rabbitmq.TopicSender;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
/**
 * webEnvironment需要MVC的支持
 * SpringBootTest.WebEnvironment.RANDOM_PORT 启动Servlet环境 端口号随机
 */
@SpringBootTest(classes = LoginApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class LoginApplicationTests {
    @Autowired
    private HelloSender helloSender;

    @Autowired
    private TopicSender topicSender;

    @Autowired
    private FanoutSender fanoutSender;

    @Test
    public void contextLoads() {
        helloSender.send();
    }

    @Test
    public void topic() {
        topicSender.send();
    }

    @Test
    public void fanout() {
        fanoutSender.sender();
    }

}

到此springb整合rabbitmq就结束了  下一篇就说一下rabbitmq的三种发送方式,以及和activemq的区别

努力吧 皮卡丘

SimpleMessageListenerContainer 是 RabbitMQ 客户端提供的一个用于消息监听的容器,它可以实现对消息的自动监听、自动连接和重连等功能。SimpleMessageListenerContainer 的使用对于 RabbitMQ 的消息监听非常方便。 下面我们来看一下 SimpleMessageListenerContainer 的使用方法: 首先,我们需要添加 RabbitMQ 的依赖: ```xml <dependency> <groupId>org.springframework.amqp</groupId> <artifactId>spring-rabbit</artifactId> <version>2.2.11.RELEASE</version> </dependency> ``` 然后,我们需要在 Spring 配置文件中配置相关的 Bean: ```xml <!-- 创建一个 ConnectionFactory --> <bean id="connectionFactory" class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory"> <property name="addresses" value="localhost:5672" /> <property name="username" value="guest" /> <property name="password" value="guest" /> <property name="virtualHost" value="/" /> </bean> <!-- 配置 RabbitAdmin --> <bean id="rabbitAdmin" class="org.springframework.amqp.rabbit.core.RabbitAdmin"> <constructor-arg ref="connectionFactory" /> </bean> <!-- 配置 SimpleMessageListenerContainer --> <bean id="simpleMessageListenerContainer" class="org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer"> <property name="connectionFactory" ref="connectionFactory" /> <property name="queueNames" value="test.queue" /> <property name="messageListener" ref="messageListener" /> </bean> <!-- 配置 MessageListener --> <bean id="messageListener" class="com.example.MessageListener" /> ``` 其中,ConnectionFactory 为连接 RabbitMQ 的工厂类,RabbitAdmin 为 RabbitMQ 的管理器,SimpleMessageListenerContainer 为消息监听容器,queueNames 表示需要监听的队列名称,messageListener 表示消息的监听器类。 最后,我们需要编写一个消息监听器类 MessageListener: ```java public class MessageListener implements ChannelAwareMessageListener { @Override public void onMessage(Message message, Channel channel) throws Exception { System.out.println("接收到消息:" + new String(message.getBody())); channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); } } ``` 在这个类中,我们实现了 ChannelAwareMessageListener 接口,它是 Spring AMQP 提供的一个用于消息监听的接口,其中 onMessage 方法为消息监听回调方法。 至此,我们就可以使用 SimpleMessageListenerContainer 来实现 RabbitMQ 的消息监听了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值