This project provides a set of base Java samples for interacting with IBM MQ using the IBM MQ classes for Java (non-JMS). These include basic examples for put/get, publish/subscribe, and request/response messaging patterns.
Each sample is located under:
src/main/java/com/ibm/mq/samples/java/
BasicPut.java– Puts a message onto a queueBasicGet.java– Gets a message from a queueBasicPub.java– Publishes a message to a queue (persisted publish)BasicSub.java– Subscribes and receives messages from a queueBasicRequest.java– Sends a message with a dynamic reply-to queueBasicResponse.java– Responds to requests received from a queueBasicProducer.java– Produces a message to a queue (used by request wrappers)BasicConsumer.java– Consumes a message from a queue (used by request/response logic)MQDetails.java– POJO to hold MQ connection configurationSampleEnvSetter.java– Utility to parseenv.jsonand load MQ endpoint configurations- Wrapper Classes (used internally for iterating over endpoints):
BasicPutWrapper.javaBasicGetWrapper.javaBasicPubWrapper.javaBasicSubWrapper.javaBasicRequestWrapper.javaBasicResponseWrapper.java
Note: Wrapper classes are utility helpers and should not be run directly.
- Java 8 or higher
- Apache Maven
- IBM MQ installed and running (locally or remotely)
This is a standard Maven project. All dependencies and build instructions are managed through pom.xml.
To build the project and download dependencies:
mvn clean packageEnsure that your environment configuration file (env.json) is properly set up.
Instead of manually specifying connection parameters in env.json, you can use a Client Channel Definition Table (CCDT) JSON file to define connection configurations. This is useful when connecting to IBM MQ instances in cloud or enterprise environments.
Set the environment variable MQCCDTURL to point to the CCDT file:
export MQCCDTURL=file:/absolute/path/to/ccdt.jsonNote (Windows): Use
setinstead ofexport:set MQCCDTURL=file:C:\path\to\ccdt.json
The sample will detect MQCCDTURL and automatically use it for connection settings. When MQCCDTURL is set and starts with file://, the program prioritizes CCDT-based configuration and skips host, channel, and port in env.json.
Make sure your CCDT file defines the appropriate connection information such as channel name, queue manager, and connection name list.
All samples should be run using the following format:
mvn exec:java \
-Dexec.mainClass="com.ibm.mq.samples.java.<ClassName>" \
-Dexec.args="-Dcom.ibm.mq.cfg.jmqiDisableAsyncThreads=true" \
-Dexec.jvmArgs="-DENV_FILE=./env.json"mvn exec:java \
-Dexec.mainClass="com.ibm.mq.samples.java.BasicPut" \
-Dexec.args="-Dcom.ibm.mq.cfg.jmqiDisableAsyncThreads=true" \
-Dexec.jvmArgs="-DENV_FILE=./env.json"mvn exec:java \
-Dexec.mainClass="com.ibm.mq.samples.java.BasicGet" \
-Dexec.args="-Dcom.ibm.mq.cfg.jmqiDisableAsyncThreads=true" \
-Dexec.jvmArgs="-DENV_FILE=./env.json"First terminal (subscriber):
mvn exec:java \
-Dexec.mainClass="com.ibm.mq.samples.java.BasicSub" \
-Dexec.args="-Dcom.ibm.mq.cfg.jmqiDisableAsyncThreads=true" \
-Dexec.jvmArgs="-DENV_FILE=./env.json"Second terminal (publisher):
mvn exec:java \
-Dexec.mainClass="com.ibm.mq.samples.java.BasicPub" \
-Dexec.args="-Dcom.ibm.mq.cfg.jmqiDisableAsyncThreads=true" \
-Dexec.jvmArgs="-DENV_FILE=./env.json"First terminal (response):
mvn exec:java \
-Dexec.mainClass="com.ibm.mq.samples.java.BasicResponse" \
-Dexec.args="-Dcom.ibm.mq.cfg.jmqiDisableAsyncThreads=true" \
-Dexec.jvmArgs="-DENV_FILE=./env.json"Second terminal (request):
mvn exec:java \
-Dexec.mainClass="com.ibm.mq.samples.java.BasicRequest" \
-Dexec.args="-Dcom.ibm.mq.cfg.jmqiDisableAsyncThreads=true" \
-Dexec.jvmArgs="-DENV_FILE=./env.json"- The environment can be configured using either
env.jsonor a CCDT file via theMQCCDTURLenvironment variable. - Samples like
BasicResponseandBasicSubare long-running and wait for messages until terminated or a timeout occurs. - Wrapper classes are designed to iterate over all endpoints in
env.json, but are not meant to be executed directly from the command line. - Make sure all relevant queues and topics are pre-created in your IBM MQ queue manager.