ResourceException при просмотре IBM MQ в службе Java REST - PullRequest
0 голосов
/ 25 мая 2018

Я написал код, который подключается к IBM MQ и просматривает очередь.Программа работает нормально при запуске в автономном режиме, но не работает с ResourceException при запуске в Spring Rest API (с tomcat)

Я использую следующие библиотеки:

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'

1 Ответ

0 голосов
/ 03 июля 2018

Я решил проблему, добавив ниже.В основном, исключение происходило из-за того, что jb, связанный с IBM, был конфликтующим.Добавление ниже разрешило это.

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