在Spring AMQP项目中Spring也提供了对RabbitMQ的支持,这里在之前学习SpringBoot的时候也整合过,但是今天这里使用的Spring的xml配置来整个rabbit.
Spring AMQP项目地址:https://spring.io/projects/spring-amqp;
下面来看看使用方式。
1. 引入依赖
pom文件如下:
<?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>com.rabbitmq.wangx</groupId> <artifactId>rabbitmq</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>rabbitmq Maven Webapp</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.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> <maven.test.failure.ignore>true</maven.test.failure.ignore> <spring.framework.version>4.3.9.RELEASE</spring.framework.version> <spring.amqp.version>2.1.1.RELEASE</spring.amqp.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> <maven.test.failure.ignore>false</maven.test.failure.ignore> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>com.rabbitmq</groupId> <artifactId>amqp-client</artifactId> <version>5.5.0</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.8.0-beta2</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.8.0-beta2</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.framework.version}</version> <exclusions> <!-- Exclude Commons Logging in favor of SLF4j --> <exclusion> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.framework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-messaging</artifactId> <version>${spring.framework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.framework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.framework.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib-nodep</artifactId> <version>2.2</version> <scope>runtime</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.22</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>1.7.22</version> <scope>runtime</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.22</version> <scope>runtime</scope> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.14</version> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.amqp</groupId> <artifactId>spring-amqp</artifactId> <version>${spring.amqp.version}</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.amqp</groupId> <artifactId>spring-rabbit</artifactId> <version>${spring.amqp.version}</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> </exclusion> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring-messaging</artifactId> </exclusion> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> </exclusion> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <finalName>rabbitmq</finalName> <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.0.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.7.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.20.1</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.0</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>
2. 配置基础信息
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" xsi:schemaLocation="http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--配置connection--> <rabbit:connection-factory id="connectionFactory" host="127.0.0.1" port="5672" username="wangx" password="wangx" virtual-host="/wangx"/> <!--使用admin管理factory--> <rabbit:admin connection-factory="connectionFactory"/> <!--定义模板,并指定交换机或queue--> <rabbit:template id="rabbitTemplate" connection-factory="connectionFactory" exchange="spring-exchange"/> <!--定义消费者所对应的key--> <rabbit:queue name="spring-queue" auto-declare="true"/> <!--声明fanout的交换机--> <rabbit:fanout-exchange name="spring-exchange" auto-declare="true"> <rabbit:bindings> <rabbit:binding queue="spring-queue"/> </rabbit:bindings> </rabbit:fanout-exchange> <!--声明监听容器--> <rabbit:listener-container connection-factory="connectionFactory"> <!--指定监听器所对应的bean和方法--> <rabbit:listener ref="consumer" method="listen" queue-names="spring-queue"/> </rabbit:listener-container> <!--消费者监听方法--> <bean id="consumer" class="com.wangx.rabbitmq.spring.Consumer"/> </beans>
消费者:
Consumer
package com.wangx.rabbitmq.spring; public class Consumer { //对应监听器中所指定的method public void listen(String msg) { System.out.println("Spring Consumer 接收到消息:" + msg); } }
发送消息:
package com.wangx.rabbitmq.spring; import org.springframework.amqp.core.Message; import org.springframework.amqp.core.MessageProperties; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Producer { public static void main(String[] args) { //读取配置文件 ApplicationContext context = new ClassPathXmlApplicationContext("rabbitMQ.xml"); //获取模板类 RabbitTemplate template = context.getBean(RabbitTemplate.class); String str = "Spring-message"; //构建MessageProperties对象 MessageProperties messageProperties = new MessageProperties(); //设置参数 messageProperties.setContentEncoding("UTF-8"); messageProperties.setContentType("text/plain"); //构建消息对象 Message message = new Message(str.getBytes(), messageProperties); //使用模板发送消息 template.send(message); } }
注意:
messageProperties.setContentEncoding("UTF-8");
messageProperties.setContentType("text/plain");
这两个参数必须要配置,否则会报如下异常:
D:devsoftjdk1.8jdk1.8injava.exe -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:58888,suspend=y,server=n -javaagent:C:Usersxing.wang.IntelliJIdea2018.1systemcaptureAgentdebugger-agent.jar=file:/C:/Users/xing.wang/AppData/Local/Temp/capture.props -Dfile.encoding=UTF-8 -classpath "D:devsoftjdk1.8jdk1.8jrelibcharsets.jar;D:devsoftjdk1.8jdk1.8jrelibdeploy.jar;D:devsoftjdk1.8jdk1.8jrelibextaccess-bridge-64.jar;D:devsoftjdk1.8jdk1.8jrelibextcldrdata.jar;D:devsoftjdk1.8jdk1.8jrelibextdnsns.jar;D:devsoftjdk1.8jdk1.8jrelibextjaccess.jar;D:devsoftjdk1.8jdk1.8jrelibextjfxrt.jar;D:devsoftjdk1.8jdk1.8jrelibextlocaledata.jar;D:devsoftjdk1.8jdk1.8jrelibext ashorn.jar;D:devsoftjdk1.8jdk1.8jrelibextsunec.jar;D:devsoftjdk1.8jdk1.8jrelibextsunjce_provider.jar;D:devsoftjdk1.8jdk1.8jrelibextsunmscapi.jar;D:devsoftjdk1.8jdk1.8jrelibextsunpkcs11.jar;D:devsoftjdk1.8jdk1.8jrelibextzipfs.jar;D:devsoftjdk1.8jdk1.8jrelibjavaws.jar;D:devsoftjdk1.8jdk1.8jrelibjce.jar;D:devsoftjdk1.8jdk1.8jrelibjfr.jar;D:devsoftjdk1.8jdk1.8jrelibjfxswt.jar;D:devsoftjdk1.8jdk1.8jrelibjsse.jar;D:devsoftjdk1.8jdk1.8jrelibmanagement-agent.jar;D:devsoftjdk1.8jdk1.8jrelibplugin.jar;D:devsoftjdk1.8jdk1.8jrelib esources.jar;D:devsoftjdk1.8jdk1.8jrelib t.jar;D:dev-ideamiddle abbitmq argetclasses;D: epositorycom abbitmqamqp-client5.5.0amqp-client-5.5.0.jar;D: epositoryorgslf4jslf4j-api1.7.22slf4j-api-1.7.22.jar;D: epositoryorgslf4jslf4j-simple1.8.0-beta2slf4j-simple-1.8.0-beta2.jar;D: epositoryorgspringframeworkspring-core4.3.9.RELEASEspring-core-4.3.9.RELEASE.jar;D: epositoryorgspringframeworkspring-context4.3.9.RELEASEspring-context-4.3.9.RELEASE.jar;D: epositoryorgspringframeworkspring-aop4.3.9.RELEASEspring-aop-4.3.9.RELEASE.jar;D: epositoryorgspringframeworkspring-beans4.3.9.RELEASEspring-beans-4.3.9.RELEASE.jar;D: epositoryorgspringframeworkspring-expression4.3.9.RELEASEspring-expression-4.3.9.RELEASE.jar;D: epositoryorgspringframeworkspring-messaging4.3.9.RELEASEspring-messaging-4.3.9.RELEASE.jar;D: epositoryorgspringframeworkspring-tx4.3.9.RELEASEspring-tx-4.3.9.RELEASE.jar;D: epositorycglibcglib-nodep2.2cglib-nodep-2.2.jar;D: epositoryorgslf4jjcl-over-slf4j1.7.22jcl-over-slf4j-1.7.22.jar;D: epositoryorgslf4jslf4j-log4j121.7.22slf4j-log4j12-1.7.22.jar;D: epositorylog4jlog4j1.2.14log4j-1.2.14.jar;D: epositoryorgspringframeworkamqpspring-amqp2.1.1.RELEASEspring-amqp-2.1.1.RELEASE.jar;D: epositoryorgspringframework etryspring-retry1.2.2.RELEASEspring-retry-1.2.2.RELEASE.jar;D: epositoryorgspringframeworkamqpspring-rabbit2.1.1.RELEASEspring-rabbit-2.1.1.RELEASE.jar;D:devsoftideaIntelliJ IDEA 2018.1.4libidea_rt.jar" com.wangx.rabbitmq.spring.Producer Connected to the target VM, address: '127.0.0.1:58888', transport: 'socket' INFO [t.support.ClassPathXmlApplicationContext]: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3fee9989: startup date [Mon Nov 19 16:22:38 CST 2018]; root of context hierarchy INFO [eans.factory.xml.XmlBeanDefinitionReader]: Loading XML bean definitions from class path resource [rabbitMQ.xml] INFO [ontext.support.DefaultLifecycleProcessor]: Starting beans in phase 2147483647 INFO [bbit.connection.CachingConnectionFactory]: Attempting to connect to: 127.0.0.1:5672 INFO [bbit.connection.CachingConnectionFactory]: Created new connection: connectionFactory#47caedad:0/SimpleConnection@2c4d1ac [delegate=amqp://wangx@127.0.0.1:5672//wangx, localPort= 58894] WARN [istener.ConditionalRejectingErrorHandler]: Execution of Rabbit message listener failed. org.springframework.amqp.rabbit.listener.exception.ListenerExecutionFailedException: Failed to invoke target method 'listen' with argument type = [class [B], value = [{[B@3f099764}] at org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter.invokeListenerMethod(MessageListenerAdapter.java:387) at org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter.onMessage(MessageListenerAdapter.java:292) at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:1511) at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.actualInvokeListener(AbstractMessageListenerContainer.java:1434) at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:1421) at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.executeListener(AbstractMessageListenerContainer.java:1400) at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.doReceiveAndExecute(SimpleMessageListenerContainer.java:848) at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.receiveAndExecute(SimpleMessageListenerContainer.java:832) at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$700(SimpleMessageListenerContainer.java:78) at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1073) at java.lang.Thread.run(Thread.java:748) Caused by: java.lang.NoSuchMethodException: com.wangx.rabbitmq.spring.Consumer.listen([B) at java.lang.Class.getMethod(Class.java:1786) at org.springframework.util.MethodInvoker.prepare(MethodInvoker.java:174) at org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter.invokeListenerMethod(MessageListenerAdapter.java:365) ... 10 more WARN [ingErrorHandler$DefaultExceptionStrategy]: Fatal message conversion error; message rejected; it will be dropped or routed to a dead letter exchange, if so configured: (Body:'[B@3f099764(byte[14])' MessageProperties [headers={}, contentType=application/octet-stream, contentLength=0, receivedDeliveryMode=PERSISTENT, priority=0, redelivered=false, receivedExchange=spring-exchange, receivedRoutingKey=, deliveryTag=1, consumerTag=amq.ctag-ZbVlvRKHCdm3KFhzW5uu8A, consumerQueue=spring-queue]) ERROR [.listener.SimpleMessageListenerContainer]: Execution of Rabbit message listener failed, and the error handler threw an exception org.springframework.amqp.AmqpRejectAndDontRequeueException: Error Handler converted exception to fatal at org.springframework.amqp.rabbit.listener.ConditionalRejectingErrorHandler.handleError(ConditionalRejectingErrorHandler.java:106) at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.invokeErrorHandler(AbstractMessageListenerContainer.java:1334) at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.handleListenerException(AbstractMessageListenerContainer.java:1585) at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.executeListener(AbstractMessageListenerContainer.java:1415) at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.doReceiveAndExecute(SimpleMessageListenerContainer.java:848) at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.receiveAndExecute(SimpleMessageListenerContainer.java:832) at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$700(SimpleMessageListenerContainer.java:78) at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1073) at java.lang.Thread.run(Thread.java:748) Caused by: org.springframework.amqp.rabbit.listener.exception.ListenerExecutionFailedException: Failed to invoke target method 'listen' with argument type = [class [B], value = [{[B@3f099764}] at org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter.invokeListenerMethod(MessageListenerAdapter.java:387) at org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter.onMessage(MessageListenerAdapter.java:292) at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:1511) at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.actualInvokeListener(AbstractMessageListenerContainer.java:1434) at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:1421) at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.executeListener(AbstractMessageListenerContainer.java:1400) ... 5 more Caused by: java.lang.NoSuchMethodException: com.wangx.rabbitmq.spring.Consumer.listen([B) at java.lang.Class.getMethod(Class.java:1786) at org.springframework.util.MethodInvoker.prepare(MethodInvoker.java:174) at org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter.invokeListenerMethod(MessageListenerAdapter.java:365) ... 10 more