当前位置: 首页> 教育> 就业 > 快手刷作品双击自助网站_软件下载平台_免费自学电商教程_推广赚钱一个50元

快手刷作品双击自助网站_软件下载平台_免费自学电商教程_推广赚钱一个50元

时间:2025/7/10 8:59:44来源:https://blog.csdn.net/m0_47498690/article/details/144045658 浏览次数:0次
快手刷作品双击自助网站_软件下载平台_免费自学电商教程_推广赚钱一个50元

Spring集成RabbitMQ

官网:https://spring.io/projects/spring-amqp

创建聚合项目

在这里插入图片描述

父pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><packaging>pom</packaging><modules><module>provider</module><module>consumer</module></modules><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.0.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>cn.yanghuisen</groupId><artifactId>spring-rabbitmq</artifactId><version>0.0.1-SNAPSHOT</version><name>spring-rabbitmq</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope><exclusions><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.springframework.amqp</groupId><artifactId>spring-rabbit-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

生产者

pom.xml

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>spring-rabbitmq</artifactId><groupId>cn.yanghuisen</groupId><version>0.0.1-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>provider</artifactId><name>provider</name><!-- FIXME change it to the project's website --><url>http://www.example.com</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target></properties><dependencies></dependencies></project>
package cn.yanghuisen;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** Hello world!**/
@SpringBootApplication
public class App
{public static void main( String[] args ){SpringApplication.run(App.class);}
}
package cn.yanghuisen;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;/*** @author admin* @version 1.0* @date 2020/5/29 22:28* @Description TODO*/
@Configuration
public class RabbitMQConfig {/*** 申明队列* @return*/@Beanpublic Queue queue(){return new Queue("topic");}/*** 申明交换机* @return*/@Beanpublic TopicExchange topicExchange(){return new TopicExchange("topicExchange");}/*** 将队列绑定到交换机上* @return*/@Beanpublic Binding binding(){return BindingBuilder.bind(queue()).to(topicExchange()).with("*.msg.#");}}
package cn.yanghuisen;import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;import javax.annotation.Resource;/*** @author admin* @version 1.0* @date 2020/5/29 22:33* @Description TODO*/
@Component
public class Send {@Resourceprivate RabbitTemplate rabbitTemplate;public void send(){String message = "Hello World";/*1、交换机参数2、路由key3、消息内容*/rabbitTemplate.convertAndSend("topicExchange","topic.msg",message);System.out.println("发送消息:"+message);}
}

消费者

pom.xml

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>spring-rabbitmq</artifactId><groupId>cn.yanghuisen</groupId><version>0.0.1-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>consumer</artifactId><name>consumer</name><!-- FIXME change it to the project's website --><url>http://www.example.com</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target></properties><dependencies></dependencies><build></build>
</project>
package cn.yanghuisen;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** Hello world!**/
@SpringBootApplication
public class App 
{public static void main( String[] args ){SpringApplication.run(App.class);}
}
package cn.yanghuisen;import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;/*** @author admin* @version 1.0* @date 2020/5/29 22:36* @Description TODO*/
@Component
@RabbitListener(queues = "topic")   // 监听队列
public class Consumer {// 接收消息的处理方法@RabbitHandlerpublic void recv(String message){System.out.println("接受消息:"+message);}
}

测试

package cn.yanghuisen;import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;import javax.annotation.Resource;/*** @author admin* @version 1.0* @date 2020/5/29 22:54* @Description TODO*/
@SpringBootTest
public class TestSend {@Resourceprivate Send send;@Testpublic void testSend(){send.send();}}
关键字:快手刷作品双击自助网站_软件下载平台_免费自学电商教程_推广赚钱一个50元

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: