Using JMS to connect to IBM MQ
I want to use JMS to connect to IBM MQ. How do i specify the queuemana开发者_JAVA百科ger, the channel and other properties ?
Using JNDI for connectionFactory/destinations lookups, provide the InitialContext
with the following properties:
java.naming.provider.url=<ip>:<port, default is 1414>/<channel name, default channel is SYSTEM.DEF.SVRCONN>
java.naming.factory.initial=com.ibm.mq.jms.context.WMQInitialContextFactory
java.naming.security.authentication=none
java.naming.security.credentials=
java.naming.security.principal=
using WAS (Websphere Application Server) queues, the properties would be as follows:
java.naming.provider.url=iiop://<ip>:<port, the defualt is 2809>
java.naming.factory.initial=com.ibm.websphere.naming.WsnInitialContextFactory
java.naming.security.authentication=none
java.naming.security.credentials=
java.naming.security.principal=
The rest would be as follows:
Properties config = new Properties();
config.load(new FileInputStream("connectionConfig.properties"));// this file would contain the properties above
InitialContext context = new InitialContext(config);
ConnectionFactory factory = (ConnectionFactory) context.lookup("ConnectionFactory");// connection factory name
Destination destination = (Destination) context.lookup("destination");// queue/topic name
You need to create an MQQueueConnectionFactory bean and set the required properties in it.
<bean id="queueConnectionFactory" class="com.ibm.mq.jms.MQQueueConnectionFactory">
<property name="transportType" ref="transport" />
<property name="queueManager" value="queueManagerName" />
<property name="hostName" value="hostName" />
<property name="port" value="portNumber" />
<property name="channel" value="channelName" />
</bean>
<bean id="transport"
class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
<property name="staticField">
<value>
com.ibm.mq.jms.JMSC.MQJMS_TP_CLIENT_MQ_TCPIP
</value>
</property>
</bean>
Here's a tutorial that may help:
Also, be sure to use the docs for the right version of WMQ. V7.0 is current and v6.0 is supported until September 2011. Whichever you are using, look at the Using Java manual for the right version:
v6.0 manual
v7.0 manual
Using IBM client API
import com.ibm.mq.MQEnvironment;
import com.ibm.mq.MQQueue;
import com.ibm.mq.MQQueueManager;
import com.ibm.mq.constants.CMQC;
public class QueueMonitoring {
public static void main(String[] args) {
int openOptions = CMQC.MQOO_INQUIRE | CMQC.MQOO_INPUT_AS_Q_DEF;
MQEnvironment.hostname = "192.168.59.103";
MQEnvironment.port = 1414;
MQEnvironment.channel = "SYSTEM.DEF.SVRCONN";
MQEnvironment.properties.put(CMQC.TRANSPORT_PROPERTY,CMQC.TRANSPORT_MQSERIES);
MQQueueManager qMgr;
try {
qMgr = new MQQueueManager("QM1");
MQQueue destQueue = qMgr.accessQueue("DOCKERQ", openOptions);
System.out.println("Queue size:" + destQueue.getCurrentDepth());
destQueue.close();
qMgr.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
}
the best way is to use the command line. It is a lot of fun. You could download the command reference book from http://publib.boulder.ibm.com/iseries/v5r2/ic2924/books/csqzaj05.pdf
If your MQ server is running on a windows machine, you could optionally use a MQExplorer and configure it graphically.
精彩评论