程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

The python side sends rabbitmq messages, and the Java side accepts messages that are all bytecodes

編輯:Python

background :
During the performance function self-test , Need to simulate a mq The message to MQ Management interface . Thinking about python More simple point , So using python to Mq Send messages in the management interface .

1.python Send a message to MQ Management interface

Go straight to the code , I don't say much nonsense .

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import pika
import json
credentials = pika.PlainCredentials('admin', '[email protected],') # mq User name and password 
# Virtual queues need to specify parameters virtual_host, If it is the default, you can leave it blank .
connection = pika.BlockingConnection(pika.ConnectionParameters(host='172.16.1.231', port=5672, virtual_host='/', credentials=credentials))
channel=connection.channel()
# Declare message queue , Messages will be delivered in this queue , If there is no , Create 
#result = channel.queue_declare(queue='msgqueue_otn_collect_request_queue', durable=True, auto_delete=True)
message = json.dumps({

"jobname": "yanglin",
"collect_time": "12345678",
"ne": [
{

"neid": "100009",
"ip": "172.16.65.111",
"port": "161",
"device_type": "snmp",
"protoParam": {

"v3securityLevel": "noAuthNoPriv",
"v3authKey": "",
"version": 2,
"timeout": 5,
"communityRead": "public",
"v3privProtocol": "DES",
"retries": 2,
"v3privKey": "",
"v3securityName": "",
"v3authProtocol": "MD5"
},
"rsurl": [
{

"rsurl": "/ne=100009/shelf=1/slot=1/card=1.1/port=1#portType=262",
"urlhash": "12345678",
"index": 1,
"metricGroup": [
{

"metricGroupId": 11104,
"oid": [
{

"name": "opticalPowerOut",
"oid": "0500111202"
},
{

"name": "opticalPowerIn",
"oid": "0500111203"
}
]
},
{

"metricGroupId": 30207,
"oid": [
{

"name": "neTemperature",
"oid": "1111111111"
}
]
}
]
}
]
}
]
})
# Insert values into the queue routing_key It's a queue name 
msg_body = message + ""
channel.basic_publish(exchange='tiap_ems_basic_mgt_direct_exchange', routing_key='msgqueue_otn_collect_request_queue', body=msg_body)
print(msg_body)
print(type(msg_body))
connection.close()

issue java The background is a character type json strand .

2. Java Receiving messages in the background

import com.raisecom.tiap.ems.basic.mgt.constant.RabbitConstant;
import com.raisecom.tiap.ems.basic.mgt.service.performance.PerfCollection8600CommonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;
/** * Class function description :<br> * <ul> * <li> Class function description 1<br> * <li> Class function description 2<br> * <li> Class function description 3<br> * </ul> * Modify the record :<br> * <ul> * <li> Modify the record description 1<br> * <li> Modify the record description 2<br> * <li> Modify the record description 3<br> * </ul> * * @author yanglin * @version 1.0 */
@Component
@Slf4j
public class PerformanceCollectListener {

@Autowired
PerfCollection8600CommonService perfCollection8600CommonService;
//public static final String MSGQUEUE_OTN_COLLECT_REQUEST_QUEUE = "msgqueue_otn_collect_request_queue";
@RabbitListener(queues = {
 RabbitConstant.MSGQUEUE_OTN_COLLECT_REQUEST_QUEUE})
@RabbitHandler
public void process(@Payload Message msgBody) {

try {

log.info("PerformanceCollectListener get msgBody++++++++++++++++:" + msgBody);
String messageBody = new String(msgBody.getBody());
perfCollection8600CommonService.process(messageBody);
} catch (Throwable e) {

log.error("process error {}", e);
}
}
}

In the code process The parameters behind the method use Message type It can solve the bytecode problem .
Below are the problems encountered in the process of trying .

2.1 Parameters use String type

public void process(@Payload String msgBody) {

perfCollection8600CommonService.process(msgBody);
}

Will find msgBody It's bytecode . Try it yourself You know the .

  • First, Chinese byte code cannot be displayed normally .
  • In case of non Chinese, it is necessary to String msgBody To str type , annoying .
2.1.1 Byte to byte str
@RabbitListener(queues = {
 RabbitConstant.MSGQUEUE_OTN_COLLECT_REQUEST_QUEUE})
public void process(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;
}

2.2 Parameters use Object type

@RabbitListener(queues = {
 RabbitConstant.MSGQUEUE_OTN_COLLECT_REQUEST_QUEUE})
public void process(Object message) {

System.out.println(message);
}

Find out message object ,Python The sending end still sends in the form of bytecode ( see message object ),java The sender is normal .

About java send out json The message to mq The management interface is not introduced here , There will be an article .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved