Skip to content

Latest commit

 

History

History
106 lines (93 loc) · 3.88 KB

IBMMQ.md

File metadata and controls

106 lines (93 loc) · 3.88 KB

Java Template Tutorial

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.

Prerequisites

Run MQ

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.

Install Maven

For instructions on installing maven for your operating system, please see the Apache Maven site.

Install the AsyncAPI Generator

This template must be used with the AsyncAPI Generator, if you have not already installed the Generator, run:

npm install -g @asyncapi/generator

Set up your Working Environment

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

Running the Publisher/Subscriber Template

These commands will allow you to run the Java Template publisher/subscriber model using IBM MQ.

  1. Run the AsyncAPI Generator.
    Note: You may need to change the username and password values if you have not followed the IBM MQ tutorial.
    ag https://ibm.biz/mq-asyncapi-yml-sample @asyncapi/java-template -o ./output -p server=production -p user=app -p password=passw0rd
    
    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 [YAML_FILE] [TEMPLATE_DIRECTORY] -o ./output -p server=[NAME_OF_SERVER] -p user=[MQ_USERNAME] -p password=[MQ_PASSWORD]
    
  2. Navigate to the generated output directory
    cd output
    
  3. Install the dependencies required to run this template
    mvn compile 
    
  4. Create .jar package using maven
    mvn package
    
  5. Run your generated Subscriber
    java -cp target/asyncapi-java-generator-0.1.0.jar com.ibm.mq.samples.jms.DemoSubscriber
    
  6. In a seperate terminal, navigate to the output directory above and run your generated Publisher
    cd ~/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

Running with Docker

To deploy a dockerised instance of this project;

  1. Build the image

     docker build -t [PACKAGE_NAME]:[VERSION] .
    
  2. 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.

Networking

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

  1. Create a docker network
     docker network create exampleMqNetwork
    
  2. Attach the docker network to your MQ container
     docker network connect exampleMqNetwork
    
  3. Update your env.json server hostname to match the container name for MQ. To find the name run
    docker ps