springboot+jmstemplate+ibmq的应用程序测试

9lowa7mx  于 2021-07-09  发布在  Java
关注(0)|答案(0)|浏览(133)

我对springboot和ibmq还不熟悉。
我有一个应用程序,其中我必须从mq读取一条消息,然后使用spring boot将其写入一个文件。
我想使用JUnit5在单元测试中测试这个功能。我该如何在代码方面做到这一点。
代码如下:
filemqapplication.java文件

import java.io.File;
import java.io.FileWriter;
import java.util.Locale;
import java.util.Random;

import javax.jms.ConnectionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

@SpringBootApplication
@EnableJms
@EnableScheduling

public class FilemqApplication {
    @Autowired
    private JmsTemplate jmsTemplate;

    @Autowired
    private ResourceBundleMessageSource resourceBundleMessageSource;

    @Value("${mq.queueName}")
    private String queueName;

    @Value("${mq.hostName}")
    private String hostName;

    @Value("${mq.port}")
    private int port;

    @Value("${mq.queueManager}")
    private String queueManager;

    @Value("${mq.channel}")
    private String channelName;

    @Value("${dir.location}")
    private String directoryLocation;

    @Value("${mq.receiveTimeout}")
    private String receiveTimeout;

    Logger logger = LoggerFactory.getLogger(EquensfilemqApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(EquensfilemqApplication.class, args);
    }

    @Scheduled(fixedDelayString = "${delay.between.queueRead}")
    void receiveMessage() {
        try {

            String[] properties = getProperties();
            MQConnectionFactory connectionFactory = (MQConnectionFactory) jmsTemplate.getConnectionFactory();
            connectionFactory.setupConnectionProperties(properties);
            jmsTemplate.setConnectionFactory(connectionFactory);
            jmsTemplate.setReceiveTimeout (Integer.parseInt(receiveTimeout));

            Object receivedMessage = jmsTemplate.receiveAndConvert (queueName);
            if (receivedMessage == null)
            {
                return;
            }

            do
            {
                logger.trace (resourceBundleMessageSource.getMessage("receiving", null, Locale.getDefault()));
                logger.trace (receivedMessage.toString());
                logger.trace ("]");

                // Log the receiving message
                String message = receivedMessage.toString();
                logger.trace (resourceBundleMessageSource.getMessage("extracting", null, Locale.getDefault()));

                String[] headerAndContent = extractHeaderAndContent (message);
                if (headerAndContent != null)
                {
                    String fileName = extractFileName(headerAndContent[0]);

                    if (fileName != null)
                    {
                        File file = new File (directoryLocation + fileName);
                        if (file.exists())
                        {
                            Object[] args = {directoryLocation + fileName};
                            logger.error (resourceBundleMessageSource.getMessage("fileExists", args, Locale.getDefault()));
                        }
                        else
                        {
                            FileWriter writer = new FileWriter (directoryLocation + fileName);
                            Object[] args = {directoryLocation + fileName};
                            logger.trace (resourceBundleMessageSource.getMessage("writingIntoFile", args, Locale.getDefault()));
                            writer.write (headerAndContent[1]);
                            writer.close();
                        }
                    }
                    else
                    {
                        logger.error (resourceBundleMessageSource.getMessage("couldNotExtractFileName", null, Locale.getDefault()));
                    }
                }
                else
                {

                    logger.error (resourceBundleMessageSource.getMessage("couldNotExtractMessageHeader", null, Locale.getDefault()));
                }

                receivedMessage = jmsTemplate.receiveAndConvert (queueName);

            }
            while (receivedMessage != null);

        } catch (Exception ex) {
            // Log error message
            ex.printStackTrace();
            logger.error (ex.toString());
        }
    }

    @Bean
    public JmsTemplate getJmsTemplate() {
        JmsTemplate jmsTemplate = new JmsTemplate();
        ConnectionFactory connectionFactory = new MQConnectionFactory();
        jmsTemplate.setConnectionFactory(connectionFactory);
        return jmsTemplate;
    }

    private String extractFileName(String completeMessage)
    {
        int startIndex = completeMessage.indexOf("FileName>");
        if (startIndex != -1)
        {
            int endIndex = completeMessage.indexOf("FileName>", startIndex + 8);
            if (endIndex == -1)
            {
                return null;
            }
            String fileName = completeMessage.substring(startIndex + 9, endIndex - 3);
            return fileName;
        }
        return null;
    }

    private String[] extractHeaderAndContent (String completeMessage)
    {
        int startIndex = completeMessage.indexOf("<IF_EWL>");
        int endIndex = completeMessage.indexOf("</IF_EWL>");
        if (startIndex != -1 && endIndex != -1)
        {
            String header = completeMessage.substring(startIndex, endIndex + 9);
            Object[] args = {header};
            logger.trace (resourceBundleMessageSource.getMessage("header", args, Locale.getDefault()));

            String content = completeMessage.substring(endIndex + 9);
            Object[] args1 = {content};
            logger.trace (resourceBundleMessageSource.getMessage("content", args1, Locale.getDefault()));

            String[] headerAndContent = new String[2];
            headerAndContent[0] = header;
            headerAndContent[1] = content;
            return headerAndContent;
        }
        return null;
    }

    private String[] getProperties ()
    {
        String[] properties = new String[4];
        properties[0] = "HostName:" + hostName;
        properties[1] = "Port:" + port;
        properties[2] = "QueueManager:" + queueManager;
        properties[3] = "ChannelName:" + channelName;
        return properties;
    }

}

mqconnectionfactory.java文件

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.ibm.mq.jms.MQQueueConnectionFactory;
import com.ibm.msg.client.wmq.WMQConstants;

@Component
public class MQConnectionFactory extends MQQueueConnectionFactory {

    @Value("${mq.queueName}")
    private String queueName;

    @Value("${mq.hostName}")
    private String hostName;

    @Value("${mq.port}")
    private int port;

    @Value("${mq.queueManager}")
    private String queueManager;

    @Value("${mq.channel}")
    private String channelName;

    public void setupConnectionProperties(String[] properties) throws Exception {
        for (int i = 0; i < properties.length; i++) {
            if (properties[i].startsWith("HostName:")) {
                setHostName(properties[i].substring(9));
            }
            if (properties[i].startsWith("Port:")) {
                setPort(Integer.parseInt(properties[i].substring(5)));
            }
            if (properties[i].startsWith("QueueManager:")) {
                setQueueManager(properties[i].substring(13));
            }
            if (properties[i].startsWith("ChannelName:")) {
                setChannel(properties[i].substring(12));
            }

        }

        setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT);
    }

}

应用程序属性

mq.queueManager=IPMT_QM
mq.channel=SYSTEM.ADMIN.SVRCONN
mq.hostName=127.0.0.1
mq.port=1414
mq.queueName=stet.q11
mq.receiveTimeout=3000
delay.between.queueRead = 6000
dir.location=/home/uppdev/temp/
logging.file.path=/home/uppdev/temp/
logging.level.com.aciworldwide.equensfilemq=ERROR

消息.属性

sending= Sending....
receiving= Receiving [
extracting = Extracting header and content.
fileExists = File with name : ["{0}"] already exists. Ignoring message.
writingIntoFile=Writing to File : "{0}"
couldNotExtractFileName = Could not extract file name.
couldNotExtractMessageHeader = Could not extract message header and file content.
header = Header ["{0}"]
content = Content ["{0}"]

谢谢您

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题