WebSphere MQ For JAVA编程实例----实现MQ trigger--样例

MQTrigger.java源码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import java.io.*;
import java.lang.*;
import com.ibm.mq.*;
 
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);
    }
 
}

 

 

JavaTrigger.java源码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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;
 
public class JavaTrigger
{
 
 
    private MQQueueManager qMgr;
 
    public static void main (String args[]) throws IOException
    {
        if (args.length < 1)
        {
            System.out.println("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)
                    {
                        System.out.println("Message: " + msg.substring(0,20));
                    }
                    else
                    {
                        System.out.println("Message: " + msg);
                    }
 
 
                }
                catch (MQException mqEx)
                {
                    rc = mqEx.reasonCode;
                    if (rc != MQException.MQRC_NO_MSG_AVAILABLE)
                    {
                        System.out.println(" PUT Message failed with rc = "
                        + rc);
                    }
                }
                catch (Exception ex)
                {
                    System.out.println("Generic exception: " + ex);
                    rc = 1;
                }
 
            } while (rc == 0);
 
 
            /**********************************************************/
            /* Cleanup MQ resources prior to exiting. */
            /**********************************************************/
            triggerQueue.close();
            qMgr.disconnect();
        }
 
        catch (MQException mqEx)
        {
            System.out.println("MQ failed with completion code = "
            + mqEx.completionCode
            + " and reason code = " + mqEx.reasonCode);
        }
    }
 
}

 

 

转载:http://ganquan.itpub.net/post/8445/290844

posted on   jay.windows  阅读(1775)  评论(0编辑  收藏  举报

编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!

导航

< 2012年9月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 1 2 3 4 5 6

统计

点击右上角即可分享
微信分享提示