作者 朱兆平

多线程处理

... ... @@ -4,9 +4,9 @@
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>
<packaging>jar</packaging>
<groupId>com.tianbo</groupId>
<artifactId>messagebus-trans-message</artifactId>
<version>1.0-parse</version>
<groupId>com.tianbo.messagebus</groupId>
<artifactId>cdhz-parse</artifactId>
<version>1.2-parse</version>
<description>消息转发服务</description>
<parent>
... ...
... ... @@ -31,7 +31,7 @@ public class MessageTransApplication {
@Bean
public TaskScheduler taskScheduler() {
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
taskScheduler.setPoolSize(100);
taskScheduler.setPoolSize(3);
return taskScheduler;
}
... ...
... ... @@ -15,7 +15,7 @@ public class KafkaReciveFallback implements KafkaReciveApi {
@Override
public ResultJson<List<String>> recive(String username) {
log.info("获取消息失败");
log.info("[FEGIN-ERR]获取消息失败");
return new ResultJson<>("400","获取消息失败",new ArrayList<>());
}
}
... ...
package com.tianbo.messagebus.service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tianbo.messagebus.controller.response.ResultJson;
import com.tianbo.messagebus.model.CUSTOM_RESPONSE;
import com.tianbo.messagebus.model.CustomReception;
import com.tianbo.messagebus.model.HEADER;
import com.tianbo.messagebus.myinterface.KafkaReciveApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.List;
@Service
@Slf4j
@EnableAsync
public class Custom_Response_Processor {
/**
* 账号名
*/
@Value("${message-bus.auth.username}")
private String USER_NAME;
@Autowired
KafkaReciveApi kafkaReciveApi;
@Autowired
Custom_Response_Service custom_response_service;
/**
* feigin从服务直接获取消息
*/
@Async
@Scheduled(fixedRate = 5000)
public void getDataFromFeigin(){
try{
//初始化数据库
CUSTOM_RESPONSE test = custom_response_service.selectByPrimaryKey("111");
log.info("1-开始执行获取任务,获取账号为:{}",USER_NAME);
if(!StringUtils.isEmpty(USER_NAME)){
analysis();
}
}catch (Exception e){
log.error("[MAIN-ERR]!!!处理消息出错:{}!!!",e.toString());
e.printStackTrace();
}
}
public void analysis(){
ResultJson<List<String>> listResultJson = kafkaReciveApi.recive(USER_NAME);
log.info("2-获取结果为:{},数量为:{}",listResultJson.toString(),listResultJson.getData().size());
if ("200".equals(listResultJson.getCode()) && listResultJson.getData()!=null && listResultJson.getData().size()>0){
log.info("3-开始处理获取数据");
List<String> dataList = listResultJson.getData();
for (int i = 0; i <dataList.size() ; i++) {
String msg = dataList.get(i);
try{
analysisMessage(msg,i);
}catch (Exception e){
log.error("[Analysis-ERR]!!!{}报文内容解析异常:{},开始处理下一条消息!!!",msg,e.toString());
e.printStackTrace();
}
}
}else {
log.info("[CONSUMER-RESULT] - 未获取到消息");
}
}
public void analysisMessage(String msg,int i){
if (!StringUtils.isEmpty(msg)){
log.info("4-循环处理消息[{}]--->{}<---",i,msg);
JSONObject rootJson = JSON.parseObject(msg);
JSONObject msgJson = rootJson.getJSONObject("MSG");
//回执实体
JSONObject body = msgJson.getJSONObject("BODY");
//报头
HEADER msgHeader = msgJson.getObject("HEADER",HEADER.class);
//判断类型
if ("CDHZ".equals(msgHeader.getSTYPE())){
analysisHZ(body);
}
}else {
log.error("[MSG-ERR]消息为空");
}
}
public void analysisHZ(JSONObject body){
log.info("@[一]@消息为舱单回执");
//判断回执类型
JSONObject manifest = body.getJSONObject("Manifest");
JSONObject head = manifest.getJSONObject("Head");
if(head != null){
log.info("@[.]开始回执报头解析");
String messageType = head.getString("MessageType");
String messageID = head.getString("MessageID");
String sendTime = head.getString("SendTime");
String senderID = head.getString("SenderID");
String receiverID = head.getString("ReceiverID");
Integer version = head.getInteger("Version");
Integer functionCode = head.getInteger("FunctionCode");
log.info("@[MessageType:{}]回执报头解析完毕",messageType);
if("MT2201".equals(messageType) || "MT9999".equals(messageType) || "MT3201".equals(messageType)){
analysisBody(messageType,manifest,messageID,sendTime,senderID,receiverID,version,functionCode);
}
}else {
log.info("@[四零一]@缺少Manifest或Head节点");
}
}
public void analysisBody(String messageType,
JSONObject manifest,
String messageID,
String sendTime,
String senderID,
String receiverID,
Integer version,
Integer functionCode
){
log.info("@[二]@开始解析:{}",messageType);
CUSTOM_RESPONSE custom_response_nmms2 = new CUSTOM_RESPONSE();
messageType = "MT2201";
// 航班信息
JSONObject response = manifest.getJSONObject("Response");
if (response!=null){
JSONObject borderTransportMeans = response.getJSONObject("BorderTransportMeans");
if (borderTransportMeans!=null ){
String flightNo = "UNKONW";
String flightDate = "20101010";
String journeyid = borderTransportMeans.getString("JourneyID");
//运单信息
JSONObject consignment = response.getJSONObject("Consignment");
if (consignment!=null){
JSONObject responseType = consignment.getJSONObject("ResponseType");
JSONObject transportContractDocument = consignment.getJSONObject("TransportContractDocument");
JSONObject associatedTransportDocument = consignment.getJSONObject("AssociatedTransportDocument");
Integer responseCode = 3;
String responseText = "回执报文未有信息";
if (responseType!=null){
//回执代码
responseCode = responseType.getIntValue("Code");
//回执内容
responseText = responseType.getString("Text");
}
String waybillMaster = "00000000000";
if (transportContractDocument!=null){
waybillMaster = transportContractDocument.getString("ID");
}
String waybillSecond="";
if (associatedTransportDocument!=null){
waybillSecond = associatedTransportDocument.getString("ID");
}
CustomReception customReception = new CustomReception( messageType,
flightNo,
flightDate,
waybillMaster,
waybillSecond,
String.valueOf(responseCode),
responseText,
messageID,
sendTime,
senderID,
receiverID,
String.valueOf(version),
String.valueOf(functionCode));
/**
* 如果回执中没有携带航班信息节点,说明是出错报文
* 到发送日志表根据messageid 找到相应的发送日志报文的航班及运单信息,再进行解析
*/
if(!StringUtils.isEmpty(journeyid)){
String[] flightList = journeyid.split("/");
if(flightList.length > 0){
flightNo = flightList[0];
flightDate = flightList[1];
log.info("@[三]@航班信息为:{}",journeyid);
customReception.setFlightNo(flightNo);
customReception.setFlightDate(flightDate);
}
custom_response_nmms2 = new CUSTOM_RESPONSE(customReception);
}else {
custom_response_nmms2 = new CUSTOM_RESPONSE(customReception);
custom_response_nmms2 = custom_response_service.getWaybillInfoByCutomResponse(custom_response_nmms2);
}
log.info("[(三.一)]{{}",custom_response_nmms2);
int ii = custom_response_service.secondAnalysisReception(custom_response_nmms2);
log.info("@[四]@回执解析完毕[{}]\n@@^PARSE SUCCESS^@@",ii);
}else {
log.info("@[四零零]@回执报文没有运单节点,解析失败.");
}
}else {
log.info("@[四零三]缺少航班信息节点,解析失败");
}
}else {
log.info("@[四零二]缺少回执内容节点,解析失败");
}
}
}
... ...
... ... @@ -397,11 +397,9 @@ public class MessageBusProcessor {
/**
* feigin从服务直接获取消息
*/
@Scheduled(fixedRate = 5000)
// @Scheduled(fixedRate = 5000)
public void getDataFromFeigin(){
try{
//初始化数据库
CUSTOM_RESPONSE test = custom_response_service.selectByPrimaryKey("111");
log.info("1-开始执行获取任务,获取账号为:{}",USER_NAME);
... ... @@ -429,101 +427,128 @@ public class MessageBusProcessor {
log.info("@[一]@消息为舱单回执");
CUSTOM_RESPONSE custom_response_nmms2 = new CUSTOM_RESPONSE();
//判断回执类型
JSONObject Manifest = body.getJSONObject("Manifest");
JSONObject head = Manifest.getJSONObject("Head");
String messageType = head.getString("MessageType");
String messageID = head.getString("MessageID");
String sendTime = head.getString("SendTime");
String senderID = head.getString("SenderID");
String receiverID = head.getString("ReceiverID");
Integer version = head.getInteger("Version");
Integer functionCode = head.getInteger("FunctionCode");
if("MT2201".equals(messageType) || "MT9999".equals(messageType)){
log.info("@[二]@开始解析:{}",messageType);
// 航班信息
JSONObject response = Manifest.getJSONObject("Response");
JSONObject borderTransportMeans = response.getJSONObject("BorderTransportMeans");
String flightNo = "UNKONW";
String flightDate = "20101010";
String journeyid = borderTransportMeans.getString("JourneyID");
//运单信息
JSONObject consignment = response.getJSONObject("Consignment");
JSONObject responseType = consignment.getJSONObject("ResponseType");
JSONObject transportContractDocument = consignment.getJSONObject("TransportContractDocument");
JSONObject associatedTransportDocument = consignment.getJSONObject("AssociatedTransportDocument");
//回执代码
Integer responseCode = responseType.getIntValue("Code");
//回执内容
String responseText = responseType.getString("Text");
Long waybillMaster = transportContractDocument.getLong("ID");
String waybillSecond="";
if (associatedTransportDocument!=null){
waybillSecond = associatedTransportDocument.getString("ID");
}
CustomReception customReception = new CustomReception( messageType,
flightNo,
flightDate,
waybillMaster.toString(),
waybillSecond,
responseCode.toString(),
responseText,
messageID,
sendTime,
senderID,
receiverID,
version.toString(),
functionCode.toString());
/**
* 如果回执中没有携带航班信息节点,说明是出错报文
* 到发送日志表根据messageid 找到相应的发送日志报文的航班及运单信息,再进行解析
*/
if(!org.apache.commons.lang.StringUtils.isEmpty(journeyid)){
String[] flightList = journeyid.split("/");
if(flightList.length > 0){
flightNo = flightList[0];
flightDate = flightList[1];
log.info("@[三]@航班信息为:{}",journeyid);
customReception.setFlightNo(flightNo);
customReception.setFlightDate(flightDate);
JSONObject manifest = body.getJSONObject("Manifest");
JSONObject head = manifest.getJSONObject("Head");
if(head != null){
log.info("@[.]开始回执报头解析");
String messageType = head.getString("MessageType");
String messageID = head.getString("MessageID");
String sendTime = head.getString("SendTime");
String senderID = head.getString("SenderID");
String receiverID = head.getString("ReceiverID");
Integer version = head.getInteger("Version");
Integer functionCode = head.getInteger("FunctionCode");
log.info("@[MessageType:{}]回执报头解析完毕",messageType);
if("MT2201".equals(messageType) || "MT9999".equals(messageType) || "MT3201".equals(messageType)){
log.info("@[二]@开始解析:{}",messageType);
messageType = "MT2201";
// 航班信息
JSONObject response = manifest.getJSONObject("Response");
if (response!=null){
JSONObject borderTransportMeans = response.getJSONObject("BorderTransportMeans");
if (borderTransportMeans!=null ){
String flightNo = "UNKONW";
String flightDate = "20101010";
String journeyid = borderTransportMeans.getString("JourneyID");
//运单信息
JSONObject consignment = response.getJSONObject("Consignment");
if (consignment!=null){
JSONObject responseType = consignment.getJSONObject("ResponseType");
JSONObject transportContractDocument = consignment.getJSONObject("TransportContractDocument");
JSONObject associatedTransportDocument = consignment.getJSONObject("AssociatedTransportDocument");
Integer responseCode = 3;
String responseText = "回执报文未有信息";
if (responseType!=null){
//回执代码
responseCode = responseType.getIntValue("Code");
//回执内容
responseText = responseType.getString("Text");
}
String waybillMaster = "00000000000";
if (transportContractDocument!=null){
waybillMaster = transportContractDocument.getString("ID");
}
String waybillSecond="";
if (associatedTransportDocument!=null){
waybillSecond = associatedTransportDocument.getString("ID");
}
CustomReception customReception = new CustomReception( messageType,
flightNo,
flightDate,
waybillMaster.toString(),
waybillSecond,
responseCode.toString(),
responseText,
messageID,
sendTime,
senderID,
receiverID,
version.toString(),
functionCode.toString());
/**
* 如果回执中没有携带航班信息节点,说明是出错报文
* 到发送日志表根据messageid 找到相应的发送日志报文的航班及运单信息,再进行解析
*/
if(!org.apache.commons.lang.StringUtils.isEmpty(journeyid)){
String[] flightList = journeyid.split("/");
if(flightList.length > 0){
flightNo = flightList[0];
flightDate = flightList[1];
log.info("@[三]@航班信息为:{}",journeyid);
customReception.setFlightNo(flightNo);
customReception.setFlightDate(flightDate);
}
custom_response_nmms2 = new CUSTOM_RESPONSE(customReception);
}else {
custom_response_nmms2 = new CUSTOM_RESPONSE(customReception);
custom_response_nmms2 = custom_response_service.getWaybillInfoByCutomResponse(custom_response_nmms2);
}
log.info("[(三.一)]{{}",custom_response_nmms2);
int ii = custom_response_service.secondAnalysisReception(custom_response_nmms2);
log.info("@[四]@回执解析完毕[{}]\n@@^PARSE SUCCESS^@@",ii);
}else {
log.info("@[四零零]@回执报文没有运单节点,解析失败.");
}
}else {
log.info("@[四零三]缺少航班信息节点,解析失败");
}
}else {
log.info("@[四零二]缺少回执内容节点,解析失败");
}
custom_response_nmms2 = new CUSTOM_RESPONSE(customReception);
}else {
custom_response_nmms2 = new CUSTOM_RESPONSE(customReception);
custom_response_nmms2 = custom_response_service.getWaybillInfoByCutomResponse(custom_response_nmms2);
}
log.info("[(三.一)]{{}",custom_response_nmms2);
}else {
log.info("@[四零一]@缺少Manifest或Head节点");
}
int ii = custom_response_service.secondAnalysisReception(custom_response_nmms2);
log.info("@[四]@回执解析完毕[{}]\n@@^PARSE SUCCESS^@@",ii);
}
}
}
}
}
}catch (Exception e){
log.info("!!!回执解析异常:{}!!!",e.toString());
log.error("!!!处理消息出错:{}!!!",e.toString());
e.printStackTrace();
}
... ...