简体   繁体   中英

Unable to post message to IBM MQ Queue

I am trying to connect to IBM MQ and post message. getting below exception but the same code works in development environment.

org.springframework.jms.connection.SingleConnectionFactory.createSession(SingleConnectionFactory.java:437)
com.ibm.mq.jms.MQQueueConnection.createSession(MQQueueConnection.java:154) ~[com.ibm.mq.allclient-9.0.4.0.jar:9.0.4.0 - p904-L171030.1
com.ibm.mq.jms.MQQueueConnection.createQueueSession(MQQueueConnection.java:130) ~[com.ibm.mq.allclient-9.0.4.0.jar:9.0.4.0 - p904-L171030.1]
com.ibm.mq.jms.MQQueueSession.<init>(MQQueueSession.java:58) ~[com.ibm.mq.allclient-9.0.4.0.jar:9.0.4.0 - p904-L171030.1]
com.ibm.mq.jms.MQSession.<init>(MQSession.java:262) ~[com.ibm.mq.allclient-9.0.4.0.jar:9.0.4.0 - p904-L171030.1]\\n\
com.ibm.mq.jms.MQSession.getTransacted(MQSession.java:876) ~[com.ibm.mq.allclient-9.0.4.0.jar:9.0.4.0 - p904-L171030.1]
Caused by: java.lang.NullPointerException

Basically, the session is becoming null in other regions when we are trying to post a message from Spring JMS Template to IBM MQ.

from java,

    public void sendNotificationsMessageToQueue(String jsonMessage) 
    { 
    jmsTemplate(Queue, new MessageCreator(){ 
    @Override public Message createMessage(Session session) throws JMSException
    { 
    return session.createTextMessage(jsonMessage); 
    } }); 

From XML:

    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory" ref="ibmConnectionFactory" /> 
    <property name="defaultDestination" ref="exQueue" /> 
    </bean>

Read this as a comment as opposed to an answer. It's a long comment and needed a little formatting.

Your XML should also have a ibmConnectionFactory bean defined which will point at the IBM MQ Factory classes. What is in there is going to be important. You need to share that, as it looks like that that is where your deployed code is failing.

I am guessing that this is not a Spring-Boot project, and you are not making use of the mq-jms-spring-boot-starter.

If you were then that you are defining jmsTemplate and ibmConnectionFactory beans is a bit strange. If the IBM MQ classes are the only Messaging classes defined in your spring-boot project then spring will automatically bind the IBM MQ connection classes to the default jmsTemplate bean.

If you have other Messaging classes defined as dependencies in your project, then your beans are ignoring them, which indicates that they are not needed and hence shouldn't be there.

If, however, you are using Spring-Boot and mq-jms-spring-boot-starter, then I find it easier to define the beans in Java code than XML, although you only need to if you are changing the defaults.

import com.ibm.mq.jms.MQConnectionFactory;
import com.ibm.mq.samples.jms.spring.globals.handlers.OurDestinationResolver;
import com.ibm.mq.samples.jms.spring.globals.handlers.OurMessageConverter;
import com.ibm.mq.spring.boot.MQConfigurationProperties;
import com.ibm.mq.spring.boot.MQConnectionFactoryFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.core.JmsTemplate;


import javax.jms.JMSException;

@Configuration
public class MQConfiguration114 {
    protected final Log logger = LogFactory.getLog(getClass());

    @Bean
    public MQConnectionFactory mqConnectionFactory() throws JMSException {
        MQConfigurationProperties properties = new MQConfigurationProperties();
        // Properties will be a mix of defaults, and those found in application.properties
        // under ibm.mq
        // Here we can override any of the properties should we need to
        MQConnectionFactoryFactory mqcff = new MQConnectionFactoryFactory(properties,null);
        MQConnectionFactory mqcf = mqcff.createConnectionFactory(MQConnectionFactory.class);
        return mqcf;
    }


    @Bean("myJmsTemplate")
    public JmsTemplate myJmsTemplate() throws JMSException {
        JmsTemplate jmsTemplate = new JmsTemplate(mqConnectionFactory());
        
        // Any other customisations necessary go here.
        // ...

        return jmsTemplate;
    }

}


It worked after removing spring jms template and written new class for obtaining IBM MQ connection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM