1. 程式人生 > >spring繼承Rabbitmq client

spring繼承Rabbitmq client

eas row tap print rac inf alt exc odi

一:概述

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>

·

spring繼承Rabbitmq client