将不同类型的事件发布到不同的队列

时间:2018-06-13 12:11:55

标签: java rabbitmq spring-amqp axon

我正在尝试创建一个简单的微服务项目来学习使用Axon Framework。

我已通过RabbitMQ使用以下代码设置消息:

   /**
 * <p>Java class for Header complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * &lt;complexType name="Header">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="MessageID" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
 *         &lt;element name="CorrelationID" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
 *         &lt;element name="SystemID" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
 *         &lt;element name="RequestorID" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>

 *       &lt;/sequence>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 * 
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Header", propOrder = {
    "messageID",
    "correlationID",
    "systemID",
    "requestorID"
})
@XmlRootElement(name="Header")
public class Header {

    @XmlElement(name = "MessageID")
    protected String messageID;
    @XmlElement(name = "CorrelationID")
    protected String correlationID;
    @XmlElement(name = "SystemID")
    protected String systemID;
    @XmlElement(name = "RequestorID")
    protected String requestorID;

    public Header() {
    }

    public Header(
            java.lang.String messageID,
            java.lang.String correlationID,
            java.lang.String systemID,
            java.lang.String requestorID) {
            this.messageID = messageID;
            this.correlationID = correlationID;
            this.systemID = systemID;
            this.requestorID = requestorID;

     }

    //Setter and Getters

    // Type metadata
    private static org.apache.axis.description.TypeDesc typeDesc =
        new org.apache.axis.description.TypeDesc(Header.class, true);

    static {
        typeDesc.setXmlType(new javax.xml.namespace.QName("http://..../..Services", "Header","prim"));

        org.apache.axis.description.ElementDesc elemField = new org.apache.axis.description.ElementDesc();
        elemField.setFieldName("messageID");
        elemField.setXmlName(new javax.xml.namespace.QName("http://..../..Services", "MessageID","prim"));
        elemField.setXmlType(new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "string"));
        elemField.setMinOccurs(0);
        elemField.setNillable(false);
        typeDesc.addFieldDesc(elemField);
        elemField = new org.apache.axis.description.ElementDesc();
        elemField.setFieldName("correlationID");
        elemField.setXmlName(new javax.xml.namespace.QName("http://..../..Services", "CorrelationID","prim"));
        elemField.setXmlType(new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "string"));
        elemField.setMinOccurs(0);
        elemField.setNillable(false);
        typeDesc.addFieldDesc(elemField);
        elemField = new org.apache.axis.description.ElementDesc();
        elemField.setFieldName("systemID");
        elemField.setXmlName(new javax.xml.namespace.QName("http://..../..Services", "SystemID","prim"));
        elemField.setXmlType(new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "string"));
        elemField.setMinOccurs(0);
        elemField.setNillable(false);
        typeDesc.addFieldDesc(elemField);
        elemField = new org.apache.axis.description.ElementDesc();
        elemField.setFieldName("requestorID");
        elemField.setXmlName(new javax.xml.namespace.QName("http://..../..Services", "RequestorID","prim"));
        elemField.setXmlType(new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "string"));
        elemField.setMinOccurs(0);
        elemField.setNillable(false);
        typeDesc.addFieldDesc(elemField);
        elemField = new org.apache.axis.description.ElementDesc();

    }

    /**
     * Return type metadata object
     */
    public static org.apache.axis.description.TypeDesc getTypeDesc() {
        return typeDesc;
    }

    /**
     * Get Custom Serializer
     */
    public static org.apache.axis.encoding.Serializer getSerializer(
           java.lang.String mechType, 
           java.lang.Class _javaType,  
           javax.xml.namespace.QName _xmlType) {
        return 
          new  org.apache.axis.encoding.ser.BeanSerializer(
            _javaType, _xmlType, typeDesc);
    }

    /**
     * Get Custom Deserializer
     */
    public static org.apache.axis.encoding.Deserializer getDeserializer(
           java.lang.String mechType, 
           java.lang.Class _javaType,  
           javax.xml.namespace.QName _xmlType) {
        return 
          new  org.apache.axis.encoding.ser.BeanDeserializer(
            _javaType, _xmlType, typeDesc);
    }



}

我的application.properties中的后续内容:

@Bean
public Exchange exchange() {
    return ExchangeBuilder.fanoutExchange("Exchange").build();
}

@Bean
public Queue queue() {
    return QueueBuilder.durable("QueueA").build();
}

@Bean
public Binding binding() {
    return BindingBuilder.bind(queue()).to(exchange()).with("*").noargs();
}

@Autowired
public void configure(AmqpAdmin admin) {
    admin.declareExchange(exchange());
    admin.declareQueue(queue());
    admin.declareBinding(binding());
}

通过此配置,通过Axon Framework发布的所有事件都将发送到QueueA。但现在我想让所有EventA事件转到QueueA,所有EventB事件都转到QueueB。我怎么能这样做?

2 个答案:

答案 0 :(得分:3)

默认情况下,Axon Framework使用事件的包名称作为AMQP路由密钥。这意味着您可以使用模式将队列绑定到主题交换,以匹配这些路由键。 有关详细信息,请参阅https://www.rabbitmq.com/tutorials/tutorial-five-java.html

您可以通过提供自定义RoutingKeyResolver(一个为给定EventMessage返回String的简单函数)来自定义Axon的行为。然后在AMQPMessageConverter中配置它,它负责基于Axon EventMessage创建AMQP消息(反之亦然)。如果您使用默认的AMQP消息格式,则可以使用DefaultAMQPMessageConverter

答案 1 :(得分:0)

但是你正在使用fanoutExchange所以它会将事件放到所有Queue中你必须创建另一个Queue并使用sameExchange绑定并且Query-side可以处理事件