您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“RabbitMQ Python端發(fā)送消息給Java端出現(xiàn)全數(shù)字問(wèn)題怎么解決”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“RabbitMQ Python端發(fā)送消息給Java端出現(xiàn)全數(shù)字問(wèn)題怎么解決”吧!
Python發(fā)送端用 pika 發(fā)送消息
發(fā)送代碼是這樣的:
#!/usr/bin/python# -*- coding:utf-8 -*-import pikaimport json credentials = pika.PlainCredentials('ding', 'ding')connection = pika.BlockingConnection(pika.ConnectionParameters('192.168.0.200',5672,'/',credentials))channel = connection.channel()channel.queue_declare(queue='test queue')msg = 'GG next'channel.basic_publish(exchange='',routing_key='test queue',body=msg)connection.close()
Java接收端是 Spring boot 項(xiàng)目,集成了 RabbitMQ 的包:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency>
接收代碼是這樣的:
@RabbitListener(queues = "test queue")public void process2(String message) { System.out.println(message);}
運(yùn)行Python腳本,Java端得到的結(jié)果是這樣的:
71,71,32,110,101,120,116
結(jié)果全是 ASCII碼了!之前用Java集成了 RabbitMQ,Java和Java之間是沒(méi)有這個(gè)問(wèn)題的。于是想到在Java端把 ASCII 碼轉(zhuǎn)換為字符串,就寫(xiě)了下面的代碼:
@RabbitListener(queues = "test queue")public void process2(String message) { System.out.println(arrayToStr(ascToArray(message)));}private String arrayToStr(int[] arr) { String res = "";for (int i = 0; i < arr.length; i++) { res += Character.toString((char)arr[i]);}return res;}private int[] ascToArray(String str) { String[] arr = str.split(",");int[] resArr = new int[arr.length];for (int i = 0; i < arr.length; i++) { resArr[i] = Integer.parseInt(arr[i]);}return resArr;}
得到的結(jié)果正常了:
GG next
正有點(diǎn)開(kāi)心之際,想到一個(gè)問(wèn)題,中文字符怎么辦?一個(gè)漢字兩個(gè)字節(jié),ASCII碼可表示不了???
于是試驗(yàn)一下Python端發(fā)送
msg = 'GG 下一把'
Java端果然出問(wèn)題了:
GG ¦??¦?????
這時(shí)我們可以看看 RabbitMQ 給我發(fā)送過(guò)來(lái)的到底是啥,Java端代碼變成這樣:
@RabbitListener(queues = "test queue")public void process2(Object message) { System.out.println(message);}
Python發(fā)送端發(fā)來(lái)的是這樣的:
(Body:'[B@49d1204c(byte[12])' MessageProperties [headers={}, contentLength=0, redelivered=false, receivedExchange=, receivedRoutingKey=test queue, deliveryTag=1, consumerTag=amq.ctag-Q4Oy_yC5qRoLoxAdu50zaw, consumerQueue=test queue])
Java發(fā)送端發(fā)來(lái)的是這樣的:
(Body:'GG 下一把' MessageProperties [headers={}, contentType=text/plain, contentEncoding=UTF-8, contentLength=0, receivedDeliveryMode=PERSISTENT, priority=0, redelivered=false, receivedExchange=, receivedRoutingKey=queue, deliveryTag=1, consumerTag=amq.ctag-o1v48tbAr8mu-pT7HZHvHA, consumerQueue=queue])
居然一個(gè)是 byte[] 數(shù)組一個(gè)是 String這可怎么辦?解決代碼如下:
import org.springframework.amqp.core.Message;@RabbitListener(queues = "test queue")public void process2(Message message) { String messageBody = new String(message.getBody());System.out.println(messageBody);}
不論是Java端還是Python端發(fā)來(lái)的,都能正確顯示了。
GG 下一把
到此,相信大家對(duì)“RabbitMQ Python端發(fā)送消息給Java端出現(xiàn)全數(shù)字問(wèn)題怎么解決”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。