2017-11-13 8 views
0

私は、Rabbit MQへのメッセージを表すPOJOを持っています。メッセージのタイプに責任がある整数(ように、それはだかどうかupdateremoveaddとは)あり:RabbitMQ別のリスナータイプ別

public class Message { 
    private String field1; 
    private String field2; 

    private Integer type; 
    ... 
    <some other fields> 
} 

私は私の春のブートアプリで、このようなメッセージを受け入れる消費者を持っています。したがって、それぞれの型を別々に扱うために、私はコード内にいくつかのスイッチ/ケース構造を追加しなければなりません。

このような場合の解決方法はありますか?あなたがルータと春の統合を使用することができます

答えて

1

...異なるサービスアクチベーター(メソッド)へのルータルートがタイプに基づいて

Rabbit Inbound channel adapter -> router -> 

EDIT

はここに例を示します

@SpringBootApplication 
public class So47272336Application { 

    public static void main(String[] args) { 
     SpringApplication.run(So47272336Application.class, args); 
    } 

    @Bean 
    public ApplicationRunner runner(RabbitTemplate rabbitTemplate) { 
     return args -> { 
      rabbitTemplate.convertAndSend("my.queue", new Domain(1, "foo")); 
      rabbitTemplate.convertAndSend("my.queue", new Domain(2, "bar")); 
      rabbitTemplate.convertAndSend("my.queue", new Domain(3, "baz")); 
     }; 
    } 

    @Bean 
    public Queue queue() { 
     return new Queue("my.queue"); 
    } 

    @Bean 
    public IntegrationFlow flow(ConnectionFactory connectionFactory) { 
     return IntegrationFlows.from(Amqp.inboundAdapter(connectionFactory, "my.queue")) 
       .route("payload.type", r -> r 
         .subFlowMapping("1", f -> f.handle("bean", "add")) 
         .subFlowMapping("2", f -> f.handle("bean", "remove")) 
         .subFlowMapping("3", f -> f.handle("bean", "update"))) 
       .get(); 
    } 

    @Bean 
    public MyBean bean() { 
     return new MyBean(); 
    } 

    public static class MyBean { 

     public void add(Domain object) { 
      System.out.println("Adding " + object); 
     } 

     public void remove(Domain object) { 
      System.out.println("Removing " + object); 
     } 

     public void update(Domain object) { 
      System.out.println("Updating " + object); 
     } 

    } 

    public static class Domain implements Serializable { 

     private final Integer type; 

     private final String info; 

     public Domain(Integer type, String info) { 
      this.type = type; 
      this.info = info; 
     } 

     public Integer getType() { 
      return this.type; 
     } 

     public String getInfo() { 
      return this.info; 
     } 

     @Override 
     public String toString() { 
      return "Domain [type=" + this.type + ", info=" + this.info + "]"; 
     } 

    } 

} 
+0

私は例を追加しました。 –

+0

RabbitMQだけで春の統合なしでそれを行うためのオプションはありますか? – asdasdsdf

+0

いいえ。何も組み込まれていない。フレームワークはあなたのドメインオブジェクトについて何も知らない。アプリケーションBeanの周りにラッパーを置いてそれを実行し、そのラッパーにスイッチを置くことができます。少なくともそれはメソッドのルーティングロジックをビジネスロジックから分離します。 –

関連する問題