连接到Websphere Mq的问题

时间:2016-07-20 10:35:07

标签: java spring ibm-mq

我使用spring jms连接到Websphere MQ。

Connection names (all as an example): hostname: <Websphere ip address> http://localhost:9043
given hostname 12.123.0.12

In Resources->JMS->Queue connection Factories
name: MQDemo
JNDI name: MQDemo

2 Queues:
name: MQ.T11.UPDATE.REQUEST
JNDI name: jms/MQ.T11.UPDATE.REQUEST

name: MQ.T11.INQUIRY.REPLY
JNDI name: jms/MQ.T11.UPDATE.REQUEST

DemoMain.java

主要方法:

ApplicationContext ctx = new ClassPathXmlApplicationContext("app-context.xml");
    // get bean from context
    JmsMessageSender jmsMessageSender = (JmsMessageSender)ctx.getBean("jmsMessageSender");
    // send to default destination 
         jmsMessageSender.send();

JMSMessageSender.java

package name: com.java.geek

 @Autowired
  private JmsTemplate jmsTemplate;


  /**
   * send text to default destination
   * @param text
   */
  @Cacheable("message")
  public void send() {
      System.out.println("in send");
        jmsTemplate.send(new MessageCreator() {
        @Override
        public Message createMessage(Session session)throws JMSException {

            System.out.println("in create message");
        Message message = (Message) session.createTextMessage("Hello JMS");
        return message;
        }
        });

    }

  public JmsTemplate getJmsTemplate()
  {
      return jmsTemplate;
  }

  public void setJmsTemplate(JmsTemplate jmsTemplate)
  {
      this.jmsTemplate = jmsTemplate;
  }

APP-context.xml中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:beans="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context.xsd">


  <context:component-scan base-package="com.java.geek" />
<bean id="amqConnectionFactory" class="com.ibm.mq.jms.MQQueueConnectionFactory">
    <property name="hostName">
        <value>http://localhost:9043</value>
    </property>
    <property name="port">
        <value>1415</value>
    </property>
    <property name="queueManager">
        <value>MQDemo</value>
    </property>
    <property name="transportType">
        <value>1</value>
    </property>
</bean>

  <!-- Pooled Spring connection factory -->
  <bean id="connectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean" lazy-init="true"> 
        <property name="MQDemo" value="java:comp/env/jms/MQDemo" /> 
         <property name="lookupOnStartup" value="false"/>
           <property name="cache" value="true" />
           <property name="proxyInterface"  value="javax.jms.QueueConnectionFactory"/>
        <constructor-arg ref="amqConnectionFactory" />
    </bean>


  <!-- JmsTemplate Definition -->
  <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> 
        <property name="connectionFactory"><ref bean="amqConnectionFactory" />  </property> 
        <property name="pubSubDomain"><value>false</value></property> 
        <!-- <property name="defaultDestination"><ref bean="senderQueue" /></property>  -->
    </bean>  

    <!-- ======================================================= -->
  <!-- JMS Send, define default destination and JmsTemplate    -->
  <!-- ======================================================= -->

  <!-- Default Destination Queue Definition -->
  <bean id="senderQueue" class="com.ibm.mq.jms.MQQueue"> 
        <constructor-arg value="TEST" /> 
        <property name="MQ.T11.UPDATE.REQUEST" value="java:comp/env/jms/MQ.T11.UPDATE.REQUEST" /> 
    </bean> 
    <bean id="jmsMessageSender" class="com.java.geek.JmsMessageSender"> 
        <property name="jmsTemplate"><ref bean="SenderJMSTemplate"/></property>     
    </bean> 

</beans>

我收到了错误,

"Error creating bean with name 'jmsMessageSender': Injection of autowired dependencies failed; 
nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.jms.core.JmsTemplate com.shengwang.demo.JmsMessageSender.jmsTemplate; 
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'connectionFactory' defined in class path resource [app-context.xml]: 
Cannot resolve reference to bean 'amqConnectionFactory' while setting constructor argument; 
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'amqConnectionFactory' defined in class path resource [app-context.xml]: Instantiation of bean failed; 
nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.ibm.mq.jms.MQQueueConnectionFactory]: Constructor threw exception; 
nested exception is java.lang.NoClassDefFoundError: com/ibm/disthub2/impl/client/SessionConfig

和另一个错误SAXException

我无法连接到Websphere MQ。请建议代码中的错误在哪里。

2 个答案:

答案 0 :(得分:0)

NoClassDefFoundError表明编译器能够找到类,但运行时进程不是。

我建议您的运行时类路径中可能缺少某些内容。尝试将它与构建/编译类路径进行比较。

答案 1 :(得分:0)

我说你需要检查代码运行的盒子上是否正确安装了MQ Client。复制WMQ jar并期望它能够正常工作是一个常见的错误。您的代码将构建,但您将从MQ类中获得这种类型的NoClassDefFound。虽然您最终可以在工作的IBM安装中追逐每个jar,但它不受支持(参见http://www-01.ibm.com/support/docview.wss?uid=swg21376217

获取WMQ安装媒体并在您使用的计算机上运行安装。然后运行IBM的#34;入门&#34;确保它可以运作。