我有一个Spring Boot应用程序,它使用ibm mq从另一个应用程序发送和接收消息。我编写了一个简单的代码,用于使用javax.jms库发送和接收消息。在xldeploy_dictionaries.json文件中提供了mq的所有详细信息
代码如下:
为pom中的mq添加了依赖性:
<dependency>
<groupId>com.ibm.websphere</groupId>
<artifactId>com.ibm.mqjms</artifactId>
<version>7.5.0.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.ibm.websphere.j2ee</groupId>
<artifactId>j2ee</artifactId>
<version>8.5.5.3</version>
</dependency>
用于发送和接收消息的单个类:
@Service
@Data
@Slf4j
public class MqUtil {
private Response response;
private String message;
private String sendRequest(String xmlContent, Response response) throws JMSException, CusTimeoutException {
@Cleanup
MessageProducer sender = null;
@Cleanup
QueueSession session = null;
@Cleanup
QueueConnection connection = null;
try {
InitialContext ctx = new InitialContext();
QueueConnectionFactory cf = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
connection = cf.createQueueConnection();
log.info(SENDING_MESSAGE_TO_MQ);
session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue(CONST_SEND_QUEUENAME);
sender = session.createProducer(queue);
TextMessage txtMessage = session.createTextMessage(xmlContent);
txtMessage.setJMSType("XML");
connection.start();
sender.send(txtMessage);
log.info("Sent to MQ {}", response.getTraceId());
String m= receiveRequestFromACBS(response, session);
connection.close();
return m;
} catch (CusTimeoutException e) {
log.error("Error occurred Timeout in queue {}", e.getMessage());
throw new CusTimeoutException(); //custom exception
} catch (NamingException e) {
log.error("Error occurred sending request to Message Queue {}" + e);
throw new CusTimeoutException();
}
}
private String receiveRequest(Response response, QueueSession session)
throws JMSException, ARATimeoutException {
@Cleanup
MessageConsumer receiver = null;
try {
this.setResponse(response);
log.info("Receiving Message from MQ");
Queue queueReceiver = session.createQueue(CONST_REC_QUEUENAME);
receiver = session.createConsumer(queueReceiver);
log.info("initializing message listener");
receiver.setMessageListener(new TextMessageListener());
log.info("back to receiveRequestFromACBS");
return consumerResponse(response); //other method
} catch (JMSException | SAXException | ParserConfigurationException | JAXBException | IOException e) {
log.error("Error occurred while receiving request from Message Queue {}", e);
throw new CUSTimeoutException();
}
}
private String consumerResponse(Response response)
throws IOException, JAXBException, ParserConfigurationException, SAXException, JMSException, CUSTimeoutException {
String replyString = this.getMessage();
---business logic---
}
消息侦听器类:
@Slf4j
public class TextMessageListener implements MessageListener {
MqUtil MqUtil= new MqUtil();
@Override
public void onMessage(Message messageReceived) {
log.info("inside message listener");
String replyString = "";
try {
if (messageReceived instanceof TextMessage) {
log.info("Message type is text");
replyString = ((TextMessage) messageReceived).getText();
xmlUtil.setMessage(replyString);
} else {
log.error("Invalid Message Type Received {}", messageReceived.getJMSType() + messageReceived.toString());
}
} catch (JMSException e) {
log.error("error while receiving the messages {}", e.getMessage());
}
}
}
此代码出现以下错误- javax.jms.IllegalStateException:不允许使用方法setMessageListener。
我了解我们不能将消息侦听器用于j2ee应用程序,任何人都可以建议其他任何异步接收方法。我已经读过我们可以将消息驱动的Bean用于mq,但是我不知道如何开始。我试图在Internet上找到它,但没有得到任何完整的示例。我需要帮助来逐步了解以springboot方式或正常方式发送和接收消息的过程,以供我使用。