package com.pgf.mqspring.service.impl; import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.Date; 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 receive() { try { return jmsTemplate.receiveAndConvert(queue).toString(); } catch (JmsException ex) { ex.printStackTrace(); return "FAIL"; } } public String receive(String id) { try { StringBuilder sb = new StringBuilder("id = '"); sb.append(id); sb.append("'"); return jmsTemplate.receiveSelected(queue, sb.toString()).toString(); } catch (JmsException ex) { ex.printStackTrace(); return "FAIL"; } } public String receiveCics(String id) { try { StringBuilder sb = new StringBuilder("JMSCorrelationID = '"); sb.append(id); sb.append("'"); /* String decStr = jmsTemplate.browseSelected(queue, sb.toString(), new BrowserCallback() { @SuppressWarnings("unchecked") public String doInJms(Session s, QueueBrowser qb) throws JMSException { String retStr = ""; Enumeration e = qb.getEnumeration(); while (e.hasMoreElements()) { final Message m = e.nextElement(); if (id.equals(m.getJMSCorrelationID())) { byte[] bytes = m.getBody(byte[].class); retStr = commonService.decodeEbcdic(bytes); break; } } return retStr; } }); */ //Thread.sleep(2000); BytesMessage bm = (BytesMessage) jmsTemplate.receiveSelected(queue, sb.toString()); byte[] bytes = bm.getBody(byte[].class); logger.info("Reply Message Length=" + bytes.length); //byte[] msg = Arrays.copyOfRange(bytes, 180, 228); //String str = commonService.decodeEbcdic(msg); //logger.info(str); return bm.toString(); } catch (JmsException | JMSException ex) { ex.printStackTrace(); return "FAIL"; } } public String receive4Stub(String id) { try { logger.info("REST_API_SESSION_ID =" + id); StringBuilder sb = new StringBuilder("REST_API_SESSION_ID = '"); sb.append(id); sb.append("'"); BytesMessage bm = (BytesMessage) jmsTemplate.receive(queue); byte[] bytes = bm.getBody(byte[].class); byte[] msg = Arrays.copyOfRange(bytes, 180, 228); String str = CharacterCodeConverter.decodeEbcdic(msg); logger.info(str); return bm.toString(); } catch (JmsException | JMSException ex) { ex.printStackTrace(); return "FAIL"; } } public String receiveMessage(String id) { try { logger.info("Message ID for Receive =" + id); StringBuilder sb = new StringBuilder("CorrelationID = '"); sb.append(id); sb.append("'"); System.out.println("timeout start:" + new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS").format(new Date())); BytesMessage bm = (BytesMessage) jmsTemplate.receiveSelected(queue, sb.toString()); System.out.println("timeout end:" + new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS").format(new Date())); byte[] bytes = bm.getBody(byte[].class); // String body = "c3c9c84000000002000000b40000000000000000c3e2d8c3c2c4c3d600000000000000000000000000000000000001110002bf2000000002000001d4000000b4000000000000000000010000016800020000000200000000404040405c5c5c5c5c5c5c5c40404040404040404040404040404040c3c9f1f840404040c7c5f0f240404040404040404040404040404040c7c5f0f240404040404040404040404040404040000000000000000000000003000000000000011ef1f8f0f4d6404040f0f0f0f0c5404040d5404040e8404040d5404040d5404040d5404040d5404040ffffffff00000000d4c7c5f0f2f0f040d4c7c5f0f2f0f040d5404040000000c600000058000000000000011e000000000000000000000000000000000040404040404040400000000000f1f061f1f761f2f200000000000000000000000000004040404040404040404040404040404040404040404040404040404040404040404040404040404040400000000000f1f07af5f97af5f2ffffc100004040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; // byte[] bytes = DatatypeConverter.parseHexBinary(body); 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; } }