设置MessageProducer上优先对消息没有任何影响

问题描述 投票:0回答:1

我有需要于低优先级的消息要处理高优先级的消息,但设置上的MessageProducer优先似乎也没有影响,以及消息被以相同的顺序消耗掉它们被发送到队列中。

下面是我的代码:

package com.example.jms;

import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jms.DefaultJmsListenerContainerFactoryConfigurer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.ProducerCallback;
import org.springframework.jms.support.converter.MappingJackson2MessageConverter;
import org.springframework.jms.support.converter.MessageConverter;
import org.springframework.jms.support.converter.MessageType;

@SpringBootApplication
public class JmsPriorityApplication {

    private static final Logger logger= LoggerFactory.getLogger(JmsPriorityApplication.class);

    public static void main(String[] args) {

        // Launch the application
        ConfigurableApplicationContext context = SpringApplication.run(JmsPriorityApplication.class, args);

        JmsTemplate jmsTemplate = context.getBean(JmsTemplate.class);
        // Send a message with a POJO - the template reuse the message converter
        System.out.println("Sending an email message.");

        jmsTemplate.execute("mailbox", new ProducerCallback<Object>() {

            @Override 
            public Object doInJms(Session session, MessageProducer producer) throws JMSException { 
                String text = "Hello this msg1";
                int priority=1;
                TextMessage message1 = session.createTextMessage(text);
                producer.send(message1, DeliveryMode.PERSISTENT, priority, 0);
                logger.info("{} sent with priority={}", text, priority);

                text = "Hello this msg2";
                priority=9;
                TextMessage message2 = session.createTextMessage(text);
                producer.send(message2, DeliveryMode.PERSISTENT, priority, 0);
                logger.info("{} sent with priority={}", text, priority);
                return null;
            }

        } );

    }


    @Bean
    public JmsListenerContainerFactory<?> myFactory(ConnectionFactory connectionFactory,
            DefaultJmsListenerContainerFactoryConfigurer configurer) {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        // This provides all boot's default to this factory, including the message converter
        configurer.configure(factory, connectionFactory);
        // You could still override some of Boot's default if necessary.
        return factory;
    }

}

receiver.Java

package com.example.jms;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

@Component
public class Receiver {

    @JmsListener(destination = "mailbox", containerFactory = "myFactory")
    public void receiveMessage(String msg) {
        System.out.println("Received <" + msg + ">");
    }

}

下面是输出:

Sending an email message.
2019-02-05 17:42:44.161  INFO 7828 --- [           main] com.example.jms.JmsPriorityApplication   : Hello this msg1 sent with priority=1
2019-02-05 17:42:44.161  INFO 7828 --- [           main] com.example.jms.JmsPriorityApplication   : Hello this msg2 sent with priority=9
Received <Hello this msg1>
Received <Hello this msg2>

我期待MSG2 MSG1之前接收到。我不知道我在这里失踪。注意:当正在发送的消息,消费者处于活动状态。

spring-boot jms
1个回答
1
投票

由于消费者是主动当消息被发送几乎可以肯定的是,经纪人正在调度的第一条消息到客户端上的代理中的第二个到达之前。这意味着,基本上没有时间优先级较高的信息,因为客户已经收到了低优先级的消息抢占低优先级的消息。

在一般情况下,优先交付的想法才有意义,当有队列中的消息的积聚。为了您的测试,你应该或者不激活消费,直到所有邮件已发送(这将需要某种形式的外部协调或人工干预的任务)或增加的消息量,从而有足够的信息建立在队列中的优先交付能实际发生。

© www.soinside.com 2019 - 2024. All rights reserved.