spring继承Rabbitmq client-----------------------待研究

一:概述

1.官网

  https://spring.io/

 

2.进入project

  

 

3.找到spring AMQP

  

 

二:程序

1.结构

  

 

2.pom

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <modelVersion>4.0.0</modelVersion>
 6 
 7     <groupId>SpringRabbitmq</groupId>
 8     <artifactId>mq</artifactId>
 9     <version>1.0-SNAPSHOT</version>
10     <dependencies>
11         <!-- https://mvnrepository.com/artifact/com.rabbitmq/amqp-client -->
12         <dependency>
13             <groupId>com.rabbitmq</groupId>
14             <artifactId>amqp-client</artifactId>
15             <version>3.6.2</version>
16         </dependency>
17         <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
18         <dependency>
19             <groupId>org.slf4j</groupId>
20             <artifactId>slf4j-api</artifactId>
21             <version>1.7.10</version>
22         </dependency>
23         <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
24         <dependency>
25             <groupId>org.slf4j</groupId>
26             <artifactId>slf4j-log4j12</artifactId>
27             <version>1.7.5</version>
28             <scope>test</scope>
29         </dependency>
30         <!-- https://mvnrepository.com/artifact/log4j/log4j -->
31         <dependency>
32             <groupId>log4j</groupId>
33             <artifactId>log4j</artifactId>
34             <version>1.2.17</version>
35         </dependency>
36         <!-- https://mvnrepository.com/artifact/junit/junit -->
37         <dependency>
38             <groupId>junit</groupId>
39             <artifactId>junit</artifactId>
40             <version>4.11</version>
41             <scope>test</scope>
42         </dependency>
43         <dependency>
44             <groupId>org.springframework.amqp</groupId>
45             <artifactId>spring-rabbit</artifactId>
46             <version>2.0.2.RELEASE</version>
47         </dependency>
48         <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
49         <dependency>
50             <groupId>org.springframework</groupId>
51             <artifactId>spring-context</artifactId>
52             <version>5.0.3.RELEASE</version>
53         </dependency>
54 
55     </dependencies>
56     <build>
57         <plugins>
58             <plugin>
59                 <groupId>org.apache.maven.plugins</groupId>
60                 <artifactId>maven-compiler-plugin</artifactId>
61                 <configuration>
62                     <source>1.7</source>
63                     <target>1.7</target>
64                 </configuration>
65             </plugin>
66         </plugins>
67     </build>
68 </project>

 

3.Java

 1 package spring;
 2 
 3 import org.springframework.amqp.rabbit.core.RabbitTemplate;
 4 import org.springframework.context.ApplicationContext;
 5 import org.springframework.context.support.AbstractApplicationContext;
 6 import org.springframework.context.support.ClassPathXmlApplicationContext;
 7 import org.springframework.context.support.GenericXmlApplicationContext;
 8 
 9 public class SpringMain {
10     public static void main(String[] args)throws Exception{
11         ApplicationContext context =  new GenericXmlApplicationContext("classpath:rabbit-context.xml");
12         //RabbitMq模板
13         RabbitTemplate template=context.getBean(RabbitTemplate.class);
14         //发送消息
15         template.convertAndSend("spring.MyConsumer");
16         Thread.sleep(2000);
17         
18     }
19 }

 

4.Java

1 package spring;
2 
3 public class MyConsumer {
4     public void listen(String foo) {
5         System.out.println("消费者:"+foo);
6     }
7 }

 

5.rabbit-context.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:rabbit="http://www.springframework.org/schema/rabbit"
 5        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 6                           http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.7.xsd">
 7     <!-- 定义MQ工厂-->
 8     <rabbit:connection-factory id="connectionFactory"
 9                                host="127.0.0.1" port="5672" virtual-host="/cjhost"
10                                username="caojun" password="123456"/>
11     <!-- 定义MQ模板,指定要连接的工厂以及交换机-->
12     <rabbit:template id="amqpTemplate"
13                      connection-factory="connectionFactory"
14                      exchange="myExchange"/>
15     <!-- MQ管理,包括队列,交换机声明等-->
16     <rabbit:admin connection-factory="connectionFactory" />
17     <!--定义队列-->
18     <rabbit:queue name="myQueue" auto-declare="true" durable="true" />
19     <!--定义交换机-->
20     <rabbit:topic-exchange name="myExchange" auto-declare="true">
21         <rabbit:bindings>
22             <rabbit:binding queue="myQueue" pattern="foo.*" />
23         </rabbit:bindings>
24     </rabbit:topic-exchange>
25 
26     <!--队列监听-->
27     <rabbit:listener-container connection-factory="connectionFactory">
28         <rabbit:listener ref="foo" method="listen" queue-names="myQueue" />
29     </rabbit:listener-container>
30 
31     <bean id="foo" class="spring.MyConsumer" />
32 </beans>

 

·

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值