Total Pageviews

2017/12/08

[Java] How to write text message to IBM MQ?

Problem
I am using IBM MQ as my queue service.
How to write text message into IBM MQ by Java?


How-To

Here has sample code:
 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
package com.xxx.mql.service;

import java.io.IOException;
import java.util.Enumeration;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.QueueBrowser;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.commons.lang.StringUtils;

import com.ibm.mq.jms.MQQueueConnection;
import com.ibm.mq.jms.MQQueueConnectionFactory;
import com.ibm.mq.jms.MQQueueReceiver;
import com.ibm.mq.jms.MQQueueSender;
import com.ibm.mq.jms.MQQueueSession;
import com.tdcc.mql.enumeration.MQParamKeyType;
import com.tdcc.mql.utils.MqlUtils;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class TestMQService {

    private static TestMQService mqServuce;
    
    private static String mqHost = MQParamKeyType.MQ_HOST.getSystemValue();
    private static String mqChannel = MQParamKeyType.MQ_CHANNEL.getSystemValue();
    private static String mqUser = MQParamKeyType.MQ_USER.getSystemValue();
    private static String mqPwd = MQParamKeyType.MQ_PASSWORD.getSystemValue();
    private static String mqQueueMgr = MQParamKeyType.MQ_QUEUEMGR.getSystemValue();
    private static String mqPort = MQParamKeyType.MQ_PORT.getSystemValue();

    public static TestMQService getInstance() {
        if (mqServuce == null) {
            mqServuce = new TestMQService();
            return mqServuce;
        } else {
            return mqServuce;
        }
    }
    
    /**
     * Send Message to MQ.
     * 
     * @param queueName
     * @param message
     * @throws JMSException
     */
    public void writeMessageToMQ(String queueName, String message) throws JMSException {
        MQQueueConnectionFactory connFactory = new MQQueueConnectionFactory();
        MQQueueSender sender = null;
        MQQueueSession session = null;
        MQQueueConnection connection = null;
        
        try {
            connFactory.setHostName(mqHost);
            connFactory.setPort(Integer.parseInt(mqPort));
            connFactory.setTransportType(1);
            connFactory.setQueueManager(mqQueueMgr);
            connFactory.setChannel(mqChannel);

            connection = (MQQueueConnection) connFactory.createQueueConnection();
            session = (MQQueueSession) connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
            Queue queue = session.createQueue(queueName);
            
            sender = (MQQueueSender) session.createSender(queue);
            
            connection.start();
            
            TextMessage textMsg = session.createTextMessage();
            textMsg.setText(message);
            
            sender.send(textMsg);
            
        } catch (JMSException e) {
            log.error(MqlUtils.toStackTrace(e));
        } finally {
            if (sender != null) {
                sender.close();
            }
            if (session != null) {
                session.close();
            }
            if (connection != null) {
                connection.close();
            }
        }
    }
}    




2017/12/07

[Java] ClassCastException : cannot cast javax.jms.Message to javax.jms.TextMessage

Problem
I get ClassCastException when I read IBM MQ queue message by Java.
The code snippet looks like:
  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
package com.xxx.mql.service;

import java.io.IOException;
import java.util.Enumeration;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.QueueBrowser;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.commons.lang.StringUtils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.common.base.Strings;
import com.ibm.mq.jms.MQQueueConnection;
import com.ibm.mq.jms.MQQueueConnectionFactory;
import com.ibm.mq.jms.MQQueueReceiver;
import com.ibm.mq.jms.MQQueueSession;
import com.tdcc.common.type.MQInfoEnum;
import com.tdcc.mql.utils.MqlUtils;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class TestMQService {

    private static TestMQService mqServuce;

    private static String mqHost = MQInfoEnum.MQ_HOST.getValue();
    private static String mqChannel = MQInfoEnum.MQ_CHANNEL.getValue();
    private static String mqUser = MQInfoEnum.MQ_USER.getValue();
    private static String mqPwd = "";
    private static String mqQueueMgr = MQInfoEnum.MQ_QUEUEMGR.getValue();

    public static TestMQService getInstance() {
        if (mqServuce == null) {
            mqServuce = new TestMQService();
            return mqServuce;
        } else {
            return mqServuce;
        }
    }

    /**
     * 透過 JMS 去取得 queue message.
     * 
     * @param queueName
     * @return
     * @throws JMSException
     */
    public String readMessageByJMS(String queueName) throws JMSException {
        MQQueueConnectionFactory connFactory = new MQQueueConnectionFactory();
        MQQueueReceiver receiver = null;
        MQQueueSession session = null;
        MQQueueConnection connection = null;
        String message = "";
        try {
            // Config
            connFactory.setHostName(mqHost);
            connFactory.setPort(1414);
            connFactory.setTransportType(1);
            connFactory.setQueueManager(mqQueueMgr);
            connFactory.setChannel(mqChannel);

            connection = (MQQueueConnection) connFactory.createQueueConnection();
            session = (MQQueueSession) connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
            Queue queue = session.createQueue(queueName);
            receiver = (MQQueueReceiver) session.createReceiver(queue);

            connection.start();

            if (receiver != null) {
                Message msg = receiver.receiveNoWait();
                if (msg == null) {
                    message = "";
                } else {
                    TextMessage textMsg = (TextMessage) msg;
                    message = StringUtils.defaultIfBlank(textMsg.getText(), "");
                }
            }

        } catch (JMSException e) {
            log.error(MqlUtils.toStackTrace(e));
        } finally {
            if (receiver != null) {
                receiver.close();
            }
            if (session != null) {
                session.close();
            }
            if (connection != null) {
                connection.close();
            }
        }
        log.info("[readMessageByJMS] message = " + message);
        return message;
    }
}


How-to
According to original requirement, the queue message should be text format. 

Different content format will use different interface:



But actually it may has non-text format in IBM MQ, so I need to check its class type before do cast. 


The updated code snippet are as bellows:

  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
package com.xxx.mql.service;

import java.io.IOException;
import java.util.Enumeration;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.QueueBrowser;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.commons.lang.StringUtils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.common.base.Strings;
import com.ibm.mq.jms.MQQueueConnection;
import com.ibm.mq.jms.MQQueueConnectionFactory;
import com.ibm.mq.jms.MQQueueReceiver;
import com.ibm.mq.jms.MQQueueSession;
import com.tdcc.common.type.MQInfoEnum;
import com.tdcc.mql.utils.MqlUtils;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class TestMQService {

    private static TestMQService mqServuce;

    private static String mqHost = MQInfoEnum.MQ_HOST.getValue();
    private static String mqChannel = MQInfoEnum.MQ_CHANNEL.getValue();
    private static String mqUser = MQInfoEnum.MQ_USER.getValue();
    private static String mqPwd = "";
    private static String mqQueueMgr = MQInfoEnum.MQ_QUEUEMGR.getValue();

    public static TestMQService getInstance() {
        if (mqServuce == null) {
            mqServuce = new TestMQService();
            return mqServuce;
        } else {
            return mqServuce;
        }
    }

    /**
     * 透過 JMS 去取得 queue message.
     * 
     * @param queueName
     * @return
     * @throws JMSException
     */
    public String readMessageByJMS(String queueName) throws JMSException {
        MQQueueConnectionFactory connFactory = new MQQueueConnectionFactory();
        MQQueueReceiver receiver = null;
        MQQueueSession session = null;
        MQQueueConnection connection = null;
        String message = "";
        try {
            // Config
            connFactory.setHostName(mqHost);
            connFactory.setPort(1414);
            connFactory.setTransportType(1);
            connFactory.setQueueManager(mqQueueMgr);
            connFactory.setChannel(mqChannel);

            connection = (MQQueueConnection) connFactory.createQueueConnection();
            session = (MQQueueSession) connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
            Queue queue = session.createQueue(queueName);
            receiver = (MQQueueReceiver) session.createReceiver(queue);

            connection.start();

            if (receiver != null) {
                Message msg = receiver.receiveNoWait();
                if (msg == null) {
                    message = "";
                } else {
                    if (msg instanceof TextMessage) {
                        TextMessage textMsg = (TextMessage) msg;
                        message = StringUtils.defaultIfBlank(textMsg.getText(), "");
                    } else {
                        String error = "*****不是文字訊息,跳過此筆訊息,class type = " + msg.getClass().toString();
                        log.error(error);
                    }
                }
            }

        } catch (JMSException e) {
            log.error(MqlUtils.toStackTrace(e));
        } finally {
            if (receiver != null) {
                receiver.close();
            }
            if (session != null) {
                session.close();
            }
            if (connection != null) {
                connection.close();
            }
        }
        log.info("[readMessageByJMS] message = " + message);
        return message;
    }
}


Reference
[1] https://docs.oracle.com/javaee/6/api/javax/jms/Message.html

2017/12/06

[Java] How to read message from IBM MQ?

Problem
I am using IBM MQ as my queue service.
How to read message (text format) from IBM MQ by Java?


How-To

Here has the sample code:
  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
package com.xxx.mql.service;

import java.io.IOException;
import java.util.Enumeration;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.QueueBrowser;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.commons.lang.StringUtils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.common.base.Strings;
import com.ibm.mq.jms.MQQueueConnection;
import com.ibm.mq.jms.MQQueueConnectionFactory;
import com.ibm.mq.jms.MQQueueReceiver;
import com.ibm.mq.jms.MQQueueSession;
import com.tdcc.common.type.MQInfoEnum;
import com.tdcc.mql.utils.MqlUtils;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class TestMQService {

    private static TestMQService mqServuce;

    private static String mqHost = MQInfoEnum.MQ_HOST.getValue();
    private static String mqChannel = MQInfoEnum.MQ_CHANNEL.getValue();
    private static String mqUser = MQInfoEnum.MQ_USER.getValue();
    private static String mqPwd = "";
    private static String mqQueueMgr = MQInfoEnum.MQ_QUEUEMGR.getValue();

    public static TestMQService getInstance() {
        if (mqServuce == null) {
            mqServuce = new TestMQService();
            return mqServuce;
        } else {
            return mqServuce;
        }
    }

    /**
     * 取得 queue message.
     * 
     * @param queueName
     * @return
     * @throws JMSException
     */
    public String readMessage(String queueName) throws JMSException {
        MQQueueConnectionFactory connFactory = new MQQueueConnectionFactory();
        MQQueueReceiver receiver = null;
        MQQueueSession session = null;
        MQQueueConnection connection = null;
        String message = "";
        try {
            // Config
            connFactory.setHostName(mqHost);
            connFactory.setPort(1414);
            connFactory.setTransportType(1);
            connFactory.setQueueManager(mqQueueMgr);
            connFactory.setChannel(mqChannel);

            connection = (MQQueueConnection) connFactory.createQueueConnection();
            session = (MQQueueSession) connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
            Queue queue = session.createQueue(queueName);
            receiver = (MQQueueReceiver) session.createReceiver(queue);

            connection.start();

            if (receiver != null) {
                Message msg = receiver.receiveNoWait();
                if (msg == null) {
                    message = "";
                } else {
                    if (msg instanceof TextMessage) {
                        TextMessage textMsg = (TextMessage) msg;
                        message = StringUtils.defaultIfBlank(textMsg.getText(), "");
                    } else {
                        String error = "*****不是文字訊息,跳過此筆訊息,class type = " + msg.getClass().toString();
                        log.error(error);
                    }
                }
            }

        } catch (JMSException e) {
            log.error(MqlUtils.toStackTrace(e));
        } finally {
            if (receiver != null) {
                receiver.close();
            }
            if (session != null) {
                session.close();
            }
            if (connection != null) {
                connection.close();
            }
        }
        log.info("[readMessageByJMS] message = " + message);
        return message;
    }
}

2017/12/05

[Java] How to count the number of message in IBM MQ?

Problem
I am using IBM MQ as my queue service. 
How to count the number of message in IBM MQ?


How-to

Here has the sample code:
 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
package com.xxx.mql.service;

import java.io.IOException;
import java.util.Enumeration;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.QueueBrowser;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.commons.lang.StringUtils;

import com.fasterxml.jackson.core.JsonProcessingException;

import com.google.common.base.Strings;

import com.ibm.mq.jms.MQQueueConnection;
import com.ibm.mq.jms.MQQueueConnectionFactory;
import com.ibm.mq.jms.MQQueueReceiver;
import com.ibm.mq.jms.MQQueueSession;
import com.tdcc.common.type.MQInfoEnum;
import com.tdcc.mql.utils.MqlUtils;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class TestMQService {

    private static TestMQService mqServuce;

    private static String mqHost = MQInfoEnum.MQ_HOST.getValue();
    private static String mqChannel = MQInfoEnum.MQ_CHANNEL.getValue();
    private static String mqUser = MQInfoEnum.MQ_USER.getValue();
    private static String mqPwd = "";
    private static String mqQueueMgr = MQInfoEnum.MQ_QUEUEMGR.getValue();

    public static TestMQService getInstance() {
        if (mqServuce == null) {
            mqServuce = new TestMQService();
            return mqServuce;
        } else {
            return mqServuce;
        }
    }

    /**
     * 透過 JMS 去查詢 queue 裡面有幾筆訊息.
     * 
     * @param queueName
     * @return
     * @throws JMSException
     */
    public int getNumOfMsg(String queueName) throws JMSException {
        MQQueueConnectionFactory connFactory = new MQQueueConnectionFactory();
        MQQueueReceiver receiver = null;
        MQQueueSession session = null;
        MQQueueConnection connection = null;
        int numOfMsg = 0;
        try {
            // Config
            connFactory.setHostName(mqHost);
            connFactory.setPort(1414);
            connFactory.setTransportType(1);
            connFactory.setQueueManager(mqQueueMgr);
            connFactory.setChannel(mqChannel);

            connection = (MQQueueConnection) connFactory.createQueueConnection();
            session = (MQQueueSession) connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
            Queue queue = session.createQueue(queueName);
            receiver = (MQQueueReceiver) session.createReceiver(queue);

            connection.start();

            QueueBrowser queueBrowser = session.createBrowser((Queue) queue);
            Enumeration msgEnum = queueBrowser.getEnumeration();
            while (msgEnum.hasMoreElements()) {
                Message message = (Message) msgEnum.nextElement();
                numOfMsg++;
            }

        } catch (JMSException e) {
            log.error(MqlUtils.toStackTrace(e));
        } finally {
            if (receiver != null) {
                receiver.close();
            }
            if (session != null) {
                session.close();
            }
            if (connection != null) {
                connection.close();
            }
        }
        log.info("[getNumOfMsgByJMS] numOfMsg = " + numOfMsg);
        return numOfMsg;
    }
}    

2017/12/04

Illegal unquoted character ((CTRL-CHAR, code 26)): has to be escaped using backslash to be included in string.

Problem
When I try to convert JSON string to Java bean via Jackson Framework, it throws an exception as bellows: 
Illegal unquoted character ((CTRL-CHAR, code 26)): has to be escaped using backslash to be included in string.

The code snippet is:
 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
package albert.practice.json;

import java.io.IOException;

import org.codehaus.jackson.JsonParser.Feature;
import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.map.ObjectMapper;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

public class JSONUtils {

    public Response convertJsonToObject(String json) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        Response response = null;
        try {
            response = mapper.readValue(json, Response.class);
        } catch (IOException e) {
            throw e;
        }
        return response;
    }

    @Data
    @ToString
    @NoArgsConstructor
    @AllArgsConstructor
    public static class Response {
        @JsonProperty("Type")
        private String type;
        @JsonProperty("Status")
        private String status;
    }
}




How-To
Owing to the JSON has some CTRL-CHAR, so Jackson Framework cannot covert to Java bean correctly. 
You need to configure OjbectMapper to accept backslash escaping character. The updated code snippet looks like:
 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
package albert.practice.json;

import java.io.IOException;

import org.codehaus.jackson.JsonParser.Feature;
import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.map.ObjectMapper;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

public class JSONUtils {

    public Response convertJsonToObject(String json) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER, true);
        Response response = null;
        try {
            response = mapper.readValue(json, Response.class);
        } catch (IOException e) {
            throw e;
        }
        return response;
    }

    @Data
    @ToString
    @NoArgsConstructor
    @AllArgsConstructor
    public static class Response {
        @JsonProperty("Type")
        private String type;
        @JsonProperty("Status")
        private String status;
    }
}





2017/12/03

[Windows] 執行 Windows Update 發生錯誤的解決方式

有時候在執行 Windows Update 時,會發生莫名的錯誤,如下:


以下是解決方式,步驟如下:


(1) 停止 Windows Update 服務


(2) 刪除以下兩個目錄下的所有檔案




(3) 啟動 Windows Update 服務


(4) 重新檢查更新