package com.pgf.mqspring.service.impl; import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.io.IOException; import javax.jms.BytesMessage; import javax.jms.CompletionListener; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.JMSProducer; import javax.jms.Message; import javax.jms.Session; import javax.jms.TextMessage; import javax.xml.bind.DatatypeConverter; 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.jms.core.MessageCreator; import org.springframework.stereotype.Service; import com.ibm.mq.constants.CMQC; import com.ibm.mq.headers.MQCIH; import com.pgf.mqspring.model.ReceiveMapVectorRequestModel; import com.pgf.mqspring.model.ReplyRequestModel; import com.pgf.mqspring.model.TestModel; import com.pgf.mqspring.service.MqSendService; import com.pgf.mqspring.util.CharacterCodeConverter; @Service public class MqSendServiceImpl implements MqSendService { private JmsTemplate jmsTemplate = null; private String queue = ""; private String messageId = ""; public MqSendServiceImpl(JmsTemplate template) { this.jmsTemplate = template; } public String send(String msg) { try { jmsTemplate.convertAndSend(queue, msg); return "OK"; } catch (JmsException ex) { ex.printStackTrace(); return "FAIL"; } } public String send(TestModel model) { try { jmsTemplate.send(queue, new MessageCreator() { public Message createMessage(Session session) throws JMSException { TextMessage message = session.createTextMessage(model.toString()); message.setJMSReplyTo(session.createQueue("QL.POC01.REPLY")); message.setStringProperty("id", model.getId()); return message; } }); // jmsTemplate.convertAndSend(QUE, model); return "OK"; } catch (JmsException ex) { ex.printStackTrace(); return "FAIL"; } } public String sendCics(TestModel model) { final String facility = "ASDFGHJKLOIUYTREWQZXCVBNM"; final String transactionId = "TEST202209061548337"; Logger logger = LogManager.getLogger(); try { jmsTemplate.send(queue, new MessageCreator() { public Message createMessage(Session session) throws JMSException { String str = "TESTテスト送信1234567890"; byte encodeMsg[] = CharacterCodeConverter.encodeEbcdic(str); // DataInput in = new DataInputStream (new ByteArrayInputStream (str.getBytes())); MQCIH cih = new MQCIH(); //cih.setFormat(CMQC.MQFMT_CICS); cih.setUOWControl(273); cih.setGetWaitInterval(180000); cih.setLinkType(2); cih.setFacilityKeepTime(180); cih.setConversationalTask(1); cih.setFacility(facility.getBytes()); cih.setTransactionId(transactionId); ByteArrayOutputStream out = new ByteArrayOutputStream(); try { cih.write(new DataOutputStream(out), CMQC.MQENC_NATIVE, 932); } catch (IOException e) { e.printStackTrace(); } byte[] header = out.toByteArray(); BytesMessage bm = session.createBytesMessage(); bm.writeBytes(header); bm.writeBytes(encodeMsg); //bm.writeBytes(str.getBytes(StandardCharsets.UTF_8)); bm.setStringProperty("JMS_IBM_Format", CMQC.MQFMT_CICS); bm.setIntProperty("JMS_IBM_Encoding", CMQC.MQENC_NATIVE); bm.setIntProperty("JMS_IBM_Character_Set", 932); bm.setJMSCorrelationID(model.getId()); logger.info("Send Message ID =" + model.getId()); return bm; } }); return "OK"; } catch (Exception ex) { ex.printStackTrace(); return "FAIL"; } } public String send4Stub(TestModel model) { final String mqmd = "4d442020000000020000000000000001ffffffff0000000000000111000003af4d51434943532020ffffffff000000003040f01d3230323230393132303933323436383831313739414d51214e45575f53455353494f4e5f434f5252454c494400000000514c2e4d444830312e5245504c59202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020202020204745545320202020202020200000000000000000000000000000000000000000000000000000000000000000202020202020202020202020202020202020202020202020202020202020202000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000ffffffff"; final String mqcih = "4349482000000002000000b40000000000000000202020202020202000000000000000000000000000000000000001110002bf2000000002ffffffff000000b40000000000000000000000000000000000000000202020202020202047455453333333332020202020202020202020202020202020202020202020204745303120202020202020202020202020202020202020202020202020202020202020202020202000000000000000000000000000000000"; Logger logger = LogManager.getLogger(); logger.info("MQMD length=" + mqmd.length()); logger.info("MQCIH length=" + mqcih.length()); // CommonService commonService = new CommonServiceImpl(); try { jmsTemplate.send(queue, new MessageCreator() { public Message createMessage(Session session) throws JMSException { byte[] bytes = DatatypeConverter.parseHexBinary(mqmd + mqcih); //MQMessage mqm = new MQMessage(); BytesMessage bm = session.createBytesMessage(); bm.writeBytes(bytes); bm.setStringProperty("JMS_IBM_Format", CMQC.MQFMT_CICS); bm.setIntProperty("JMS_IBM_Encoding", CMQC.MQENC_NATIVE); bm.setIntProperty("JMS_IBM_Character_Set", 943); bm.setStringProperty("REST_API_SESSION_ID", model.getId()); return bm; } }); return "OK"; } catch (Exception ex) { ex.printStackTrace(); return "FAIL"; } } public String sendRawData(TestModel model) { //final String msgId = "20220912093247445184"; final String correId = "AMQ!NEW_SESSION_CORRELID"; Logger logger = LogManager.getLogger(); messageId = ""; logger.info("MessageID Cleared"); try { JMSProducer jmsProducer = jmsTemplate.getConnectionFactory().createContext().createProducer(); jmsProducer.setAsync(new CompletionListener() { @Override public void onCompletion(Message msg) { try { messageId = msg.getJMSMessageID(); } catch (JMSException e) { e.printStackTrace(); } System.out.println(messageId); } @Override public void onException(Message msg, Exception e) { System.out.println(e.getMessage()); } }); Destination dest = jmsTemplate.getConnectionFactory().createContext().createQueue(queue); Destination reply = jmsTemplate.getConnectionFactory().createContext().createQueue("QL.MDH01.REPLY"); BytesMessage bm = jmsTemplate.getConnectionFactory().createContext().createBytesMessage(); bm.writeBytes(createRawDataMessage(model)); bm.setStringProperty("JMS_IBM_Format", CMQC.MQFMT_CICS); bm.setIntProperty("JMS_IBM_Encoding", CMQC.MQENC_NATIVE); bm.setIntProperty("JMS_IBM_Character_Set", 932); //bm.setJMSMessageID(msgId); bm.setJMSCorrelationID(correId); bm.setJMSReplyTo(reply); bm.setStringProperty("JMSXUserID","GETS"); jmsProducer.send(dest, bm); return "200"; } catch (Exception ex) { ex.printStackTrace(); return "500"; } } public String sendMqMessage(ReplyRequestModel model) { Logger logger = LogManager.getLogger(); logger.info("sendReqestMessage start"); String mqmd = model.getMqmd().toString(); ReceiveMapVectorRequestModel recMapVecModel = model.getSendMessage().getReceiveMapVector(); String ads = model.getSendMessage().getAds(); String transactionId = model.getMqcih().getTransactionId(); MQCIH cih = new MQCIH(); cih.setUOWControl(Integer.parseInt(model.getMqcih().getUowControl())); cih.setGetWaitInterval(Integer.parseInt(model.getMqcih().getGetWaitInterval())); cih.setLinkType(Integer.parseInt(model.getMqcih().getLinkType())); cih.setFacilityKeepTime(Integer.parseInt(model.getMqcih().getFacilityKeepTime())); cih.setConversationalTask(Integer.parseInt(model.getMqcih().getConversationalTask())); cih.setFacility(DatatypeConverter.parseHexBinary(model.getMqcih().getFacility())); cih.setAuthenticator(model.getMqcih().getAutherticator()); cih.setTransactionId(transactionId); ByteArrayOutputStream out = new ByteArrayOutputStream(); try { cih.write(new DataOutputStream(out), CMQC.MQENC_NATIVE, 932); } catch (IOException e) { e.printStackTrace(); } byte[] header = out.toByteArray(); byte[] convRecMapVec = null; byte[] convAds = null; if (recMapVecModel != null) { convRecMapVec = CharacterCodeConverter.encodeVectorData(transactionId, recMapVecModel, 48); } if (!ads.isEmpty()) { try { convAds = CharacterCodeConverter.encodeAdsData(transactionId, ads); } catch (IOException e) { e.printStackTrace(); return "500"; } } try { JMSProducer jmsProducer = jmsTemplate.getConnectionFactory().createContext().createProducer(); jmsProducer.setAsync(new CompletionListener() { @Override public void onCompletion(Message msg) { try { messageId = msg.getJMSMessageID(); logger.info("Send Message ID=" + messageId); } catch (JMSException e) { e.printStackTrace(); } System.out.println(messageId); } @Override public void onException(Message msg, Exception e) { System.out.println(e.getMessage()); } }); Destination dest = jmsTemplate.getConnectionFactory().createContext().createQueue(queue); Destination reply = jmsTemplate.getConnectionFactory().createContext().createQueue("QL.MDH01.REPLY"); BytesMessage bm = jmsTemplate.getConnectionFactory().createContext().createBytesMessage(); //bm.writeBytes(mqmd.getBytes()); bm.writeBytes(header); if (convRecMapVec != null) { bm.writeBytes(convRecMapVec); } if (convAds != null) { bm.writeBytes(convAds); } bm.setStringProperty("JMS_IBM_Format", CMQC.MQFMT_CICS); bm.setIntProperty("JMS_IBM_Encoding", CMQC.MQENC_NATIVE); bm.setIntProperty("JMS_IBM_Character_Set", 943); bm.setJMSCorrelationID(model.getMqmd().getCorrelId()); bm.setJMSReplyTo(reply); bm.setStringProperty("JMSXUserID", "GETS"); jmsProducer.send(dest, bm); logger.info("sendReqestMessage end"); return "200"; } catch (Exception ex) { ex.printStackTrace(); return "500"; } } public String getQueue() { return queue; } public void setQueue(String que) { // example "queue:///QL.POC01.TEST2?targetClient=1" queue = "queue:///" + que + "?targetClient=1"; } public String getMessageId() { return messageId; } public void setMessageId(String messageId) { this.messageId = messageId; } private byte[] createRawDataMessage(TestModel model) { final String mqcih1 = "4349482000000002000000b40000000000000000202020202020202000000000000000000000000000000000000001110002bf2000000002ffffffff000000b4000000000000000000000000"+ model.getFacility()/*"0000000000000000"*/ + "202020202020202047455453333333332020202020202020202020202020202020202020202020204745303120202020202020202020202020202020202020202020202020202020202020202020202000000000000000000000000000000000"; final String mqcih2 = "4349482000000002000000b40000000000000000202020202020202000000000000000000000000000000000000001110002bf2000000002ffffffff000000b4000000000000000000000000"+ model.getFacility()/*"0138000100000001"*/ + "202020202020202047455453333333332020202020202020202020202020202020202020202020204745303120202020202020202020202020202020202020202020202020202020202020202020202000000000000000000000000000000000"; final String mqcih3 = "4349482000000002000000b40000000000000000202020202020202000000000000000000000000000000000000001110002bf2000000002ffffffff000000b4000000000000000000000000"+ model.getFacility()/*"0138000100000001"*/ + "202020202020202047455453333333332020202020202020202020202020202020202020202020204745303220202020202020202020202020202020202020202020202020202020202020202020202000000000000000000000000000000000"; final String mqcih4 = "4349482000000002000000b40000000000000000202020202020202000000000000000000000000000000000000001110002bf2000000002ffffffff000000b4000000000000000000000000"+ model.getFacility()/*"0138000100000001"*/ + "202020202020202047455453333333332020202020202020202020202020202020202020202020204745303220202020202020202020202020202020202020202020202020202020202020202020202000000000000000000000000000000000"; final String mqcih5 = "4349482000000002000000b40000000000000000202020202020202000000000000000000000000000000000000001110002bf2000000002ffffffff000000b4000000000000000000000000"+ model.getFacility()/*"0138000100000001"*/ + "20202020202020204745545333333333202020202020202020202020202020202020202020202020454d303420202020202020202020202020202020202020202020202020202020202020202020202000000000000000000000000000000000"; final String mqcih6 = "4349482000000002000000b40000000000000000202020202020202000000000000000000000000000000000000001110002bf2000000002ffffffff000000b4000000000000000000000000"+ model.getFacility()/*"0138000100000001"*/ + "20202020202020204745545333333333202020202020202020202020202020202020202020202020454d303420202020202020202020202020202020202020202020202020202020202020202020202000000000000000000000000000000000"; final String vecAds2 = "000000ecf1f8f0f2c9404040f0f0f0f0e8404040d4c7c5f0f1f0f040d4c7c5f0f1f0f0407d40404000000000000000bb0000000000000000000000000000cc0000d4c7c5f0f1f0f0400000cc0000" + model.getDate()/*"f0f961f1f261f2f2"*/ + "0000cc0000" + model.getTime()/*"f0f97af3f27af4f4"*/ + "0000cc0000c7c5e3e2404040400000cc0000c7c5e3e2f3f3f3f30000cc000040404040404040400000cc000040404040404040400000cc00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; final String vecAds4 = "000000f8f1f8f0f2c9404040f0f0f0f0e8404040d4c7c5f0f2f0f040d4c7c5f0f2f0f0407d40404000000000000000c60000000000000000000000000000cc000040404040404040400000cc0000" + model.getDate()/*"f0f961f1f261f2f2"*/ + "0000cc0000c5d4f0f40000cc00004040404040404040404040404040404040404040404040404040404040404040404040404040404040400000cc0000f0f97af3f27af4f40000cc000040400000cc0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"; final String vecAds6 = "000009ccf1f8f0f2c9404040f0f0f0f0e8404040d4c5d4f0f4f0f040d4c5d4f0f4f0f0407d404040000000000000099a0000000000000000000000000000cc0000c5d4f0f4404040400000cc000040404040404040400000cc000040404040404040404040400000cc000040404040404040400000cc000040404040404040404040400000cc000040404040404040404040400000cc0000f8f3f0f0f9f4f3f2f9f30000cc0000f2f0f2f3f0f3f1f60000cc000040404040404040400000cc0000400000cc0000400000cc0000404040404040400000cc00004040404040400000cc000040404040404040404040400000cc00004040404040404040404040400000cc000040404040400000cc000040400000cc000040404040404040404040400000cc000040404040404040404040400000cc000040404040404040404040404040404040404040404040404040404040404040400000cc00004040404040404040404040400000cc000040404040400000cc000040400000cc000040404040404040404040400000cc000040404040404040404040400000cc000040404040404040404040404040404040404040404040404040404040404040400000cc00004040404040404040404040400000cc000040404040400000cc000040400000cc000040404040404040404040400000cc000040404040404040404040400000cc000040404040404040404040404040404040404040404040404040404040404040400000cc00004040404040404040404040400000cc000040404040400000cc000040400000cc000040404040404040404040400000cc000040404040404040404040400000cc000040404040404040404040404040404040404040404040404040404040404040400000cc00004040404040404040404040400000cc000040404040400000cc000040400000cc000040404040404040404040400000cc000040404040404040404040400000cc000040404040404040404040404040404040404040404040404040404040404040400000cc00004040404040404040404040400000cc000040404040400000cc000040400000cc000040404040404040404040400000cc000040404040404040404040400000cc000040404040404040404040404040404040404040404040404040404040404040400000cc00004040404040404040404040400000cc000040404040400000cc000040400000cc000040404040404040404040400000cc000040404040404040404040400000cc000040404040404040404040404040404040404040404040404040404040404040400000cc00004040404040404040404040400000cc000040404040400000cc000040400000cc000040404040404040404040400000cc000040404040404040404040400000cc000040404040404040404040404040404040404040404040404040404040404040400000cc00004040404040404040404040400000cc000040404040400000cc000040400000cc000040404040404040404040400000cc000040404040404040404040400000cc000040404040404040404040404040404040404040404040404040404040404040400000cc00004040404040404040404040400000cc000040404040400000cc000040400000cc000040404040404040404040400000cc000040404040404040404040400000cc000040404040404040404040404040404040404040404040404040404040404040400000cc00004040404040404040404040400000cc000040404040400000cc000040400000cc000040404040404040404040400000cc000040404040404040404040400000cc000040404040404040404040404040404040404040404040404040404040404040400000cc00004040404040404040404040400000cc000040404040400000cc000040400000cc000040404040404040404040400000cc000040404040404040404040400000cc000040404040404040404040404040404040404040404040404040404040404040400000cc00004040404040404040404040400000cc000040404040400000cc000040400000cc000040404040404040404040400000cc000040404040404040404040400000cc000040404040404040404040404040404040404040404040404040404040404040400000cc00004040404040404040404040400000cc000040404040400000cc000040400000cc000040404040404040404040400000cc000040404040404040404040400000cc000040404040404040404040404040404040404040404040404040404040404040400000cc00004040404040404040404040400000cc000040404040400000cc000040400000cc000040404040404040404040400000cc000040404040404040404040400000cc000040404040404040404040404040404040404040404040404040404040404040400000cc00004040404040404040404040400000cc000040404040400000cc000040400000cc000040404040404040404040400000cc000040404040404040404040400000cc000040404040404040404040404040404040404040404040404040404040404040400000cc00004040404040404040404040400000cc000040404040400000cc000040400000cc000040404040404040404040400000cc000040404040404040404040400000cc000040404040404040404040404040404040404040404040404040404040404040400000cc00004040404040404040404040400000cc000040404040400000cc000040400000cc000040404040404040404040400000cc000040404040404040404040400000cc000040404040404040404040404040404040404040404040404040404040404040400000cc00004040404040404040404040400000cc000040404040400000cc000040400000cc000040404040404040404040400000cc000040404040404040404040400000cc000040404040404040404040404040404040404040404040404040404040404040400000cc00004040404040404040404040400000cc000040404040400000cc000040400000cc000040404040404040404040400000cc000040404040404040404040400000cc000040404040404040404040404040404040404040404040404040404040404040400000cc00004040404040404040404040400000cc000040404040404040404040400000cc000040404040404040404040400000cc000040404040404040400000cc00004040404040400000cc000040404040404040404040400000cc00004040404040404040404040400000cc00004040404040404040404040400000cc0000404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040404040400000"; Logger logger = LogManager.getLogger(); logger.info("Send Destination=" + queue); logger.info("msgKind = " + model.getMsgKind()); logger.info("facility = " + model.getFacility()); logger.info("date = " + model.getDate()); logger.info("time = " + model.getTime()); byte[] bytes = null; switch (model.getMsgKind()) { case "1": bytes = DatatypeConverter.parseHexBinary(mqcih1); break; case "2": bytes = DatatypeConverter.parseHexBinary(mqcih2 + vecAds2); break; case "3": bytes = DatatypeConverter.parseHexBinary(mqcih3); break; case "4": bytes = DatatypeConverter.parseHexBinary(mqcih4 + vecAds4); break; case "5": bytes = DatatypeConverter.parseHexBinary(mqcih5); break; case "6": bytes = DatatypeConverter.parseHexBinary(mqcih6 + vecAds6); break; default: bytes = DatatypeConverter.parseHexBinary(mqcih1); break; } return bytes; } }