Rabbitmq入门案例

Rabbitmq入门案例1创建一个maven项目配置资源pom.xml文件<projectxmlns=”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…

大家好,又见面了,我是你们的朋友全栈君。

Rabbitmq入门案例

学习视频参考以下网址:https://www.bilibili.com/video/av49799767?p=7

1 创建一个maven项目

配置资源pom.xml文件

<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">
	<modelVersion>4.0.0</modelVersion>

	<groupId>cn.yunhe</groupId>
	<artifactId>springcloud-mq</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>springcloud-mq</name>
	<description>Demo project for Spring Boot</description>



	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.1.RELEASE</version>
		<relativePath />
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
		<maven-jar-plugin.version>3.1.1</maven-jar-plugin.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-test</artifactId>
			<scope>test</scope>
		</dependency>
		
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-amqp</artifactId>
		
		</dependency>
		
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>


	</build>


</project>

1.1在资源管理目录下里创建一个application.properties文件

在这里插入图片描述

spring.application.name=springcloud-mq

spring.rabbitmq.host=192.168.40.96
spring.rabbitmq.port=5672
spring.rabbitmq.username=rabbitmq
spring.rabbitmq.password=123456

1.2 在主目录下创建SpringbootServerApplication类

@SpringBootApplication
public class SpingbootServerApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpingbootServerApplication.class, args);
	}
	
}

1.3 创建消息队列QueueConfig

/**
 *  创建消息队列
 * 
 * @author 85762
 *
 */


@Configuration//初始化
public class QueueConfig {

	@Bean
	public Queue createQueue() {
		
		return new Queue("hello-queue");
		
	}
	
	
	
}

1.4 创建消息发送者Sender

/**
 * 消息发送者
 * @author 85762
 *
 */


@Component
public class Sender {
	
	@Autowired
	private AmqpTemplate rabbAmqpTemplate;
	
	

	/*
	 * 发送消息的方法
	 * 
	 */
	public void send(String msg) {
		//向消息队列发送消息
		//参数一:队列的名称
		//参数二:消息
		this.rabbAmqpTemplate.convertAndSend("hello-queue",msg);
		
		
	}
	
}

创建消息接收者Receiver

/**
 * 
 * 消息接收者
 * @author 85762
 *
 */
@Component
public class Receiver {

	/*
	 * 接受消息的方法,采用消息队列监听机制
	 */
	@RabbitListener(queues="hello-queue")
	public void process(String msg) {
		
		System.out.println("receiver:"+msg);
		
	}
	
	
}

创建消息队列测试类QueueTest


/**
 * 消息队列测试类
 * @author 85762
 *
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes=SpingbootServerApplication.class)
public class QueueTest {

	@Autowired
	private Sender sender;
	
	@Test
	public void test1() throws InterruptedException {
		while(true) {
			
			Thread.sleep(1000);
			this.sender.send("Hello RabbitMQ");
		}
	
	}
	
	
	
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/126340.html原文链接:https://javaforall.net

(0)
上一篇 2022年4月7日 下午7:20
下一篇 2022年4月7日 下午7:20


相关推荐

  • session.setAttribute(“key”,value);

    session.setAttribute(“key”,value);1、session.setAttribute(“key”,value);是session设置值的方法,原理同java中的HashMap的键值对,意思也就是key现在为“user”;存放的值为userName,userName应该为一个String类型的变量吧?看你自己的定义。2、可以使用session.getAttribute(“key”);来取值,以为着你能得到userName的值。3、注意…

    2022年10月16日
    8
  • 反转链表(leetcode 206)

    反转链表(leetcode 206)1 题目单向链表反转是一道经典的求职面试笔试或机试题 给定如下如下链表的节点定义 structLinkNo intvalue LinkNode next 比如有一个链表是这样的 1 gt 2 gt 3 gt 4 gt 5 反转后成为 5 gt 4 gt 3 gt 2 gt 1 请实现函数 LinkNode Reverse LinkNode h

    2026年3月20日
    2
  • python读取txt文件并取其某一列数据「建议收藏」

    菜鸟笔记1首先读取的txt文件如下:AAAAF1100003E8180003E1FC0003E7700003FFFC90AAAAF1100003E8240003E2080003E76C0003FFFCA5AAAAF1100003E8140003E2040003E7600003FFFC85AAAAF1100003E7F00003E2080003E…

    2022年4月6日
    1.2K
  • Java 实现一个单例模式_Java实现单例模式的两种方式

    Java 实现一个单例模式_Java实现单例模式的两种方式单例模式在实际开发中有很多的用途,比如我们在项目中常用的工具类,数据库等资源的连接类。这样做的好处是避免创建多个对象,占用内存资源,自始自终在内存中只有一个对象为我们服务。单例对象一般有两种实现方式。懒汉式和饿汉式。饿汉式实现如下:packagecom.day05;/***饿汉式**@authorAdministrator**/publicclassSingle{//定义一个个私有静态本类对…

    2022年7月8日
    23
  • 《Getting Started with WebRTC》第一章 WebRTC介绍

    《Getting Started with WebRTC》第一章 WebRTC介绍

    2022年1月22日
    478
  • Win10安装Matlab R2017a技术指导

    Win10安装Matlab R2017a技术指导下面带来win1064-bitMatlab2017a的安装过程,亲测,可用。(一)所需文件及下载地址1.Matlab-R2017a-ISO镜像文件链接:https://pan.baidu.com/s/1pNhyKGF密码:cgt22.破解文件链接:https://pan.baidu.com/s/1nwWDgKh密码:4fwa链接:https://pan.ba

    2022年5月6日
    53

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号