首页 > 应用开发 > Java > Java中String类的初始化?
一、简介
在Spring项目中,可以使用Spring-Rabbit去操作RabbitMQ
一般在开发过程中:
生产者工程:
application.yml 文件配置 RabbitMQ 相关信息;
在生产者工程中编写配置类,用于创建交换机和队列,并进行绑定
注入 RabbitTemplate 对象,通过 RabbitTemplate 对象发送消息到交换机消费者工程:
application.yml 文件配置 RabbitMQ 相关信息
创建消息处理类,用于接收队列中的消息并进行处理二、搭建生产者工程
①创建工程
创建生产者工程 springboot-rabbitmq-producer
②添加依赖
修改 pom.xml 文件内容为如下:
xsi:schemaLocation="
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.4.RELEASE
com.itheima
springboot-rabbitmq-producer
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-amqp
③启动类
package com.itheima.rabbitmq; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ProducerApplication {
public static void main(String[] args) {
SpringApplication.run(ProducerApplication.class);
}
}
④配置RabbitMQ
配置文件
创建 application.yml ,内容如下:
spring:
rabbitmq:
host: localhost
port: 5672
virtual-host: /itcast
username: caiyo
password: caiyo
绑定交换机和队列
创建 RabbitMQ 队列与交换机绑定的配置类 com.itheima.rabbitmq.config.RabbitMQConfig
package com.itheima.rabbitmq.config; import org.springframework.amqp.core.*; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class RabbitMQConfig {
//交换机名称
public static final String ITEM_TOPIC_EXCHANGE = "item_topic_exchange";
//队列名称
public static final String ITEM_QUEUE = "item_queue";
//声明交换机
@Bean("itemTopicExchange")
public Exchange topicExchange(){
return ExchangeBuilder.topicExchange(ITEM_TOPIC_EXCHANGE).durable(true).build();
}
//声明队列
@Bean("itemQueue")
public Queue itemQueue(){
return QueueBuilder.durable(ITEM_QUEUE).build();
}
//绑定队列和交换机
@Bean
public Binding itemQueueExchange(@Qualifier(
"itemQueue") Queue queue, @Qualifier("itemTopicExchange") Exchange exchange){
return BindingBuilder.bind(queue).to(exchange).with("item.#").noargs();
}
}
⑤消息发送Controller
我们创建一个SpringMVC的Controller方便我们进行测试
package com.itheima.rabbitmq.controller; import com.itheima.rabbitmq.config.RabbitMQConfig; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; /**
* 发送消息的测试类
*/ @RestController public class SendMsgController {
//注入RabbitMQ的模板
@Autowired
private RabbitTemplate rabbitTemplat e; 沈阳治疗尖锐湿疣医院 http://www.syxbsd.com/
/**
* 测试
*/
@GetMapping("/sendmsg")
public String sendMsg(@RequestParam String msg, @RequestParam String key){
/**
* 发送消息
* 参数一:交换机名称
* 参数二:路由key
* 参数三:发送的消息
*/
rabbitTemplate.convertAndSend(RabbitMQConfig.ITEM_TOPIC_EXCHANGE ,key ,msg);
//返回消息 return "发送消息成功!";
}
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69979119/viewspace-2768659/ ,如需转载,请注明出处,否则将追究法律责任。