bugfix> java > 投稿

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'

回答 1 件
  • 以下を追加して問題を解決しました。基本的に、IBM mq関連のjarが競合していたため、例外が発生していました。以下を追加すると解決しました。

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

あなたの答え