使用Java Config进行Spring集成和JMS

时间:2018-01-12 10:12:03

标签: java spring spring-integration spring-jms

我是Spring Integration的新手,想要实现一个非常简单的场景,目前只有Spring Boot应用程序和@JmsListener注释的MDB方法(在方法中硬编码)。

  1. 从JMS队列(即Websphere MQ)中检索消息
  2. 检查标头字段并根据其值
  3. 路由到特定服务方法
  4. 将消息存储在数据库中(即MongoDB)
  5. 如果出现错误,请将错误消息存储在DB
  6. 中的另一个集合中

    有人能为我提供一个配置这种情况的例子吗?我更喜欢Java配置,但xml也可以。不幸的是,我无法从弹簧集成示例中获得完成此任务的知识。

    先谢谢

1 个答案:

答案 0 :(得分:2)

我希望这是不言自明的,足以让你开始......

@SpringBootApplication
public class So48223952Application {

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

    @Bean
    public ApplicationRunner runner(JmsTemplate template) {
        return args -> {
            template.convertAndSend("foo", "sendingFoo", m -> {
                m.setStringProperty("myHeader", "foo");
                return m;
            });
            template.convertAndSend("foo", "sendingBar", m -> {
                m.setStringProperty("myHeader", "bar");
                return m;
            });
            Thread.sleep(10_000);
        };
    }

    @Bean
    public IntegrationFlow flow(ConnectionFactory connectionFactory) {
        return IntegrationFlows.from(Jms.messageDrivenChannelAdapter(connectionFactory)
                        .destination("foo"))
                .channel(MessageChannels.publishSubscribe("pubsub"))
                .route("headers['myHeader']",
                        m -> m.channelMapping("foo", "fooChannel")
                              .channelMapping("bar", "barChannel"))
                .get();
    }

    @Bean
    public IntegrationFlow toMongo() {
        return IntegrationFlows.from("pubsub")
                .<String, String>transform(p -> "Sending to db " + p)
                .handle(System.out::println) // store in DB here
                .get();
    }

    @Bean
    public IntegrationFlow foo() {
        return IntegrationFlows.from("fooChannel")
                .<String, String>transform(p -> "on fooChannel " + p)
                .handle(System.out::println)
                .get();
    }

    @Bean
    public IntegrationFlow bar() {
        return IntegrationFlows.from("barChannel")
                .<String, String>transform(p -> "on barChannel " + p)
                .handle(System.out::println)
                .get();
    }

}

结果:

GenericMessage [payload=on fooChannel sendingFoo, headers={jms_redelivered=false, myHeader=foo, jms_destination=queue://foo, id=ea65c71e-3702-88aa-fa07-fe0e53ec7539, priority=4, jms_timestamp=1515771377035, jms_messageId=ID:gollum.local-62392-1515771376836-4:2:1:1:1, timestamp=1515771377049}]
GenericMessage [payload=Sending to db sendingFoo, headers={jms_redelivered=false, myHeader=foo, jms_destination=queue://foo, id=26967f68-b2ad-a0f6-df62-5e8387f345f7, priority=4, jms_timestamp=1515771377035, jms_messageId=ID:gollum.local-62392-1515771376836-4:2:1:1:1, timestamp=1515771377049}]
GenericMessage [payload=on barChannel sendingBar, headers={jms_redelivered=false, myHeader=bar, jms_destination=queue://foo, id=6609a77c-55aa-9b84-49f6-da915b5d1734, priority=4, jms_timestamp=1515771377042, jms_messageId=ID:gollum.local-62392-1515771376836-4:3:1:1:1, timestamp=1515771377052}]
GenericMessage [payload=Sending to db sendingBar, headers={jms_redelivered=false, myHeader=bar, jms_destination=queue://foo, id=6b81ae82-4b2d-9d68-bbee-31a24e407565, priority=4, jms_timestamp=1515771377042, jms_messageId=ID:gollum.local-62392-1515771376836-4:3:1:1:1, timestamp=1515771377052}]

修改

使用错误处理...

@SpringBootApplication
public class So48223952Application {

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

    @Bean
    public ApplicationRunner runner(JmsTemplate template) {
        return args -> {
            template.convertAndSend("foo", "sendingFoo", m -> {
                m.setStringProperty("myHeader", "foo");
                return m;
            });
            template.convertAndSend("foo", "sendingBar", m -> {
                m.setStringProperty("myHeader", "bar");
                return m;
            });
            Thread.sleep(10_000);
        };
    }

    @Bean
    public IntegrationFlow flow(ConnectionFactory connectionFactory) {
        return IntegrationFlows.from(Jms.messageDrivenChannelAdapter(connectionFactory)
                        .destination("foo")
                        .errorChannel("errors"))
                .channel(MessageChannels.publishSubscribe("pubsub"))
                .route("headers['myHeader']",
                        m -> m.channelMapping("foo", "fooChannel")
                              .channelMapping("bar", "barChannel"))
                .get();
    }

    @Bean
    public IntegrationFlow toMongo() {
        return IntegrationFlows.from("pubsub")
                .<String, String>transform(p -> "Sending to db " + p)
                .handle(System.out::println) // store in DB here
                .get();
    }

    @Bean
    public IntegrationFlow foo() {
        return IntegrationFlows.from("fooChannel")
                .<String, String>transform(p -> "on fooChannel " + p)
                .handle(System.out::println)
                .get();
    }

    @Bean
    public IntegrationFlow bar() {
        return IntegrationFlows.from("barChannel")
                .<String, String>transform(p -> "on barChannel " + p)
                .handle(m -> {
                    throw new RuntimeException("error testing");
                })
                .get();
    }

    @Bean
    public IntegrationFlow errorFlow() {
        return IntegrationFlows.from("errors")
                .handle(m -> {
                    MessagingException me = (MessagingException) m.getPayload();
                    System.out.println("Message: " + me.getFailedMessage() + "\nFailed with "
                            + me.getCause().getMessage());
                })
                .get();
    }

}

GenericMessage [payload=on fooChannel sendingFoo, ...
GenericMessage [payload=Sending to db sendingFoo, ...
Message: GenericMessage [payload=on barChannel sendingBar, ...
Failed with error testing