Send test message to java app using jmeter (jms,mq) plugin

87 views
Skip to first unread message

Mehrdad

unread,
Nov 8, 2023, 10:31:14 AM11/8/23
to jmeter-plugins

Hi
I try  to use jms and mq plugin of jmeter to send message to my java app, like this:

Jmeter > mq > java-app



On java app got this error:


2023-07-15 15:46:49,538 ERROR CUS-10636 [JmsListener] FATAL: exception in read message on listener: com.ibm.jms.JMSBytesMessage cannot be cast to javax.jms.TextMessage



https://github.com/JoseLuisSR/mqmeter/issues/16





Here is the code of java app:

package mylib.platform.service.endpoint.jms;

import mylib.common.boot.SystemVerifier;
import mylib.common.exception.CommonExceptionCode;
import mylib.common.exception.ExceptionLogger;
import mylib.common.exception.MYRuntimeException;
import mylib.common.monitoring.UniqueID;
import mylib.common.platform.Constants;
import mylib.common.platform.message.EndpointMessage;
import mylib.common.util.str.StringUtility;
import mylib.platform.domain.endpoint.Endpoint;
import mylib.platform.domain.endpoint.Inpoint;
import mylib.platform.domain.endpoint.Outpoint;
import mylib.platform.service.core.MYCore;
import mylib.platform.service.endpoint.EndpointService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.jms.*;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;

@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public class JmsListener implements MessageListener {

    private static final Logger LOGGER = LoggerFactory.getLogger(JmsListener.class);
    private static AtomicInteger count = new AtomicInteger(0);

    @EJB
    private SystemVerifier systemVerifier;

    @EJB
    private MYCore myCore;

    @EJB
    private EndpointService endpointService;

    @Resource
    private String defaultName = "";

    private EndpointMessage extractEndpointMessage(Message message, Inpoint inpoint, long receiveTime) throws JMSException, UnsupportedEncodingException {
        String text;
        if (inpoint == null)
            throw new MYRuntimeException(CommonExceptionCode.NULL_POINTER, "JmsListener", "inpoint");

        if (inpoint.getMessageType() == Endpoint.MessageType.BYTE) {
            BytesMessage byteMessage = (BytesMessage) message;
            byte[] byteData = new byte[(int) byteMessage.getBodyLength()];
            byteMessage.readBytes(byteData);
            text = new String(byteData, inpoint.getEncoding());
        } else {
            text = ((TextMessage) message).getText();
        }

        Outpoint replyEndpoint = endpointService.getOutpoint(inpoint.getReplyEndpoint());

        Date arrivalTime = new Date(message.getJMSTimestamp());
        int expirationTime = inpoint.getTimeToLive();

        EndpointMessage em = new EndpointMessage(text, inpoint.getName(), replyEndpoint.getName(), arrivalTime, expirationTime);
        em.setReceiveTime(receiveTime);
        em.setCookie(extractCookie(message));

        if (!StringUtility.isNullOrEmpty(message.getJMSCorrelationID()))
            em.setCorrelationID(message.getJMSCorrelationID());
        else if (message.getJMSReplyTo() != null)
            em.setCorrelationID(message.getJMSMessageID());

        return em;
    }

    private String extractCookie(Message message) throws JMSException {
        Object obj = message.getObjectProperty(Constants.MY_COOKIE);
        if (obj == null)
            return null;
        if (!(obj instanceof String)) {
            throw new RuntimeException("Invalid Cookie");
        }
        return (String) obj;
    }

    private Inpoint extractInpoint(Message message) throws JMSException {

        String source = message.getStringProperty(Constants.SOURCE_NAME);
        if (StringUtility.isNullOrEmpty(source)) {
           
            if (message.getJMSDestination() != null) {
                String destination = message.getJMSDestination().toString();
                source = destination.substring(destination.lastIndexOf('/') + 1);
            } else {
                source = defaultName;
            }
        }

        Endpoint result = endpointService.getEndpointByQueueName(source);
        if (result == null || result instanceof Outpoint)
            throw new MYRuntimeException(CommonExceptionCode.INVALID_ENDPOINT_NAME, source);
        return (Inpoint) result;
    }

    @PostConstruct
    public void init() {
        if (systemVerifier != null)
            if (!systemVerifier.isReadyToStart())
                LOGGER.error("NEVER HAPPENS");
            else
                LOGGER.debug("################### MDB {} is ready to start...", count.incrementAndGet());
    }

    @Override
    public void onMessage(Message message) {
        EndpointMessage epMsg;
        try {
            //TODO check PCI constraint
            if (message == null)
                throw new MYRuntimeException(CommonExceptionCode.NULL_POINTER, "JmsListener", "message");

            long receiveTime = System.currentTimeMillis();

            Inpoint inpoint = extractInpoint(message);
            MDC.put("source", inpoint.getName() + "-" + UniqueID.getNewID());

            epMsg = extractEndpointMessage(message, inpoint, receiveTime);

            String log = inpoint.isLogMessage() ? epMsg.getData() : StringUtility.getStarred(epMsg.getData(), 30);
            LOGGER.info("Receive Message[{}] Q[{}] CID[{}] Cookie[{}]", log, inpoint.getQueueName(), epMsg.getCorrelationID(), epMsg.getCookie());

            myCore.processEndpointMessage(epMsg);

        } catch (Exception e) {
            LOGGER.error("FATAL: exception in read message on listener: {}", e.getMessage());
            ExceptionLogger.log("FATAL: exception in read message on listener", e);
        } finally {
            MDC.clear();
        }
    }


}



Any Idea?

Thanks

Dmitri T

unread,
Nov 9, 2023, 3:41:15 AM11/9/23
to jmeter-plugins
The plugin you're using seems to be sending a text message while your app expects it to be a bytes message.

I fail to see how you can configure the plugin to send the bytes message, I would suggest using JSR223 Sampler with Groovy language to construct the bytes message and send it to the queue.

Code snippets could be found:

Reply all
Reply to author
Forward
0 new messages