jmeter icon indicating copy to clipboard operation
jmeter copied to clipboard

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

Open mehrdad2000 opened this issue 2 years ago • 0 comments

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

mehrdad2000 avatar Nov 08 '23 15:11 mehrdad2000