JMSTemplate中createMessage的send Message方法中的延迟

时间:2018-08-21 21:43:37

标签: spring-boot jms ibm-mq spring-jms

我是MQ的新手,我们正在使用JMS + IBM MQ Framework进行春季启动。我们面临一个非常随机的问题,执行jmsQueueTemplate的send方法的延迟时间为5-15分钟。

随机延迟在// POINT 1和// POINT 2之间

try {
                logger.info("Calling Send Method ");
                //POINT 1
                jmsQueueTemplate.send(new MessageCreator() {

                    public Message createMessage(Session session) throws JMSException {
                        JMSTextMessage message = null;
                        try {
                        //POINT 2
                        logger.info("Session Object ........ " + session.toString());
                        logger.info("Session Status  - Is Session Transacted ?........ " + session.getTransacted());
                        logger.info("Session Status acknowledge mode........ " + session.getAcknowledgeMode());
                        logger.info("Ready to send Sample message........ ");
                        logger.info("Send ConnectionFactory is: " + jmsQueueTemplate.getConnectionFactory().toString());
                        logger.info("Send destination is: " + jmsQueueTemplate.getDefaultDestination());
                        logger.info("Reply destination is: " + destination);
                        logger.info("Sent message correlationId is: " + correlationId);
                        logger.info("##########################################################");

                        message = (JMSTextMessage) session.createTextMessage();

                        String fosXmlBatchRequest = XMLUtil.createBatchFosRequestXML(oBatchFosRequest);
                        message.setText(fosXmlBatchRequest);
                        message.setJMSCorrelationID(correlationId);
                        logger.info(transactionType + " : Sending message is:");
                        logger.info(message.getText());
                        logger.info("##########################################################");
                        message.setJMSReplyTo(destination);

                        } catch (Exception e) {
                            logger.info("Exception Occured :" + e.getMessage());
                        }
                        return message;
                    }
                }); 
            } catch(Exception e) {
                logger.info("Exception while Sending Message To FOS",e);
                logger.info(e.toString(),e);
                logger.error("Exception while Sending Message To FOS",e);
                logger.error(e.toString(),e);
            }

我们不处理会话对象,而让spring负责会话创建。我不确定延迟什么时候开始出现?我们失去会话对象吗?任何建议。

我们的弹簧配置如下。

import javax.jms.Destination;
import javax.jms.JMSException;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.listener.DefaultMessageListenerContainer;
import org.springframework.jms.support.converter.MappingJackson2MessageConverter;
import org.springframework.jms.support.converter.MessageConverter;
import org.springframework.jms.support.converter.MessageType;
import org.springframework.stereotype.Component;

import com.ibm.mq.jms.MQQueue;
import com.ibm.mq.jms.MQQueueConnectionFactory;


/**
 * 
 * @author krna
 * This class defines configuration for JMS Queues required for FOS update.
 */

@Configurable
@Component
public class JMSConfiguration {

    private final Logger log = LogManager.getLogger(JMSConfiguration.class);

    @Autowired
    MessageListenerReciever messageListenerReciever1;

    @Autowired
    MessageListenerReciever messageListenerReciever2;

    private String hostName1;
    private String hostName2;
    private String hostName3;
    private String writePort;
    private String readPort;
    private String channel;
    private String transportType;
    private String updateQueue;
    private String replyQueue;
    private String queueGateway;

    @Autowired
    JMSConfiguration(Environment environment){
        this.hostName1 = environment.getRequiredProperty("jms.cf.write.hostName1");
        this.hostName2 = environment.getRequiredProperty("jms.cf.read.hostName2");
        this.hostName3 = environment.getRequiredProperty("jms.cf.read.hostName3");
        this.writePort = environment.getRequiredProperty("jms.cf.write.port");
        this.readPort = environment.getRequiredProperty("jms.cf.read.port");
        this.channel = environment.getRequiredProperty("jms.cf.channel");
        this.transportType = environment.getRequiredProperty("jms.cf.transportType");
        this.updateQueue = environment.getRequiredProperty("jms.queue.update");
        this.replyQueue = environment.getRequiredProperty("jms.queue.reply");
        this.queueGateway = environment.getRequiredProperty("jms.queue.gateway");
    }

    public MQQueueConnectionFactory connectionFactory1() {
        MQQueueConnectionFactory connectionFactory = new MQQueueConnectionFactory();
        try {
            connectionFactory.setHostName(hostName1);
            connectionFactory.setPort(Integer.parseInt(writePort));
            connectionFactory.setChannel(channel);
            connectionFactory.setTransportType(Integer.parseInt(transportType));
        } catch (NumberFormatException | JMSException e) {
            log.error(e.toString(),e);
        }
        return connectionFactory;
    }

    public MQQueueConnectionFactory connectionFactory2() {
        MQQueueConnectionFactory connectionFactory = new MQQueueConnectionFactory();
        try {
            connectionFactory.setHostName(hostName2);
            connectionFactory.setPort(Integer.parseInt(readPort));
            connectionFactory.setChannel(channel);
            connectionFactory.setTransportType(Integer.parseInt(transportType));
        } catch (NumberFormatException | JMSException e) {
            log.error(e.toString(),e);
        }
        return connectionFactory;
    }

    public MQQueueConnectionFactory connectionFactory3() {
        MQQueueConnectionFactory connectionFactory = new MQQueueConnectionFactory();
        try {
            connectionFactory.setHostName(hostName3);
            connectionFactory.setPort(Integer.parseInt(readPort));
            connectionFactory.setChannel(channel);
            connectionFactory.setTransportType(Integer.parseInt(transportType));
        } catch (NumberFormatException | JMSException e) {
            log.error(e.toString(),e);
        }
        return connectionFactory;
    }

    @Bean
    public Destination jmsDestinationResolverSender() throws JMSException {
        return new MQQueue(updateQueue);
    }

    @Bean
    public Destination jmsDestinationResolverReceiver() throws JMSException {
        return new MQQueue(replyQueue);
    }

    @Bean
    public Destination jmsDestinationResolverReplyTo() throws JMSException {
        return new MQQueue(queueGateway, replyQueue);
    }

    @Bean
    public JmsTemplate jmsQueueTemplate() throws JMSException {
        JmsTemplate jmsTemplate = new JmsTemplate();
        jmsTemplate.setConnectionFactory(connectionFactory1());
        jmsTemplate.setDefaultDestination(jmsDestinationResolverSender());
        jmsTemplate.afterPropertiesSet();

        log.info("Send ConnectionFactory is:" + jmsTemplate.getConnectionFactory());
        log.info("Send destination is:" + jmsTemplate.getDefaultDestination());
        log.info("Send Delivery Delay is :" + jmsTemplate.getDeliveryDelay());
        log.info("Send Delivery Mode is:" + jmsTemplate.getDeliveryMode());

        return jmsTemplate;
    }

    @Bean
    public DefaultMessageListenerContainer listenerContainer() throws JMSException {
        DefaultMessageListenerContainer defMsgListCont = new DefaultMessageListenerContainer();
        defMsgListCont.setConnectionFactory(connectionFactory3());
        defMsgListCont.setDestination(jmsDestinationResolverReceiver());
        defMsgListCont.setMessageListener(messageListenerReciever1);
        defMsgListCont.afterPropertiesSet();
        return defMsgListCont;
    }

    @Bean
    public DefaultMessageListenerContainer listenerContainer2() throws JMSException {
        DefaultMessageListenerContainer defMsgListCont = new DefaultMessageListenerContainer();
        defMsgListCont.setConnectionFactory(connectionFactory2());
        defMsgListCont.setDestination(jmsDestinationResolverReceiver());
        defMsgListCont.setMessageListener(messageListenerReciever2);
        defMsgListCont.afterPropertiesSet();
        return defMsgListCont;
    }   

      // Serialize message content to json using TextMessage
      @Bean
      public MessageConverter jacksonJmsMessageConverter() {
        MappingJackson2MessageConverter converter = new MappingJackson2MessageConverter();
        converter.setTargetType(MessageType.TEXT);
        converter.setTypeIdPropertyName("_type");
        return converter;
      }


}

更新:今天又发生了。另一个有趣的是,MQ msg上的sourcetimestamp与发送时间不同。 Sourcetimestamp显示正确的时间,但是执行被延迟

0 个答案:

没有答案