require 'java' # Optional on JRuby

# define the name of the QueueManager
qmgr_name = "REID"
# and define the name of the Queue
q_name = "SYSTEM.DEFAULT.LOCAL.QUEUE"

# Create a connection to the QueueManager
puts "Connecting to queue manager: "+qmgr_name
qmgr = Java.com.ibm.mq.MQQueueManager.new qmgr_name
MQC = Java::com.ibm.mq.MQC # Reference (Shortcut) to MQC Constants Class

# Set up the options on the queue we wish to open
openOptions = MQC::MQOO_INPUT_AS_Q_DEF | MQC::MQOO_OUTPUT

# Now specify the queue that we wish to open and the open options
puts "Accessing queue: "+q_name
queue = qmgr.access_queue(q_name, openOptions)

# Define a simple WebSphere MQ Message ...
msg = Java::com.ibm.mq.MQMessage.new
# ... and write some text in UTF8 format
msg.writeUTF("Hello, World!")

# Specify the default put message options
pmo = Java::com.ibm.mq.MQPutMessageOptions.new

# Put the message to the queue
puts "Sending a message..."
queue.put(msg, pmo);

# Now get the message back again. First define a WebSphere MQ message
# to receive the data
rcvMessage = Java::com.ibm.mq.MQMessage.new

# Specify default get message options
gmo = Java::com.ibm.mq.MQGetMessageOptions.new

# Get the message off the queue.
puts "...and getting the message back again"
queue.get(rcvMessage, gmo)

# And display the message text...
msgText = rcvMessage.readUTF
puts "The message is: " + msgText

# Close the queue
puts "Closing the queue"
queue.close

# Disconnect from the QueueManager
puts "Disconnecting from the Queue Manager"
qmgr.disconnect
puts "Done!"