I am using spring jms to connect to 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
In main method:
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>
I am getting an error,
"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
and another error, SAXException
.
I am unable to connect to Websphere MQ. Kindly suggest where is a mistake in the code.
The NoClassDefFoundError suggests that the compiler was able to find the class but the runtime process was not.
I suggest that something is probably missing from your runtime classpath. Try comparing it to your build/compile classpath.
I'd say you need to check whether MQ Client is correctly installed on the box your code is running. It is a common mistake to just copy WMQ jars and expect it to work. Your code will build, but you will get precisely this kind of NoClassDefFound from within MQ classes. While you may get it to work eventually chasing every single jar inside a working IBM installation, it will not be supported (see http://www-01.ibm.com/support/docview.wss?uid=swg21376217)
Obtain WMQ installation media and run install on the machine you use. Then run through IBM's "Getting Started" to make sure it's operational.