1.参数集成对象
EdiXigemaMqConfig.java
public class EdiImbMqConfig {
private String name;
private String host;
private Integer port;
private String queueName;
private String channel;
private String qmgr;
private String userName;
private String userPwd;
private String applicationName;
private boolean checkPwd;
private String timeout;
private boolean isAutoAck;
}
2.连接配置
package com.framework.jms.xigema;
import com.framework.core.exception.ErrorCode;
import com.framework.core.exception.ServerException;
import com.ibm.mq.jms.MQDestination;
import com.ibm.msg.client.jms.JmsConnectionFactory;
import com.ibm.msg.client.jms.JmsFactoryFactory;
import com.ibm.msg.client.wmq.WMQConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import javax.jms.Destination;
import javax.jms.JMSContext;
import javax.jms.JMSException;
import static com.framework.core.exception.ErrorCode.MQ_NAME_ERROR;
import static com.framework.jms.common.Constant.TRANSACTED;
/**
* <p> Description : xigema连接配置类 </p >
* <p> Author : kz </p >
* <p> Create Time : 2023/2/22 17:00 </p>
* <p> Author Email: <a>kz</a> </p>
*/
@Slf4j
public class EdiXigemaConnection {
// Create variables for the connection to MQ
/**
* queueName
*/
private String queueName = null;
/**
* cipherSuite
*/
private String cipherSuite = null;
JMSContext context;
public EdiXigemaConnection(EdiXigemaMqConfig mqConfigEntity, String id) {
log.info("Get application is starting");
mqConnectionVariables(mqConfigEntity);
JmsConnectionFactory connectionFactory = createJmsConnectionFactory();
setJmsProperties(connectionFactory, mqConfigEntity, id);
log.info("created connection factory");
context = connectionFactory.createContext();
log.info("context created");
}
public EdiXigemaConnection(EdiXigemaMqConfig mqConfigEntity, String id, boolean isTransacted) {
log.info("Get application is starting");
mqConnectionVariables(mqConfigEntity);
JmsConnectionFactory connectionFactory = createJmsConnectionFactory();
setJmsProperties(connectionFactory, mqConfigEntity, id);
log.info("created connection factory");
if (isTransacted) {
context = connectionFactory.createContext(TRANSACTED);
} else {
context = connectionFactory.createContext();
}
log.info("transacted context created");
}
private void mqConnectionVariables(EdiXigemaMqConfig mqConfigEntity) {
if (StringUtils.isEmpty(mqConfigEntity.getQueueName())) {
throw new ServerException(MQ_NAME_ERROR);
}
queueName = mqConfigEntity.getQueueName();
}
public JMSContext getContext() {
return context;
}
public void closeContext() {
context.close();
context = null;
}
public Destination getDestination() {
return context.createQueue("queue:///" + queueName);
}
public void setTargetClient(Destination destination) {
try {
MQDestination mqDestination = (MQDestination) destination;
mqDestination.setTargetClient(WMQConstants.WMQ_CLIENT_NONJMS_MQ);
} catch (JMSException jmsex) {
log.warn("Unable to set target destination to non JMS");
}
}
private JmsConnectionFactory createJmsConnectionFactory() {
JmsFactoryFactory ff;
JmsConnectionFactory cf;
try {
ff = JmsFactoryFactory.getInstance(WMQConstants.WMQ_PROVIDER);
cf = ff.createConnectionFactory();
} catch (JMSException jmsex) {
recordFailure(jmsex);
cf = null;
}
return cf;
}
private void setJmsProperties(JmsConnectionFactory cf, EdiXigemaMqConfig mqConfigEntity, String id) {
try {
checkConfig(mqConfigEntity);
cf.setStringProperty(WMQConstants.WMQ_HOST_NAME, mqConfigEntity.getHost());
cf.setIntProperty(WMQConstants.WMQ_PORT, mqConfigEntity.getPort());
cf.setStringProperty(WMQConstants.WMQ_CHANNEL, mqConfigEntity.getChannel());
cf.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT);
cf.setStringProperty(WMQConstants.WMQ_QUEUE_MANAGER, mqConfigEntity.getQmgr());
cf.setStringProperty(WMQConstants.WMQ_APPLICATIONNAME, id);
cf.setBooleanProperty(WMQConstants.USER_AUTHENTICATION_MQCSP, true);
cf.setStringProperty(WMQConstants.USERID, mqConfigEntity.getUserName());
cf.setStringProperty(WMQConstants.PASSWORD, mqConfigEntity.getUserPwd());
if (cipherSuite != null && !cipherSuite.isEmpty()) {
cf.setStringProperty(WMQConstants.WMQ_SSL_CIPHER_SUITE, cipherSuite);
}
} catch (JMSException jmsex) {
recordFailure(jmsex);
}
return;
}
private void checkConfig(EdiXigemaMqConfig mqConfigEntity) {
if (StringUtils.isEmpty(mqConfigEntity.getHost())) {
throw new ServerException(ErrorCode.HOST_ERROR);
}
if (mqConfigEntity.getPort() == null) {
throw new ServerException(ErrorCode.PORT_ERROR);
}
if (StringUtils.isEmpty(mqConfigEntity.getChannel())) {
throw new ServerException(ErrorCode.CHANNEL_ERROR);
}
if (StringUtils.isEmpty(mqConfigEntity.getQmgr())) {
throw new ServerException(ErrorCode.QMGR_ERROR);
}
if (mqConfigEntity.isCheckPwd()) {
if (StringUtils.isEmpty(mqConfigEntity.getUserName())) {
throw new ServerException(ErrorCode.USER_NAME_ERROR);
}
if (StringUtils.isEmpty(mqConfigEntity.getUserPwd())) {
throw new ServerException(ErrorCode.USER_PWD_ERROR);
}
}
}
public static void recordFailure(Exception ex) {
if (ex != null) {
if (ex instanceof JMSException) {
processJmsException((JMSException) ex);
} else {
log.warn(ex.getMessage());
}
}
log.error("FAILURE");
return;
}
private static void processJmsException(JMSException jmsex) {
log.info(jmsex.getMessage());
Throwable innerException = jmsex.getLinkedException();
log.info("Exception is: " + jmsex);
if (innerException != null) {
log.info("Inner exception(s):");
}
while (innerException != null) {
log.warn(innerException.getMessage());
innerException = innerException.getCause();
}
return;
}
}
3.创建消费者
package com.framework.jms.xigema;
import lombok.extern.slf4j.Slf4j;
import javax.jms.*;
import java.util.Objects;
/**
* <p> Description : 消费者基础类 </p >
* <p> Author : kz </p >
* <p> Create Time : 2023/2/24 17:00 </p >
* <p> Author Email: < a href=" ">kz</ a> </p >
*/
@Slf4j
public class EdiXigemaConsumer {
public static final String CONSUMER_GET = "queue";
private JMSContext context = null;
private Destination destination = null;
private JMSConsumer consumer = null;
private EdiXigemaConnection ch = null;
public EdiXigemaConsumer(EdiXigemaMqConfig mqConfigEntity) {
String id = "Basic Get";
log.info("Sub application is starting");
ch = new EdiXigemaConnection(mqConfigEntity, id);
log.info("created connection factory");
context = ch.getContext();
log.info("context created");
destination = ch.getDestination();
log.info("destination created");
consumer = context.createConsumer(destination);
log.info("consumer created");
}
public EdiXigemaConsumer(EdiXigemaMqConfig mqConfigEntity, String debugFlowKey) {
ch = new EdiXigemaConnection(mqConfigEntity, debugFlowKey, false);
log.info("created connection factory");
context = ch.getContext();
log.info("context created");
destination = ch.getDestination();
consumer = context.createConsumer(destination);
log.info("consumer created");
}
/**
* @return: java.lang.String
* @author: kz
* @date: 2023/4/10 17:06
* @description: 接收消息
*/
public String receive() {
boolean continueProcessing = true;
while (continueProcessing) {
try {
Message receivedMessage = consumer.receive();
if (receivedMessage == null) {
log.info("No message received from this endpoint");
continueProcessing = false;
} else {
return new EdiXigemaConsumerHelper().getStringMessage(receivedMessage);
}
} catch (JMSRuntimeException jmsex) {
log.error("receiveMsg error", jmsex);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
return null;
}
}
return null;
}
/**
* @return: void
* @author: kz
* @date: 2023/4/10 17:05
* @description: 关闭连接
*/
public void close() {
if(Objects.isNull(consumer)){
return;
}
consumer.close();
ch.closeContext();
consumer = null;
ch = null;
}
/**
* @param i
* @return: java.lang.String
* @author: kz
* @date: 2023/4/10 17:05
* @description: 限时接收消息
*/
public String receiveWithTimeOut(int i) {
Message receivedMessage = consumer.receive(i);
if (receivedMessage == null) {
log.info("No message received from this endpoint");
} else {
return new EdiXigemaConsumerHelper().getStringMessage(receivedMessage);
}
return null;
}
}
4.创建消费者消息解析辅助类
package com.framework.jms.xigema;
import lombok.extern.slf4j.Slf4j;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.TextMessage;
/**
* <p> Description : 消费者消息解析辅助类 </p >
* <p> Author : kz </p >
* <p> Create Time : 2023/2/24 17:00 </p >
* <p> Author Email: < a href=" ">kz</ a> </p >
*/
@Slf4j
public class EdiXigemaConsumerHelper {
public String getStringMessage(Message receivedMessage) {
if (receivedMessage instanceof TextMessage) {
TextMessage textMessage = (TextMessage) receivedMessage;
try {
log.debug("Received message: {}", textMessage.getText());
return textMessage.getText();
} catch (JMSException jmsex) {
recordFailure(jmsex);
}
} else if (receivedMessage instanceof Message) {
log.warn("Message received was not of type TextMessage.");
} else {
log.error("Received object not of JMS Message type!");
}
return null;
}
private void recordFailure(Exception ex) {
if (ex != null) {
if (ex instanceof JMSException) {
processJmsException((JMSException) ex);
} else {
log.warn(ex.getMessage());
}
}
log.info("FAILURE");
return;
}
private void processJmsException(JMSException jmsex) {
log.warn(jmsex.getMessage());
Throwable innerException = jmsex.getLinkedException();
log.warn("Exception is: " + jmsex);
if (innerException != null) {
log.warn("Inner exception(s):");
}
while (innerException != null) {
log.warn(innerException.getMessage());
innerException = innerException.getCause();
}
return;
}
}
5.创建生产者
package com.framework.jms.xigema;
import lombok.extern.slf4j.Slf4j;
import javax.jms.Destination;
import javax.jms.JMSContext;
import javax.jms.JMSProducer;
import javax.jms.JMSRuntimeException;
import java.util.Objects;
/**
* <p> Description : 生产者基础对象 </p >
* <p> Author : kz </p >
* <p> Create Time : 2023/2/24 17:00 </p >
* <p> Author Email: < a href=" ">kz</ a> </p >
*/
@Slf4j
public class EdiXigemaProducer {
public static final String PRODUCER_PUT = "queue";
private JMSContext context = null;
private Destination destination = null;
private JMSProducer producer = null;
private EdiXigemaConnection ch = null;
public EdiXigemaProducer(EdiXigemaMqConfig mqConfigEntity) {
String id = "Basic put";
log.info("Sub application is starting");
ch = new EdiXigemaConnection(mqConfigEntity, id);
log.info("created connection factory");
context = ch.getContext();
log.info("context created");
destination = ch.getDestination();
// Set so no JMS headers are sent.
ch.setTargetClient(destination);
log.info("destination created");
producer = context.createProducer();
}
/**
* @param message
* @return: void
* @author: kz
* @date: 2023/4/10 16:35
* @description: 发送消息
*/
public void send(String message) {
log.debug("Publishing messages.\n");
try {
producer.send(destination, message);
} catch (JMSRuntimeException jmsex) {
log.error("运行异常", jmsex);
} catch (Exception e) {
log.error("程序异常", e);
}
}
/**
* @return: void
* @author: kz
* @date: 2023/4/10 16:35
* @description: 关闭连接
*/
public void close() {
if(Objects.isNull(ch)){
return;
}
ch.closeContext();
ch = null;
}
}
创建完成后,可直接通过new EdiXigemaProducer 和 EdiXigemaConsumer完成实例化,调用send、receive方法发送和接收消息。