-
Notifications
You must be signed in to change notification settings - Fork 178
Add base java samples #316
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
stutiuneyal
wants to merge
8
commits into
ibm-messaging:master
Choose a base branch
from
stutiuneyal:add-base-java-samples
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f59383e
add base java samples
stutiuneyal 6a41305
updated readme
stutiuneyal 5507019
address review comments
stutiuneyal c3938d9
Deleted outdated samples
stutiuneyal 66d12e1
Deleted outdated samples
stutiuneyal 0125416
updated env config for base java mq samples
stutiuneyal 6080377
added Java-MQ sample link to README
stutiuneyal bc54ec2
Rename Readme.md to README.md
stutiuneyal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
# IBM Java-MQ Samples | ||
|
||
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. | ||
|
||
## Samples Included | ||
|
||
Each sample is located under: | ||
|
||
``` | ||
src/main/java/com/ibm/mq/samples/java/ | ||
``` | ||
|
||
- `BasicPut.java` – Puts a message onto a queue | ||
- `BasicGet.java` – Gets a message from a queue | ||
- `BasicPub.java` – Publishes a message to a queue (persisted publish) | ||
- `BasicSub.java` – Subscribes and receives messages from a queue | ||
- `BasicRequest.java` – Sends a message with a dynamic reply-to queue | ||
- `BasicResponse.java` – Responds to requests received from a queue | ||
- `BasicProducer.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 configuration | ||
- `SampleEnvSetter.java` – Utility to parse `env.json` and load MQ endpoint configurations | ||
- Wrapper Classes (used internally for iterating over endpoints): | ||
- `BasicPutWrapper.java` | ||
- `BasicGetWrapper.java` | ||
- `BasicPubWrapper.java` | ||
- `BasicSubWrapper.java` | ||
- `BasicRequestWrapper.java` | ||
- `BasicResponseWrapper.java` | ||
|
||
> **Note**: Wrapper classes are utility helpers and should not be run directly. | ||
|
||
## Prerequisites | ||
|
||
- Java 8 or higher | ||
- Apache Maven | ||
- IBM MQ installed and running (locally or remotely) | ||
|
||
## Project Setup | ||
|
||
This is a standard Maven project. All dependencies and build instructions are managed through `pom.xml`. | ||
|
||
### Building the Project | ||
|
||
To build the project and download dependencies: | ||
|
||
```bash | ||
mvn clean package | ||
``` | ||
|
||
## Running the Samples | ||
|
||
Ensure that your environment configuration file (`env.json`) is properly set up. | ||
|
||
## Using a CCDT File | ||
|
||
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: | ||
|
||
```bash | ||
export MQCCDTURL=file:/absolute/path/to/ccdt.json | ||
``` | ||
|
||
> **Note (Windows):** Use `set` instead of `export`: | ||
> | ||
> ```cmd | ||
> 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**. | ||
|
||
## Run Instructions | ||
|
||
All samples should be run using the following format: | ||
|
||
```bash | ||
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" | ||
``` | ||
|
||
### Put | ||
|
||
```bash | ||
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" | ||
``` | ||
### Get | ||
|
||
```bash | ||
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" | ||
``` | ||
|
||
### Publish/Subscribe | ||
|
||
**First terminal (subscriber):** | ||
|
||
```bash | ||
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):** | ||
|
||
```bash | ||
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" | ||
``` | ||
|
||
### Request/Response | ||
|
||
**First terminal (response):** | ||
|
||
```bash | ||
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):** | ||
|
||
```bash | ||
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" | ||
``` | ||
|
||
## Notes | ||
|
||
- The environment can be configured using either `env.json` or a CCDT file via the `MQCCDTURL` environment variable. | ||
- Samples like `BasicResponse` and `BasicSub` are 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<!-- | ||
(c) Copyright IBM Corporation 2025 | ||
|
||
Licensed to the Apache Software Foundation (ASF) under one or more | ||
contributor license agreements. See the NOTICE file distributed with | ||
this work for additional information regarding copyright ownership. | ||
The ASF licenses this file to You under the Apache License, Version 2.0 | ||
(the "License"); you may not use this file except in compliance with | ||
the License. You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 | ||
http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.ibm.mq.samples</groupId> | ||
<artifactId>base-mq-java</artifactId> | ||
<version>1.0.0</version> | ||
<packaging>jar</packaging> | ||
|
||
<properties> | ||
<maven.compiler.source>1.8</maven.compiler.source> | ||
<maven.compiler.target>1.8</maven.compiler.target> | ||
</properties> | ||
|
||
<dependencies> | ||
<!-- MQ Classes for Java (Base Java MQ API) --> | ||
<dependency> | ||
<groupId>com.ibm.mq</groupId> | ||
<artifactId>com.ibm.mq.allclient</artifactId> | ||
<version>9.4.3.0</version> | ||
</dependency> | ||
|
||
<!-- JSON library --> | ||
<dependency> | ||
<groupId>org.json</groupId> | ||
<artifactId>json</artifactId> | ||
<version>20240303</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>3.8.1</version> | ||
<configuration> | ||
<release>1.8</release> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
69 changes: 69 additions & 0 deletions
69
Java-MQ/src/main/java/com/ibm/mq/samples/java/BasicConsumer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* (c) Copyright IBM Corporation 2025 | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.ibm.mq.samples.java; | ||
|
||
import com.ibm.mq.*; | ||
import com.ibm.mq.constants.MQConstants; | ||
|
||
import java.io.IOException; | ||
import java.util.Hashtable; | ||
|
||
public class BasicConsumer { | ||
|
||
public static final String CONSUMER_GET = "queue"; | ||
public static final String CONSUMER_SUB = "topic"; | ||
public static final String CONSUMER_REQUEST = "model_queue"; | ||
|
||
public Boolean getMessage(MQDetails details, Hashtable<String, Object> props, MQQueue queue, String mode) { | ||
|
||
MQMessage msg = new MQMessage(); | ||
MQGetMessageOptions gmo = new MQGetMessageOptions(); | ||
|
||
if (mode.equals(CONSUMER_GET)) { | ||
gmo.options = MQConstants.MQGMO_NO_WAIT | | ||
MQConstants.MQGMO_CONVERT | | ||
MQConstants.MQGMO_FAIL_IF_QUIESCING; | ||
} else if (mode.equals(CONSUMER_SUB)) { | ||
gmo.options = MQConstants.MQGMO_NO_SYNCPOINT | | ||
MQConstants.MQGMO_WAIT | | ||
MQConstants.MQGMO_CONVERT | | ||
MQConstants.MQGMO_FAIL_IF_QUIESCING; | ||
gmo.waitInterval = 10000; | ||
} else if (mode.equals(CONSUMER_REQUEST)) { | ||
gmo.options = MQConstants.MQGMO_WAIT | MQConstants.MQGMO_CONVERT; | ||
gmo.waitInterval = 10000; | ||
} | ||
|
||
try { | ||
queue.get(msg, gmo); | ||
String str = msg.readStringOfByteLength(msg.getDataLength()); | ||
System.out.println("Received message: " + str); | ||
} catch (MQException mqe) { | ||
if (mqe.reasonCode == MQConstants.MQRC_NO_MSG_AVAILABLE) { | ||
System.out.println("No more messages."); | ||
return false; | ||
} else { | ||
System.err.println("Error retrieving message: " + mqe); | ||
return false; | ||
} | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
Java-MQ/src/main/java/com/ibm/mq/samples/java/BasicConsumerWrapper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* (c) Copyright IBM Corporation 2025 | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.ibm.mq.samples.java; | ||
|
||
import com.ibm.mq.constants.MQConstants; | ||
|
||
import java.util.Hashtable; | ||
|
||
import com.ibm.mq.*; | ||
|
||
public class BasicConsumerWrapper { | ||
|
||
MQQueueManager qMgr = null; | ||
MQQueue queue = null; | ||
|
||
public void sendMessage() { | ||
|
||
SampleEnvSetter envSetter = new SampleEnvSetter(); | ||
envSetter.setEnvValues(); | ||
|
||
// iterate for every endpoint in env.json | ||
int length = envSetter.getDetails().size(); | ||
System.out.println(length); | ||
|
||
for (int i = 0; i < length; i++) { | ||
MQDetails details = envSetter.getDetails().get(i); | ||
Hashtable<String,Object> props = envSetter.getProps().get(i); | ||
System.out.println("Wrapper: "+props); | ||
|
||
System.out.println("Wrapper QMGR: "+props); | ||
try { | ||
qMgr = new MQQueueManager(details.getQMGR(), props); | ||
System.out.println("Connected to queue manager: " + details.getQMGR()); | ||
|
||
int openOptions = MQConstants.MQOO_INPUT_AS_Q_DEF; | ||
queue = qMgr.accessQueue(details.getQUEUE_NAME(), openOptions); | ||
|
||
System.out.println("Wrapper Queue: "+queue); | ||
|
||
boolean keepReading = true; | ||
BasicConsumer bc = new BasicConsumer(); | ||
while (keepReading) { | ||
keepReading = bc.getMessage(details, props, queue,BasicConsumer.CONSUMER_GET); | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} finally { | ||
try { | ||
if (queue != null) | ||
queue.close(); | ||
if (qMgr != null) | ||
qMgr.disconnect(); | ||
} catch (MQException me) { | ||
me.printStackTrace(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
Java-MQ/src/main/java/com/ibm/mq/samples/java/BasicGet.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* | ||
* (c) Copyright IBM Corporation 2025 | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.ibm.mq.samples.java; | ||
|
||
public class BasicGet { | ||
|
||
public static void main(String[] args) { | ||
BasicConsumerWrapper wrapper = new BasicConsumerWrapper(); | ||
wrapper.sendMessage(); | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The construct in the mains should be
construct - creates the connection
action - put / get / pub / sub etc
close - closes the connection
to emphasise that the connection create / close is time consuming and should be performed once for multiple actions.