gpt4 book ai didi

java - 在 Java REST 服务中浏览 IBM MQ 时出现 ResourceException

转载 作者:行者123 更新时间:2023-11-28 23:15:22 25 4
gpt4 key购买 nike

我编写了连接到 IBM MQ 并浏览队列的代码。该程序在独立运行时运行良好,但在基于 Spring 的 Rest API(使用 tomcat)中运行时失败并出现 ResourceException

我正在使用以下库:

com.ibm.mq.allclient-9.0.5.0.jar
com.ibm.mq.jar
javax.jms-api-2.0.1.jar

程序:如下:

import java.util.Enumeration;

import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueBrowser;
import javax.jms.QueueConnection;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.springframework.stereotype.Component;

import com.ibm.mq.jms.MQQueueConnection;
import com.ibm.mq.jms.MQQueueConnectionFactory;
import com.ibm.msg.client.wmq.WMQConstants;

@Component
public class TestMessage {

MQQueueConnection mqQueueConnection = null;

public static void main(String[] args) {
try {

TestMessage messageUtil = new TestMessage();
messageUtil.connect();
System.out.println(messageUtil.messageExists("queuename", "searchvalue"));

} catch (Exception e) {
e.printStackTrace();
}

}

public final String className = "TestMessage";

public QueueConnection connect() {
String methodName = "connect";

MQQueueConnection connection = null;

try {

String hostname = "xxxxxx";//Replace with correct value
String channel = "xxxxx";//Replace with correct value
String queueManager = "xxxx";//Replace with correct value
Integer port = 0;//Replace with correct value

MQQueueConnectionFactory connectionFactory = new MQQueueConnectionFactory();
connectionFactory.setHostName(hostname);
connectionFactory.setPort(port);
connectionFactory.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT);
connectionFactory.setQueueManager(queueManager);
connectionFactory.setChannel(channel);

connection = (MQQueueConnection) connectionFactory.createQueueConnection();
connection.start();
mqQueueConnection = connection;

} catch (JMSException jmse) {
jmse.printStackTrace();
}
return connection;

}

public boolean messageExists(String queueName, String searchValue) {

boolean returnValue = false;

QueueConnection connection = mqQueueConnection;
Session session = null;
QueueBrowser browser = null;
int i = 0;
try {

session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);

Queue queue = session.createQueue(queueName);
browser = session.createBrowser(queue);
Enumeration<TextMessage> en = browser.getEnumeration();
while (en.hasMoreElements()) {
TextMessage message = (TextMessage) en.nextElement();
i++;
if (message.getText().contains(searchValue)) {
System.out.println(message);
System.out.println(message.getText());
returnValue = true;
break;
}
}
System.out.println("finished");
}
catch (Exception jmse) {
jmse.printStackTrace();
} catch (Error jmse) {
jmse.printStackTrace();
} finally {
try {
if (browser != null) {
browser.close();
}
if (session != null) {
session.close();
}
if (connection != null) {
connection.close();
}
} catch (Exception jmse) {
jmse.printStackTrace();
}
}

return returnValue;
}
}

错误跟踪是:

java.lang.NoClassDefFoundError: javax/resource/ResourceException
at com.ibm.msg.client.wmq.common.internal.Reason.createException(Reason.java:203)
at com.ibm.msg.client.wmq.internal.WMQMessageConsumer.checkJmqiCallSuccess(WMQMessageConsumer.java:222)
at com.ibm.msg.client.wmq.internal.WMQMessageConsumer.checkJmqiCallSuccess(WMQMessageConsumer.java:156)
at com.ibm.msg.client.wmq.internal.WMQConsumerShadow.getMsg(WMQConsumerShadow.java:1861)
at com.ibm.msg.client.wmq.internal.WMQSyncConsumerShadow.receiveInternal(WMQSyncConsumerShadow.java:230)
at com.ibm.msg.client.wmq.internal.WMQConsumerShadow.receive(WMQConsumerShadow.java:1466)
at com.ibm.msg.client.wmq.internal.WMQQueueEnumeration.retrieveMessage(WMQQueueEnumeration.java:370)
at com.ibm.msg.client.wmq.internal.WMQQueueEnumeration.hasMoreElements(WMQQueueEnumeration.java:291)
at com.ibm.msg.client.jms.internal.JmsQueueBrowserImpl$JmsEnumImpl.hasMoreElements(JmsQueueBrowserImpl.java:362)
at com.ibm.mq.jms.MQQueueEnumeration.hasMoreElements(MQQueueEnumeration.java:91)
at xxxxxxxxxxxxxxxxxxxx.TestMessage.messageExists(TestMessage.java:89)
at xxxxxxxxxxxxxxxxxxxx.TestMessage.main(TestMessage.java:28)
Caused by: java.lang.ClassNotFoundException: javax.resource.ResourceException
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 12 more

有人可以帮我找出问题所在吗?我正在使用 Spring 4、Gradle、tomcat、Spring boot

下面是gradle依赖:

compile('org.springframework.boot:spring-boot-starter-web') 
compile('org.springframework.boot:spring-boot-starter-aop')
compile('org.springframework:spring-aspects')
compile("org.springframework.boot:spring-boot-starter-data-mongodb")

compile group: 'commons-io', name: 'commons-io', version: '2.5'
compile group: 'org.apache.commons', name: 'commons-vfs2', version: '2.1'
compile group: 'de.odysseus.staxon', name: 'staxon', version: '1.3'
compile group: 'net.sourceforge.jtds', name: 'jtds', version: '1.3.1'

compile fileTree(dir: "lib", includes: ['*.jar'])

compile('org.springframework.boot:spring-boot-starter-test')
compile group: 'commons-dbcp', name: 'commons-dbcp', version: '1.4'
compile 'org.hibernate:hibernate-core:5.2.4.Final'

compile (group: 'jcifs', name: 'jcifs', version: '1.3.17'){
exclude group: 'javax.servlet', module: 'servlet-api'
}
compile group: 'net.sf.ehcache', name: 'ehcache', version: '2.10.2.2.21'
compile group: 'org.apache.commons', name: 'commons-pool2', version: '2.4.2'
compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.3.4'
compile 'org.hibernate:hibernate-core:5.2.4.Final'
compile group: 'org.hibernate', name: 'hibernate-c3p0', version: '5.2.4.Final'
compile group: 'com.thoughtworks.xstream', name: 'xstream', version: '1.4.10'

testCompile group: 'com.mockrunner', name: 'mockrunner-jdbc', version: '1.1.2'
testCompile("junit:junit")
testCompile group: 'org.powermock', name: 'powermock-module-junit4', version: '1.6.4'
testCompile group: 'org.powermock', name: 'powermock-api-mockito', version: '1.6.4'

最佳答案

我通过添加以下内容解决了这个问题。基本上异常是由于 ibm mq 相关的 jar 冲突。在下面添加解决了它。

    compile("com.ibm.mq:mq-jms-spring-boot-starter:+")

关于java - 在 Java REST 服务中浏览 IBM MQ 时出现 ResourceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50533010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com