我有高优先级的消息,需要处理的优先级高于低优先级的消息,但是在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>
我期望在msg1之前收到msg2。我不确定我在这里缺少什么。注意:发送消息时使用者处于活动状态。
答案 0 :(得分:1)
由于消费者在发送消息时处于活动状态,因此几乎可以确定,在第二个消息到达代理之前,代理正在将第一个消息分发给客户端。这意味着,由于客户端已经收到了较低优先级的消息,因此基本上没有时间让较高优先级的消息抢占较低优先级的消息。
通常,只有当队列中有消息堆积时,优先传递的想法才有意义。为了进行测试,您应该在所有消息都已发送之前不激活使用者(此任务需要某种外部协调或手动干预),或者增加消息量,以便在优先发送中可以在队列中积累足够的消息。真的发生了。