Behaviour Difference: Intellij vs Eclipse

559 Views Asked by At

I am trying to import and run existing and working Java code into Intellij but I ran into few problems and this causes me to write this question here:

My Storm topology (java) code is reading data from Websphere MQ and the recieved message which is a byte-stream and parsed by a "Message Parser" project; it parses these bytestream "String" and generates meaningful message based on some rules.

When I run it in Eclipse it works without any issue, but in Intellij it shows me problem related to message parser. I senses it as encoding issue and tried to:

  • Change the code file encoding to UTF-8
  • Change line separator to become identical (Unix)

But this did not lead me to resolution.

Because you all have good expertise in Java IDEs, hence I expect you must have faced the IDE's compatibility issues for Java code many of the times. Kindly let me know if there is any way to resolve this problem.

The code where the problem is occuring is given below:

public void execute(Tuple input) {

    String strMessage = null;
    Message posMsg = null;
                Object jmsMsg = input.getValueByField(FieldEnum.FIELD_MESSAGE
                .getFieldName());
        posMsg = ((JMSMessage) jmsMsg);
        strMessage = convertStreamToString(posMsg);

        System.out.println("Message recieved from MQ : "+ strMessage);

        @SuppressWarnings("rawtypes")
        Map parsedSegments = MessageParser.instance().parseMessage(
                strMessage);

        @SuppressWarnings("serial")
        Type nposMessageType = new TypeToken<Map<String, Map<String, String>>>() {
        }.getType();
        String segmentsJson = gson.toJson(parsedSegments, nposMessageType);

        System.out.println(" testing the messages "+segmentsJson);
MessageDetail MessageDetail = new MessageDetail(
                Constant.TOPOLOGY_NAME, segmentsJson);
        this.outputCollector.emit(Constant.STREAM_MSG_PARSER_SUCCESS,
                new Values(MessageDetail));
        this.outputCollector.ack(input);
}


        /**
 * Convert stream to string
 * 
 * @param jmsMsg
 * @return
 * @throws Exception
 */
private static String convertStreamToString(final Message jmsMsg) throws Exception {
    String stringMessage = "";
    BytesMessage bMsg = (BytesMessage) jmsMsg;
    byte[] buffer = new byte[40620];
    int byteRead;
    ByteArrayOutputStream bout = new java.io.ByteArrayOutputStream();
    while ((byteRead = bMsg.readBytes(buffer)) != -1) {
        bout.write(buffer, 0, byteRead);
    }
    bout.flush();
    stringMessage = new String(bout.toByteArray());
    bout.close();
    return stringMessage;
}
1

There are 1 best solutions below

5
On

Your code is to blame. You're using

new String(bout.toByteArray());

to convert bytes to characters. That uses the platform default encoding. It's different when running from Eclipse than it is when running with IntelliJ, because IntelliJ, if I'm not mistaken, passes a JVM option forcing the default encoding to UTF-8.

But that's not an IntelliJ problem. Your code would fail the same way if you were running your program out of any IDE, on a platform whose default encoding is UTF-8 (Linuxes and Macs, for example).

Every time you transform bytes into chars and vice-versa, make sure to specify an encoding explicitely. If MQ is generating the message, read its documentation to know which encoding it uses, and make sure to use the same one. For example, if it's ISO-8859-1, use

new String(bytes, StandardCharsets.ISO_8859_1)