Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the all-digital problem when RabbitMQ Python sends messages to Java?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains how to solve the all-digital problem when RabbitMQ Python sends messages to Java. Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to solve the all-digital problem when sending messages to the RabbitMQ Python side.

RabbitMQ accepts a bunch of digital Bug

Python sender sends messages with pika

The sending code is as follows:

#! / usr/bin/python#-*-coding:utf-8-*-import pikaimport jsoncredentials = pika.PlainCredentials ('ding',' ding') connection = pika.BlockingConnection (pika.ConnectionParameters ('192.168.0.200)) channel = connection.channel () channel.queue_declare (queue='test queue') msg =' GG next'channel.basic_publish (exchange='',routing_key='test queue',body=msg) connection.close ()

The Java receiver is the Spring boot project, which integrates the package of RabbitMQ:

Org.springframework.bootspring-boot-starter-amqp

The receiving code looks like this:

@ RabbitListener (queues = "test queue") public void process2 (String message) {System.out.println (message);}

Run the Python script, and the result on the Java side looks like this:

71,71,32110101120116

It turned out to be all ASCII codes! There is no such problem with the previous integration of RabbitMQ,Java and Java with Java. So thinking of converting ASCII code to a string on the Java side, I wrote the following code:

@ 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; iTunes +) {resArr [I] = Integer.parseInt (arr [I]);} return resArr;}

The results are normal:

GG next

When I was a little happy, I thought of a question: what about Chinese characters? A Chinese character two bytes, ASCII code can not express ah?

So try sending it on Python.

Msg ='GG next'

Sure enough, something went wrong on the Java side:

GG has a lot of trouble.

At this point, we can see what RabbitMQ sent me, and the Java-side code looks like this:

@ RabbitListener (queues = "test queue") public void process2 (Object message) {System.out.println (message);}

The message from the sender of Python reads as follows:

(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])

The message from the sender of Java reads as follows:

(Body:'GG 's next '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])

What if one is an byte [] array and the other is String? The resolution code is as follows:

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);}

Whether it is sent on the Java side or Python side, it can be displayed correctly.

GG next here, I believe that everyone on the "RabbitMQ Python side to send messages to the Java side of the all-digital problem how to solve" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report