Websphere MQ Runs Inside The QMQM Subsystem So This Needs To Be Running Before Anything Else Can Be Done
Websphere MQ Runs Inside The QMQM Subsystem So This Needs To Be Running Before Anything Else Can Be Done
In this case we have called the Queue manager CAT31, and used the default
dead letter queue of SYSTEM.DEAD.LETTER.QUEUE. This is the queue any
undeliverable message will end up.
Creating a Queue
Starting an MQ Listener
import java.io.IOException;
import java.util.Hashtable;
import com.ibm.mq.MQC;
import com.ibm.mq.MQException;
import com.ibm.mq.MQGetMessageOptions;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQPutMessageOptions;
import com.ibm.mq.MQQueue;
import com.ibm.mq.MQQueueManager;
/**
* Sample class to show Java & WebSphere MQ
*
* @author Logicalis Computing Solutions (LCS)
*
*/
try {
String server = "CAT31"; // The server name
Integer port = new Integer(1414); // The Port the listener is running on
String user = "USER"; // User name
String password = "PASSWORD"; // Password
String qmanager = "CAT31"; // Queue Manager name
String q = "CAT31.GET.AND.PUT"; // Queue to put messages on
MQQueue requestQueue;
MQPutMessageOptions pmo = new MQPutMessageOptions();
// properties.put(MQC.USER_ID_PROPERTY, user);
// properties.put(MQC.PASSWORD_PROPERTY, password);
properties.put(MQC.HOST_NAME_PROPERTY, server);
properties.put(MQC.PORT_PROPERTY, port);
properties.put(MQC.CHANNEL_PROPERTY, "SYSTEM.ADMIN.SVRCONN");
// ------------------------------------------------------------
} catch (MQException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Sample RPG Code to receive and send messages
NOTE – the input parameters (Q name and Q manager name) are both 48 character and must be padded out with blanks to 48 characters in order
for this to function correctly.
Parms: QNAME, QMNAME
There are some other sample RPG and RPGLE programs to be found in library QMQMSAMP
H
****************************************************************
* *
* Program name: MQTEST4 *
* *
* Description: Sample RPG program that gets messages from *
* a message queue (example using MQGET), *
* processes them and puts them back to the *
* message queue (example using MQPUT) *
* *
****************************************************************
* *
* Function: *
* *
* MQTEST4 is a sample RPG program to get messages from a *
* message queue, Process and put them back on the message *
* queue. It is an example using MQGET & MQPUT. *
* *
* Program logic: *
* MQCONN to the queue manager *
* MQOPEN queue for INPUT *
* while no MQI failures, *
* . MQGET next message, remove from queue *
* . MQPUT message *
* . (no message available is failure, and ends loop) *
* MQCLOSE the subject queue *
* MQDISC from the queue manager *
* *
****************************************************************
* *
* *
* *
* Exceptions signaled: none *
* Exceptions monitored: none *
* *
* MQTEST4 has 2 parameters - (1) name of the queue *
* (2) name of the queue manager *
* *
****************************************************************
FQSYSPRT O F 132 PRINTER
*
** Declare MQI structures needed
* MQI named constants
I/COPY CMQR
* Object Descriptor
IMQOD DS
I/COPY CMQODR
* Message Descriptor
IMQMD DS
I/COPY CMQMDR
* Get message options
IMQGMO DS
I/COPY CMQGMOR
*
* Put message options
IMQPMO DS
I/COPY CMQPMOR
** note, sample uses defaults where it can
*
* program parameters are: 48-character target queue name
* 48-character queue manager name
*
C *ENTRY PLIST
C PARM QNAME 48
C PARM QMNAME 48
C MOVELQNAME ODON 48
*
** indicate that sample program has started
C MOVEL'start' STATUS 5
C EXCPTSAMPLE
*
C EXSR S@STRT
*
C EXSR S@PROC
*
C EXSR S@END
*
C MOVEL'end ' STATUS 5
C SETON LR
*
****************************************************************
* S@STRT - CONNECT and OPEN Q's *
****************************************************************
C S@STRT BEGSR
*
* Connect to the queue manager
C Z-ADDMQCONN CID
C CALL 'QMQM'
C PARM CID 90
C PARM QMNAME 48
C PARM HCONN 90
C PARM MQCODE 90
C PARM CREASN 90
*
* report reason and stop if it failed
C MQCODE IFEQ CCFAIL
** "MQCONN ended with reason code ...."
C MOVEL'MQCONN' CNAME 6
C MOVE CREASN RCODE 10
C EXCPTMQCALL
** "unable to connect to MQM"
C MOVE MQCODE CCODE
C EXCPTMESSC
C SETON LR
C RETRN
C ENDIF
*
* Open the named Input message Q (and fail if MQM *
* is quiescing); exclusive or shared use of the queue is *
* controlled by the queue definition here *
* *
* Resulting queue handle is HOBJ *
****************************************************************
* options are input-as-queue-def and fail-if-quiescing
* and Output
C OOINPQ ADD OOFIQ OPTS
C ADD OOOUT OPTS
* call ...
C Z-ADDMQOPEN CID
C CALL 'QMQM'
C PARM CID 90
C PARM HCONN 90
C PARM MQOD
C PARM OPTS 90
C PARM HOBJ 90
C PARM OCODE 90
C PARM REASON 90
*
* report reason, if any; stop if failed
C REASON IFNE RCNONE
** "MQOPEN ended with reason code ...."
C MOVEL'MQOPEN' CNAME 6
C MOVE REASON RCODE 10
C EXCPTMQCALL
C ENDIF
*
C OCODE IFEQ CCFAIL
** "unable to open queue for input"
C EXCPTMESS
C ENDIF
*
C ENDSR
*
****************************************************************
* S@PROC - GET and PUT to Q *
****************************************************************
C S@PROC BEGSR
*
** initial loop condition based on result of MQOPEN
C MOVE OCODE CCODE
* buffer length available ...
C Z-ADD60 BUFLEN
* option is to wait up to 15 seconds for next message
C Z-ADDGMWT GMOPT wait
C ADD GMCONV GMOPT convert
C Z-ADD15000 GMWI up to 15sec
*
** MsgId and CorrelId are selectors that must be cleared
** to get messages in sequence, and they are set each MQGET
C MOVELMINONE MDMID
C MOVELCINONE MDCID
** clear buffer because MQGET only fills to length of message
C MOVEL*BLANKS BUFFER
* call ...
C Z-ADDMQGET CID
C CALL 'QMQM'
C PARM CID 90
C PARM HCONN 90
C PARM HOBJ 90
C PARM MQMD
C PARM MQGMO
C PARM BUFLEN 90
C PARM BUFFER 60
C PARM MESLEN 90
C PARM CCODE 90
C PARM REASON 90
*
** report reason, if any
C SELEC
C REASON WHEQ RCNONE
** special text for "no more messages"
C REASON WHEQ RC2033
C EXCPTNOMESS
** otherwise say "MQGET ended with reason code ...."
C OTHER
C MOVEL'MQGET ' CNAME
C MOVE REASON RCODE
C EXCPTMQCALL
C ENDSL
*
** . . .display each message received
C CCODE IFNE CCFAIL
C EXCPTRCVD
** Send message back to Q
C EXSR S@SEND
C ENDIF
*
** end DO-while loop; MQI failure causes loop to end
*
C ENDSR
*
****************************************************************
* S@END - CLOSE and DISCONNECT from Q *
****************************************************************
C S@END BEGSR
*
* ... close queue (with no options)
C Z-ADDMQCLOS CID
C Z-ADDCONONE OPTS
C CALL 'QMQM'
C PARM CID 90
C PARM HCONN 90
C PARM HOBJ 90
C PARM OPTS 90
C PARM CCODE 90
C PARM REASON 90
*
* report reason, if any, resulting from this call
C REASON IFNE RCNONE
** "MQCLOS ended with reason code ...."
C MOVEL'MQCLOS' CNAME
C MOVE REASON RCODE
C EXCPTMQCALL
C ENDIF
*
* Disconnect from the queue manager *
C Z-ADDMQDISC CID
C CALL 'QMQM'
C PARM CID 90
C PARM HCONN 90
C PARM MQCODE 90
C PARM REASON 90
*
* report reason, if any, rsulting from this call
C MQCODE IFEQ CCFAIL
** "MQDISC ended with reason code ...."
C MOVEL'MQDISC' CNAME 6
C MOVE REASON RCODE 10
C ENDIF
*
C ENDSR
*
****************************************************************
* S@SEND - Send a message back
****************************************************************
C S@SEND BEGSR
*
C 'SEND ' CAT BUFFER BUFER2
C MOVELFMSTR MDFMT
*
C** put buffer to message queue
C Z-ADDMQPUT CID
C CALL 'QMQM'
C PARM CID 90
C PARM HCONN 90
C PARM HOBJ 90
C PARM MQMD
C PARM MQPMO
C PARM BUFLEN 90
C PARM BUFER2 70
C PARM CCODE 90
C PARM REASON 90
*
** report reason, if any; stop if failed
C REASON IFNE RCNONE
** "MQPUT ended with reason code ...."
C MOVEL'MQPUT ' CNAME
C MOVE REASON RCODE
C EXCPTMQCALL
C ENDIF
**
** . . .display each message sent
C CCODE IFNE CCFAIL
C EXCPTSENT
C ENDIF
**
C ENDSR
**
****************************************************************
OQSYSPRT E 1 SAMPLE
O 20 'Sample MQTEST4 '
O STATUS 25
O E 1 RCVD
O 10 'Received <'
O BUFFER
O '>'
O E 1 SENT
O 10 'Sent <'
O BUFER2
O '>'
O E 1 MQCALL
O CNAME 10
O ' ended with reason code'
O RCODE 45
O E 1 MESSC
O 25 'Unable to connect to'
O ' queue manager'
O E 1 MESS
O 25 'Unable to open queue'
O ' for input'
O E 1 NOMESS
O 25 'no more messages'
**