This file provides example commands which can be used to run the Java Template publisher/subscriber model using IBM MQ and a provided YAML file.
You will need a running instance of MQ with a queue manager named QM1
, instructions of how to run MQ on a container and create this queue manager can be found here. If you are new to MQ, or want a refresher, you can click here.
For instructions on installing maven for your operating system, please see the Apache Maven site.
This template must be used with the AsyncAPI Generator, if you have not already installed the Generator, run:
npm install -g @asyncapi/generator
To work with the Java Template, you will need to clone the repository. Navigate to a directory where you would like to store the code, for example run
mkdir ~/asyncapi-java-tutorial
You will then need to enter the directory you have just created, for example with
cd ~/asyncapi-java-tutorial
These commands will allow you to run the Java Template publisher/subscriber model using IBM MQ.
- Run the AsyncAPI Generator.
Note: You may need to change the username and password values if you have not followed the IBM MQ tutorial.Note: The syntax of the above command is shown below. You do not need to run the below line, it is for informational purposes only.ag https://ibm.biz/mq-asyncapi-yml-sample @asyncapi/java-template -o ./output -p server=production -p user=app -p password=passw0rd
ag [YAML_FILE] [TEMPLATE_DIRECTORY] -o ./output -p server=[NAME_OF_SERVER] -p user=[MQ_USERNAME] -p password=[MQ_PASSWORD]
- Navigate to the generated output directory
cd output
- Install the dependencies required to run this template
mvn compile
- Create .jar package using maven
mvn package
- Run your generated Subscriber
java -cp target/asyncapi-java-generator-0.1.0.jar com.ibm.mq.samples.jms.DemoSubscriber
- In a seperate terminal, navigate to the
output
directory above and run your generated Publishercd ~/asyncapi-java-tutorial/output java -cp target/asyncapi-java-generator-0.1.0.jar com.ibm.mq.samples.jms.DemoProducer
The messages will now be seen to be being sent from the running publisher to the running subscriber, using MQ topics. Your output from your subscriber should look something like
Oct 14, 2021 9:53:23 AM com.ibm.mq.samples.jms.SingleReleasedSubscriber receive
INFO: Received message: {
“title” : “Hackathon”,
“artist” : “Java”,
“album” : “JMS”,
“genre” : “Java”,
“length” : 166
}
TYPE: com.ibm.mq.samples.jms.models.Single
To deploy a dockerised instance of this project;
-
Build the image
docker build -t [PACKAGE_NAME]:[VERSION] .
-
Run the image in detached mode
docker run -d [PACKAGE_NAME]:[VERSION]
Please note; The default Dockerfile
included in output will only run DemoSubscriber.java
. This will need to be replaced with the entrypoint to your application.
Docker networking needs to be properly configured in order to allow your project to connect to an MQ instance.
If MQ is also running in a docker container
- Create a docker network
docker network create exampleMqNetwork
- Attach the docker network to your MQ container
docker network connect exampleMqNetwork
- Update your env.json server hostname to match the container name for MQ. To find the name run
docker ps