基于WebSphere MQ的MQ trigger实例

        WebSphere MQ的配置请参考配置IBM WEBSPHERE MQ触发器中的方法二,唯一要改的是定义进程,定义进程改为如下:

DEFINE PROCESS('JAVA.PROCESS') REPLACE +
DESCR('Process to test triggering') +
APPLICID('java -classpath E:/MQTrigger/MQTrigger.jar com.bijian.study.JavaTrigger')

        由java -classpath E:/MQTrigger/MQTrigger.jar com.bijian.study.JavaTrigger可知,我将如下工程代码导出为MQTrigger.jar放到了E盘的MQTrigger目录下。

JavaTrigger.java

package com.bijian.study;

import java.io.IOException;

import com.ibm.mq.MQC;
import com.ibm.mq.MQException;
import com.ibm.mq.MQGetMessageOptions;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQQueue;
import com.ibm.mq.MQQueueManager;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class JavaTrigger {

    private static Logger logger = LogManager.getLogger(JavaTrigger.class.getName());
    
    private MQQueueManager qMgr;

    public static void main(String args[]) throws IOException {
        if (args.length < 1) {
            logger.info("This must be a triggered application"); 
        } else {
            JavaTrigger jt = new JavaTrigger();
            jt.start(args);
        }
        System.exit(0);
    }

    public void start(String args[]) {
        try {

            MQException.log = null;

            /******************************************************/
            /* Create a MQTrigger class object to read the MQTMC2 */
            /* structure into the correct attribute. */
            /******************************************************/
            MQTrigger tmc = new MQTrigger(args[0]);

            /******************************************************/
            /* Connect to the queue manager identified by the */
            /* trigger. */
            /******************************************************/

            qMgr = new MQQueueManager(tmc.getQueueManagerName());

            /******************************************************/
            /* Open the queue identified by the trigger. */
            /******************************************************/

            int openOptions = MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_FAIL_IF_QUIESCING;

            MQQueue triggerQueue = qMgr.accessQueue(tmc.getQueueName(), openOptions, null, null, null);

            /******************************************************/
            /* Set up our options to get the first message */
            /* Wait 5 seconds to be cetain all messages are */
            /* processed. */
            /******************************************************/
            MQGetMessageOptions gmo = new MQGetMessageOptions();
            gmo.options = MQC.MQGMO_WAIT | MQC.MQGMO_CONVERT;
            gmo.waitInterval = 5000;

            MQMessage triggerMessage = new MQMessage();

            /*****************************************************/
            /* Read each message from the queue until there are */
            /* no more messages to get. */
            /*****************************************************/
            long rc = 0;
            do {
                rc = 0;
                try {
                    /***********************************************/
                    /* Set the messageId and correlationId to none */
                    /* to get all messages with no message */
                    /* selection. */
                    /***********************************************/
                    triggerMessage.clearMessage();
                    triggerMessage.correlationId = MQC.MQCI_NONE;
                    triggerMessage.messageId = MQC.MQMI_NONE;

                    triggerQueue.get(triggerMessage, gmo);
                    String msg = triggerMessage.readString(triggerMessage.getMessageLength());

                    /***********************************************/
                    /* Insert business logic for the message here. */
                    /* For this sample, echo the first 20 */
                    /* characters of the message. */
                    /***********************************************/
                    if (msg.length() > 20) {
                        logger.info("Message: " + msg.substring(0, 20)); 
                    } else {
                        logger.info("Message: " + msg); 
                    }

                } catch (MQException mqEx) {
                    rc = mqEx.reasonCode;
                    if (rc != MQException.MQRC_NO_MSG_AVAILABLE) {
                        logger.info(" PUT Message failed with rc = " + rc);
                    }
                } catch (Exception ex) {
                    logger.info("Generic exception: " + ex);
                    rc = 1;
                }

            } while (rc == 0);

            /**********************************************************/
            /* Cleanup MQ resources prior to exiting. */
            /**********************************************************/
            triggerQueue.close();
            qMgr.disconnect();
        }

        catch (MQException mqEx) {
            logger.info("MQ failed with completion code = " + mqEx.completionCode + " and reason code = "
                    + mqEx.reasonCode);
        }
    }
}

MQTrigger.java

package com.bijian.study;

class MQTrigger {

    private String structId;
    private String version;
    private String qName;
    private String processName;
    private String triggerData;
    private String applType;
    private String applId;
    private String envData;
    private String userData;
    private String qMgrName;

    /******************************************************/
    /* Constructor to parse the MQTMC2 stucture and set */
    /* the class attributes. */
    /* Values derived from field definitions given for */
    /* MQTMC2 in the WebSphere Application Programming */
    /* Reference. */
    /******************************************************/
    public MQTrigger(String tmcStruct) throws StringIndexOutOfBoundsException {

        structId = tmcStruct.substring(0, 3).trim();
        version = tmcStruct.substring(4, 8).trim();
        qName = tmcStruct.substring(8, 55).trim();
        processName = tmcStruct.substring(56, 103).trim();
        triggerData = tmcStruct.substring(104, 167).trim();
        applType = tmcStruct.substring(168, 171).trim();
        applId = tmcStruct.substring(172, 427).trim();
        envData = tmcStruct.substring(428, 555).trim();
        userData = tmcStruct.substring(556, 683).trim();
        qMgrName = tmcStruct.substring(684, 730).trim();

    }

    public String getStructId() {
        return (structId);
    }

    public String getVersion() {
        return (version);
    }

    public String getQueueName() {
        return (qName);
    }

    public String getProcessName() {
        return (processName);
    }

    public String getTriggerData() {
        return (triggerData);
    }

    public String getApplicationType() {
        return (applType);
    }

    public String getApplicationId() {
        return (applId);
    }

    public String getEnvironmentData() {
        return (envData);
    }

    public String getUserData() {
        return (userData);
    }

    public String getQueueManagerName() {
        return (qMgrName);
    }
}

运行效果:


 

参考文章:http://www.cnblogs.com/windows/archive/2012/09/25/2701613.html

猜你喜欢

转载自bijian1013.iteye.com/blog/2308754
今日推荐