package com.pgf.mqspring.service.impl; import java.util.Arrays; import javax.jms.BytesMessage; import javax.jms.JMSException; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.jms.JmsException; import org.springframework.jms.core.JmsTemplate; import org.springframework.stereotype.Service; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.pgf.mqspring.model.ReplyResponseModel; import com.pgf.mqspring.model.SendMapVectorRequestModel; import com.pgf.mqspring.service.MqReceiveService; import com.pgf.mqspring.util.CharacterCodeConverter; @Service public class MqReceiveServiceImpl implements MqReceiveService { private JmsTemplate jmsTemplate = null; private String queue = ""; Logger logger = LogManager.getLogger(); public MqReceiveServiceImpl(JmsTemplate template) { this.jmsTemplate = template; } public String receiveMessage(String id) { try { logger.info("Message ID for Receive =" + id); StringBuilder sb = new StringBuilder("CorrelationID = '"); sb.append(id); sb.append("'"); BytesMessage bm = (BytesMessage) jmsTemplate.receiveSelected(queue, sb.toString()); byte[] bytes = bm.getBody(byte[].class); ReplyResponseModel response = new ReplyResponseModel(); response = this.editReplyData(bytes); // MQMD response.getMqmd().setCorrelId(bm.getJMSCorrelationID()); //Json変換 String json = ""; try { ObjectMapper mapper = new ObjectMapper(); json = mapper.writeValueAsString(response); } catch (JsonProcessingException e) { e.printStackTrace(); } return json; } catch (JmsException | JMSException ex) { ex.printStackTrace(); return "500"; } } private ReplyResponseModel editReplyData(byte[] bytes) { ReplyResponseModel response = new ReplyResponseModel(); // MQCIH response.getMqcih().setStrucId(CharacterCodeConverter.decodeEbcdic(Arrays.copyOfRange(bytes, 0, 4))); response.getMqcih().setVersion(String.valueOf(Long.parseLong(CharacterCodeConverter.decodeHexStringData(Arrays.copyOfRange(bytes, 4, 8)), 16))); response.getMqcih().setStrucLength(String.valueOf(Long.parseLong(CharacterCodeConverter.decodeHexStringData(Arrays.copyOfRange(bytes, 8, 12)), 16))); response.getMqcih().setFormat(CharacterCodeConverter.decodeEbcdic(Arrays.copyOfRange(bytes, 20, 28))); response.getMqcih().setFlags(String.valueOf(Long.parseLong(CharacterCodeConverter.decodeHexStringData(Arrays.copyOfRange(bytes, 28, 32)), 16))); response.getMqcih().setReturnCode(String.valueOf(Long.parseLong(CharacterCodeConverter.decodeHexStringData(Arrays.copyOfRange(bytes, 32, 36)), 16))); response.getMqcih().setCompCode(String.valueOf(Long.parseLong(CharacterCodeConverter.decodeHexStringData(Arrays.copyOfRange(bytes, 36, 40)), 16))); response.getMqcih().setReason(String.valueOf(Long.parseLong(CharacterCodeConverter.decodeHexStringData(Arrays.copyOfRange(bytes, 40, 44)), 16))); response.getMqcih().setUowControl(String.valueOf(Long.parseLong(CharacterCodeConverter.decodeHexStringData(Arrays.copyOfRange(bytes, 44, 48)), 16))); response.getMqcih().setGetWaitInterval(String.valueOf(Long.parseLong(CharacterCodeConverter.decodeHexStringData(Arrays.copyOfRange(bytes, 48, 52)), 16))); response.getMqcih().setLinkType(String.valueOf(Long.parseLong(CharacterCodeConverter.decodeHexStringData(Arrays.copyOfRange(bytes, 52, 56)), 16))); response.getMqcih().setOutputDataLength(String.valueOf(Long.parseLong(CharacterCodeConverter.decodeHexStringData(Arrays.copyOfRange(bytes, 56, 60)), 16))); response.getMqcih().setFacilityKeepTime(String.valueOf(Long.parseLong(CharacterCodeConverter.decodeHexStringData(Arrays.copyOfRange(bytes, 60, 64)), 16))); response.getMqcih().setAdsDescriptor(String.valueOf(Long.parseLong(CharacterCodeConverter.decodeHexStringData(Arrays.copyOfRange(bytes, 64, 68)), 16))); response.getMqcih().setConversationalTask(String.valueOf(Long.parseLong(CharacterCodeConverter.decodeHexStringData(Arrays.copyOfRange(bytes, 68, 72)), 16))); response.getMqcih().setTaskEndStatus(String.valueOf(Long.parseLong(CharacterCodeConverter.decodeHexStringData(Arrays.copyOfRange(bytes, 72, 76)), 16))); response.getMqcih().setFacility(CharacterCodeConverter.decodeHexStringData(Arrays.copyOfRange(bytes, 76, 84))); response.getMqcih().setFunction(String.valueOf(Long.parseLong(CharacterCodeConverter.decodeHexStringData(Arrays.copyOfRange(bytes, 84, 88)), 16))); response.getMqcih().setAbendCode(CharacterCodeConverter.decodeEbcdic(Arrays.copyOfRange(bytes, 88, 92))); response.getMqcih().setAutherticator(CharacterCodeConverter.decodeEbcdic(Arrays.copyOfRange(bytes, 92, 100))); response.getMqcih().setReplyToFormat(CharacterCodeConverter.decodeEbcdic(Arrays.copyOfRange(bytes, 108, 116))); String transactionId = CharacterCodeConverter.decodeEbcdic(Arrays.copyOfRange(bytes, 124, 128)); response.getMqcih().setTransactionId(transactionId); response.getMqcih().setFacilityLike(CharacterCodeConverter.decodeEbcdic(Arrays.copyOfRange(bytes, 128, 132))); response.getMqcih().setAttentionId(CharacterCodeConverter.decodeEbcdic(Arrays.copyOfRange(bytes, 132, 136))); response.getMqcih().setStartCode(CharacterCodeConverter.decodeEbcdic(Arrays.copyOfRange(bytes, 136, 140))); response.getMqcih().setCancelCode(CharacterCodeConverter.decodeEbcdic(Arrays.copyOfRange(bytes, 140, 144))); response.getMqcih().setNextTransactionCode(CharacterCodeConverter.decodeEbcdic(Arrays.copyOfRange(bytes, 144, 148))); response.getMqcih().setCursorPositon(String.valueOf(Long.parseLong(CharacterCodeConverter.decodeHexStringData(Arrays.copyOfRange(bytes, 164, 168)), 16))); response.getMqcih().setErrorOffset(String.valueOf(Long.parseLong(CharacterCodeConverter.decodeHexStringData(Arrays.copyOfRange(bytes, 168, 172)), 16))); // Vector SendMapVectorRequestModel sendMapVectorModel = new SendMapVectorRequestModel(); // sendMapVectorModel.setVectorLength(String.valueOf(Long.parseLong(CharacterCodeConverter.decodeHexStringData(Arrays.copyOfRange(bytes, 180, 184)), 16))); sendMapVectorModel.setVectorDescriptor(CharacterCodeConverter.decodeEbcdic(Arrays.copyOfRange(bytes, 184, 188))); sendMapVectorModel.setVectorType(CharacterCodeConverter.decodeEbcdic(Arrays.copyOfRange(bytes, 188, 189))); sendMapVectorModel.setVectorVersion(String.valueOf(Long.parseLong(CharacterCodeConverter.decodeHexStringData(Arrays.copyOfRange(bytes, 189, 196)), 16))); sendMapVectorModel.setScEraseIndic(CharacterCodeConverter.decodeEbcdic(Arrays.copyOfRange(bytes, 196, 200))); sendMapVectorModel.setScEraseaupIndic(CharacterCodeConverter.decodeEbcdic(Arrays.copyOfRange(bytes, 200, 204))); sendMapVectorModel.setScFreekbIndic(CharacterCodeConverter.decodeEbcdic(Arrays.copyOfRange(bytes, 204, 208))); sendMapVectorModel.setScAlarmIndic(CharacterCodeConverter.decodeEbcdic(Arrays.copyOfRange(bytes, 208, 212))); sendMapVectorModel.setScFrsetIndic(CharacterCodeConverter.decodeEbcdic(Arrays.copyOfRange(bytes, 212, 216))); sendMapVectorModel.setScLastIndic(CharacterCodeConverter.decodeEbcdic(Arrays.copyOfRange(bytes, 216, 220))); sendMapVectorModel.setScWaitIndic(CharacterCodeConverter.decodeEbcdic(Arrays.copyOfRange(bytes, 220, 224))); sendMapVectorModel.setScCursor(CharacterCodeConverter.decodeHexStringData(Arrays.copyOfRange(bytes, 224, 228)).replaceAll("FFFFFFFF", "-1")); sendMapVectorModel.setScMsrData(CharacterCodeConverter.decodeHexStringData(Arrays.copyOfRange(bytes, 228, 232))); sendMapVectorModel.setSmMapset(CharacterCodeConverter.decodeEbcdic(Arrays.copyOfRange(bytes, 232, 240))); sendMapVectorModel.setSmMap(CharacterCodeConverter.decodeEbcdic(Arrays.copyOfRange(bytes, 240, 248))); sendMapVectorModel.setSmDataIndic(CharacterCodeConverter.decodeEbcdic(Arrays.copyOfRange(bytes, 248, 252))); sendMapVectorModel.setSmDataLen(String.valueOf(Long.parseLong(CharacterCodeConverter.decodeHexStringData(Arrays.copyOfRange(bytes, 252, 256)), 16))); sendMapVectorModel.setSmDataOffset(String.valueOf(Long.parseLong(CharacterCodeConverter.decodeHexStringData(Arrays.copyOfRange(bytes, 256, 260)), 16))); sendMapVectorModel.setSmAdsdLen(String.valueOf(Long.parseLong(CharacterCodeConverter.decodeHexStringData(Arrays.copyOfRange(bytes, 260, 264)), 16))); sendMapVectorModel.setSmAdsdOffset(String.valueOf(Long.parseLong(CharacterCodeConverter.decodeHexStringData(Arrays.copyOfRange(bytes, 264, 268)), 16))); response.getReceiveMessage().setSendMapVector(sendMapVectorModel); // Ads Data response.getReceiveMessage().setAds(CharacterCodeConverter.decodeAdsData(transactionId, Arrays.copyOfRange(bytes, 268, bytes.length)).replaceAll("\\u0000", "")); return response; } public String getQueue() { return queue; } public void setQueue(String que) { queue = que; } }