我有一个场景,消息流基于相关ID。所以我在发件人邮件属性中设置相关Id字符串。但我在接收者消息中没有收到相同的id,无效。我已经看到在消息处理器类中设置相同的相关id的另一个选项,可以设置Message。在“转换和发送”。但请告诉我有没有办法在简单的发送和接收功能中获取相关ID。
messProperties.setCorrelationId()
在最新的春季版本中已弃用。
我的发件人:
public class Send {
private static ApplicationContext context = new ClassPathXmlApplicationContext("rabbitConfiguration.xml");
private static RabbitTemplate template = context.getBean(RabbitTemplate.class);
public static void main(String[] args) {
MessageProperties messProperties = new MessageProperties();
messProperties.setDeliveryMode(MessageDeliveryMode.PERSISTENT);
//messProperties.setCorrelationId("coid".getBytes());
messProperties.setCorrelationIdString("dee");
Message messageToSend = new Message("Message with ssl".getBytes(), messProperties);
template.send("message.ssl", messageToSend);
}
}
MyReceiver代码:
public class Receive {
private static ApplicationContext context = new ClassPathXmlApplicationContext("rabbitConfiguration.xml");
private static RabbitTemplate template = context.getBean(RabbitTemplate.class);
public static void main(String[] args) {
Message recMess = template.receive("SSLQueue");
String id=new String(recMess.getMessageProperties().getCorrelationId());
System.out.println("the corelation id from receiver is"+id);
String recStr = new String(recMess.getBody());
System.out.println("received message is"+recStr);
}
}
答案 0 :(得分:0)
CorrelationId和CorrelationIdString不一致:
在MessageProperties中,不推荐使用setCorrelationId(byte [] correlationId)。
但DefaultMessagePropertiesConverter仍然使用BYTES类型的(已弃用)CorrelationIdPolicy。
那么,如果你看一下DefaultMessagePropertiesConverter.toMessageProperties
if (!CorrelationIdPolicy.BYTES.equals(this.correlationIdPolicy) && correlationId != null) {
target.setCorrelationIdString(correlationId);
}
没有设置correlationId ...
我认为在2.0之前,修复此问题的唯一方法是以这种方式配置RabbitTemplate:
DefaultMessagePropertiesConverter messageConverter = new DefaultMessagePropertiesConverter();
messageConverter.setCorrelationIdAsString(DefaultMessagePropertiesConverter.CorrelationIdPolicy.STRING);
RabbitTemplate template = new RabbitTemplate(connectionFactory);
template.setMessagePropertiesConverter(messageConverter);