简体   繁体   中英

Spring Integration and JMS using Java Config

I'm pretty new to Spring Integration and would like to implement a pretty simple scenario which is currently done by Spring Boot application and @JmsListener annotated MDB method only (hard-coded in the method).

  1. Retrieve a message from JMS queue (ie Websphere MQ)
  2. Check header filed and route to a particular service method based on its value
  3. Store the message in the database (ie MongoDB)
  4. In case of a error, store the erroneous message in another collection in the DB

Can somebody provide me an example for the configuration of such scenario? I'd d prefer Java config but xml is OK too. Unfortunately I was not able to gain the knowledge to accomplish this task from spring integration examples.

Thanks in advace

I hope this is self-explanatory and enough to get you started...

@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();
    }

}

result:

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}]

EDIT

With Error handling...

@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();
    }

}

and

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM