1. 程式人生 > >Spring Boot使用RabbitMQ出現詭異異常:Failed to send reply with payload 'OK',Cannot determine ReplyTo message

Spring Boot使用RabbitMQ出現詭異異常:Failed to send reply with payload 'OK',Cannot determine ReplyTo message

今天專案中出現一個詭異的異常:

org.springframework.amqp.rabbit.listener.exception.ListenerExecutionFailedException: Listener threw exception
	at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.wrapToListenerExecutionFailedExceptionIfNeeded(AbstractMessageListenerContainer.java:915)
	at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:825)
	at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:745)
	at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$001(SimpleMessageListenerContainer.java:97)
	at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$1.invokeListener(SimpleMessageListenerContainer.java:189)
	at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.invokeListener(SimpleMessageListenerContainer.java:1276)
	at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.executeListener(AbstractMessageListenerContainer.java:726)
	at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.doReceiveAndExecute(SimpleMessageListenerContainer.java:1219)
	at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.receiveAndExecute(SimpleMessageListenerContainer.java:1189)
	at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$1500(SimpleMessageListenerContainer.java:97)
	at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1421)
	at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.amqp.rabbit.listener.adapter.ReplyFailureException: Failed to send reply with payload 'OK'
	at org.springframework.amqp.rabbit.listener.adapter.AbstractAdaptableMessageListener.handleResult(AbstractAdaptableMessageListener.java:285)
	at org.springframework.amqp.rabbit.listener.adapter.MessagingMessageListenerAdapter.onMessage(MessagingMessageListenerAdapter.java:108)
	at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:822)
	... 10 common frames omitted
Caused by: org.springframework.amqp.AmqpException: Cannot determine ReplyTo message property value: Request message does not contain reply-to property, and no default response Exchange was set.
	at org.springframework.amqp.rabbit.listener.adapter.AbstractAdaptableMessageListener.getReplyToAddress(AbstractAdaptableMessageListener.java:373)
	at org.springframework.amqp.rabbit.listener.adapter.AbstractAdaptableMessageListener.handleResult(AbstractAdaptableMessageListener.java:281)
	... 12 common frames omitted

專案使用Spring Boot、Gradle、Groovy、RabbitMQ,出現異常的程式碼如下:

@RabbitListener(queues = Constants.POST_PUSH_QUEUE_THIRD_REDIS)
    @RabbitHandler
    def processThirdRedis(Article article) throws Exception {
        def key = REDIS_ARTICLE_PREFIX + article.id
        redisService.set(key, JsonUtil.toJson(article))
    }

終於在這裡找到了答案。

原來,使用

@RabbitListener(queues = Constants.POST_PUSH_QUEUE_THIRD_REDIS)
    @RabbitHandler
修飾的方法,即佇列的監聽函式,不能返回任何值!否則會導致一個rabbit reply message  回覆異常,該異常是由於此方法返回的訊息沒有設定目的地,檢視原始碼可以看出:
protected void handleResult(Object resultArg, Message request, Channel channel, Object source) throws Exception {
		if (channel != null) {
			if (this.logger.isDebugEnabled()) {
				this.logger.debug("Listener method returned result [" + resultArg
						+ "] - generating response message for it");
			}
			try {
				Object result = resultArg instanceof ResultHolder ? ((ResultHolder) resultArg).result : resultArg;
				Message response = buildMessage(channel, result);
				postProcessResponse(request, response);
				Address replyTo = getReplyToAddress(request, source, resultArg);
				sendResponse(channel, replyTo, response);
			}
			catch (Exception ex) {
				throw new ReplyFailureException("Failed to send reply with payload '" + resultArg + "'", ex);
			}
		}
		else if (this.logger.isWarnEnabled()) {
			this.logger.warn("Listener method returned result [" + resultArg
					+ "]: not generating response message for it because no Rabbit Channel given");
		}
	}
其中getReplyToAddress()方法:
protected Address getReplyToAddress(Message request, Object source, Object result) throws Exception {
		Address replyTo = request.getMessageProperties().getReplyToAddress();
		if (replyTo == null) {
			if (this.responseAddress == null && this.responseExchange != null) {
				this.responseAddress = new Address(this.responseExchange, this.responseRoutingKey);
			}
			if (result instanceof ResultHolder) {
				replyTo = evaluateReplyTo(request, source, result, ((ResultHolder) result).sendTo);
			}
			else if (this.responseExpression != null) {
				replyTo = evaluateReplyTo(request, source, result, this.responseExpression);
			}
			else if (this.responseAddress == null) {
				throw new AmqpException(
						"Cannot determine ReplyTo message property value: " +
								"Request message does not contain reply-to property, " +
								"and no default response Exchange was set.");
			}
			else {
				replyTo = this.responseAddress;
			}
		}
		return replyTo;
	}

而我的程式碼為什麼會出現這個異常呢?因為Groovy的方法會自動將最後一句的結果返回,所有不能使用def定義方法,而應該使用void!

解決後的程式碼為:

@RabbitListener(queues = Constants.POST_PUSH_QUEUE_THIRD_REDIS)
    @RabbitHandler
    void processThirdRedis(Article article) throws Exception {
        def key = REDIS_ARTICLE_PREFIX + article.id
        redisService.set(key, JsonUtil.toJson(article))
    }

相關推薦

Spring Boot使用RabbitMQ出現詭異異常Failed to send reply with payload 'OK'Cannot determine ReplyTo message

今天專案中出現一個詭異的異常:org.springframework.amqp.rabbit.listener.exception.ListenerExecutionFailedException: Listener threw exception at org.sprin

解決Maven打包怪異異常Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:2.6:resource

今天在使用maven打包時正常操作卻報出瞭如題的錯誤:         Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:2.6:resource......         這個

關於spring-data-mongodb用戶名密碼登錄報錯問題Failed to authenticate to database

數據 base 設置 thread read ber ram tro pat 一.問題   1.spring-data-mongodb用戶名密碼登錄報錯問題:Failed to authenticate to database org.springframew

U盤安裝Ubuntu 16.04出現Failed to load ldlinux.c32

u盤啟動 ins tps failed target als spa span b- 啟動的時候如果不開啟UEFI,則會提示: Failed to load ldlinux.c32 Boot failed: please change disks and press

tomcat啟動時出現Failed to start component [StandardEngine[Catalina].StandardHost[localhost].錯誤 的原因

tomcat執行專案出現:Failed to start component [StandardEngine[Catalina].StandardHost[localhost]. 並且出現cause by : java.lang.ClassFormatError: Absent Code att

spring-boot整合mybatis使用druid資料來源時Failed to bind properties under'spring.datasource' to javax.sql.Data

其實,只是缺少了報錯內容中的Value值中的mavenrepository資料 在pom.xml中引入stat ,wall,log4j就解決了。 我的只需要引入log4j就能正常執行專案了,( 因為Reason的原因有寫,就是:org.apache.log4j.Logger 

eclipse遠端除錯時出現Failed to connect to remote VM. Connection refused. Connection refused: connect

如圖: 以下都是在網路中找的解決方案: 解決方案1:服務是在root 賬戶下啟動的,關掉服務殺掉程序,然後用普通使用者登入重啟服務!(對我無效,因為我的服務不可以用普通使用者啟動) 解決方案2:在Eclipse中找到Run-Debug Configur

Android下載sdk中出現如下錯誤時Failed to rename directory...解決辦法

1、備份tools資料夾 2、將temp資料夾下的ToolPackage.new01改名為tools並替換原來的tools 3、重新啟動SDK Setup 好了,問題解決了,開啟sdk你就可以看到正在下載platform和api了。

Maven部署到Tomcat出現錯誤Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1

大家都知道錯誤內容相同時,出現的場景也不一定相同。所以本篇提出的錯誤解決方案可能僅僅適用跟我相同場景下的情況。 1、出現錯誤的場景: 按照我之前一篇文章【Eclipse中Maven的整合和使用介紹】進行配置Maven之後,在再src/main/java資料夾下建立Servl

SpringBoot啟動報錯Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified and

      今天倒騰了一下自己的SpringBoot demo的程式碼,發現啟動報瞭如下的錯誤: *************************** APPLICATION FAILED TO START *************************** Des

springboot打包成war後部署項目出現異常 LifecycleException: Failed to start component

查看 release embed cep exce local .net lifecycle ali 完整異常:org.apache.catalina.LifecycleException: Failed to start component [Standard

maven出現Failed to execute goal on project ...: Could not resolve dependencies for project ...

出現 end pan maven could clear pro mon resolv 項目結構是一個父項目,多個子項目目錄: 例如: common --------------(父項目) fristDemo ------------(子項目

SSM單元測試時出現Failed to load ApplicationContext的一種可能解決辦法

allow .class 網上 sent contex font enc example clu SSM單元測試時出現: 嚴重: Caught exception while allowing TestExecutionListener [org.springframew

swagger ui js 錯誤Failed to execute 'serializeToString' on 'XMLSerializer': parameter 1 is not of type 'Node'.

排除法 解決 swa set ring param execute 錯誤 exec 經過排查,引發此錯誤的原因是,表中有一個字段名稱為“NodeName”,應該是在前臺xml解析時引發沖突所致。我的解決辦法是: 修改列名,修改映射。 如下: [Column("NodeNa

Struts2配置攔截器自定義棧時拋異常Unable to load configuration. - interceptor-ref - file:/D:/tomcat_install/webapps/crm/WEB-INF/classes/struts.xml

註意 其他 默認 才會 eba staf -i sta fault 代碼如下: <interceptors> <!-- 註冊自定義攔截器 --> <interceptor name="LoginInterceptor" class="co

hadoop解決windows下Failed to set permissions of path: mp .staging to 0700

1.0 style ati 配置文件 github unable ica perm lac 17/04/24 15:32:44 WARN util.NativeCodeLoader: Unable to load native-Hadoop library for your

解決 RHEL 7/ CentOS 7/Fedora 出現Unit iptables.service failed to load

wall failed 出現 fail rest exec 分開 fire iptable 防火墻配置後執行service iptables save 出現"Failed to restart iptables.service: Unit iptables.servic

CentOS系統中出現錯誤--SSHconnect to host centos-py port 22: Connection refused

start 配置 下載 pan 要去 isp 執行 hostname openssh   我在第一次搭建自己的 hadoop2.2.0單節點的偽分布集成環境時遇到了此錯誤,通過思考問題和查找解決方案最終搞定了這個問題,其錯誤原因主要有以下幾種:   1)S

cloudstackFailed to delete storage pool

cloudstack 存儲 一、說明cloudstack新建資源池的時候,出現錯誤,因此想著把之前的信息全部刪除,然後再次新建資源池。當刪除主存儲的時候,出現“Failed to delete storage pool”。二、解決方法MariaDB [(none)]> use cloud; Ma

ssh框架搭建出現異常 class com.my.entity.user not found while looking for property: id

增長 native column while 一個 ava pre true not 在處理用戶註冊的時候,user實體的bean創建不出來,原代碼如下: <class name="com.my.entity.User" table="user">