先导入依赖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">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>springrabbitmq-p</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<spring.version>4.0.3.RELEASE</spring.version>
</properties>
<dependencies>
<!-- RabbitMQ -->
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>1.4.5.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.0.3.RELEASE</version>
<scope>test</scope>
</dependency>
<!--===============================spring 核心start===========================-->
<!--spring-core spring-beans 控制反转和依赖注入功能-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<!--jndi注册处,提供资源绑定加载,事件广播,还有EJB, JMX, 和basic remoting等javaEE特征 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!--提供第三方库集成到spring应用,如缓存 caching (EhCache, Guava, JCache), 邮件mailing (JavaMail), 计划任务scheduling
(CommonJ, Quartz) 和模板引擎template engines (FreeMarker, JasperReports, Velocity) -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
编写配置文件
消费者与提供者的配置之间需要关联
大致流程:
地址文件
application.properties:
rabbit.hosts=192.168.63.130
rabbit.username=peng
rabbit.password=peng
rabbit.port=5672
rabbit.virtualHost=/peng #此处是虚拟机
消费者配置文件amqp-rabbitmq-consumer.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="application.properties"/>
<!-- 连接配置 -->
<rabbit:connection-factory id="connectionFactory" host="${rabbit.hosts}" username="${rabbit.username}" password="${rabbit.password}" port="${rabbit.port}" virtual-host="${rabbit.virtualHost}"/>
<bean id="springQueueListener" class="listener.MsgListener"/>
<rabbit:listener-container connection-factory="connectionFactory" auto-declare="true">
<rabbit:listener ref="springQueueListener" queue-names="order4"/>
</rabbit:listener-container>
</beans>
消费者监听类
package listener;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
/**
* @ClassName: MsgListener
* @author: 鹏
* @date: 2023/7/6 13:16
*/
public class MsgListener implements MessageListener {
@Override
public void onMessage(Message message) {
//打印消息
System.out.println(new String(message.getBody()));
}
}
消费者测试类(作用就是加载配置文件初始化监听器)
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @ClassName: C_test
* @author: 鹏
* @date: 2023/7/6 13:23
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:amqp-rabbitmq-consumer.xml")
public class C_test {
@Test
public void test(){
System.out.println(13);
}
}
提供者配置文件amqp-rabbitmq.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="application.properties"/>
<!-- 连接配置 -->
<rabbit:connection-factory id="connectionFactory" host="${rabbit.hosts}" username="${rabbit.username}" password="${rabbit.password}" port="${rabbit.port}" virtual-host="${rabbit.virtualHost}"/>
<!--定义交换机、队列-->
<rabbit:admin connection-factory="connectionFactory"/>
<!--
durable:是否持久化
exclusive: 仅创建者可以使用的私有队列,断开后自动删除
auto_delete: 当所有消费客户端连接断开后,是否自动删除队列
-->
<!-- 申明消息队列Queue -->
<rabbit:queue id="peng_queue1" name="order1" durable="true" auto-delete="false" exclusive="false" />
<rabbit:queue id="peng_queue2" name="order2" durable="true" auto-delete="false" exclusive="false" />
<rabbit:queue id="peng_queue3" name="order3" durable="true" auto-delete="false" exclusive="false" />
<!--创建fanout交换机并绑定队列-->
<rabbit:fanout-exchange name="spring_fanout_exchange" id="spring_fanout_exchange" auto-delete="true">
<rabbit:bindings>
<rabbit:binding queue="peng_queue1"/>
<rabbit:binding queue="peng_queue2"/>
</rabbit:bindings>
</rabbit:fanout-exchange>
<rabbit:queue id="peng_queue4" name="order4" durable="true" auto-delete="false" exclusive="false" />
<rabbit:queue id="peng_queue5" name="order5" durable="true" auto-delete="false" exclusive="false" />
<rabbit:queue id="peng_queue6" name="order6" durable="true" auto-delete="false" exclusive="false" />
<!--创建topics交换机并绑定队列-->
<rabbit:topic-exchange name="spring_topic_exchange" id="spring_topic_exchange" auto-delete="true">
<rabbit:bindings>
<rabbit:binding queue="peng_queue4" pattern="*.*"/>
<rabbit:binding queue="peng_queue5" pattern="*.one"/>
<rabbit:binding queue="peng_queue6" pattern="two.#"/>
</rabbit:bindings>
</rabbit:topic-exchange>
<!--定义rabbitTemplate对象可以在代码中方便发送消息-->
<rabbit:template id="rabbitTemplate" connection-factory="connectionFactory"/>
</beans>
提供者测试类
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @ClassName: P_test
* @author: 鹏
* @date: 2023/7/6 10:46
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:amqp-rabbitmq.xml")
public class P_test {
@Autowired
private RabbitTemplate rabbitTemplate;
/*
helloword方式
*/
@Test
public void addQueue(){
rabbitTemplate.convertAndSend("order5","第一个spring队列");
}
/*
fanout方式
*/
@Test
public void addFanoutQueue(){
rabbitTemplate.convertAndSend("spring_fanout_exchange","","第一个springFanout消息");
}
/*
fanout方式
*/
@Test
public void addTopicQueue(){
rabbitTemplate.convertAndSend("spring_topic_exchange","6666.one","第一个springTopic消息");
rabbitTemplate.convertAndSend("spring_topic_exchange","two.666","第一个springTopic消息");
}
}