Spring Integration - Filter - Send messages to a different end point

别来无恙 提交于 2019-12-24 20:18:40

问题


My input is a csv file as given below:

USER_ID, USER_NAME, FREQUENCY, FREQUENCY_DETAIL
A123, AAA, ANNUALLY, 1-JUN
B123, BBB, INVALID_FREQUENCY, 21-JUN
C123, CCC, ANNUALLY, APR
D123, DDD, WEEKLY, 1-DEC

Validations:

USER_ID -> alphanumeric
USERNAME -> alphabets only
FREQUENCY -> must be one of DAILY, WEEKLY, MONTHLY , ANNUALLY
FREQUENCY DETAIL -> Pattern \\d{1,2}-\\w{3}

My Bean is as follows:

class UserBean {
    String userID;
    String userName;
    String frequency;
    String frequencyDetail;

    String status = "SUCCESS"; //Code generated. If any of the above fields is not valid, set as "ERROR, <field that failed>". E.g.: ERROR, FREQUENCY_DETAIL
}

My flow is as follows:

  1. Read csv file from folder
  2. Convert to UserBean (using openCsv)
  3. If the status of any of the beans contains "ERROR", write to a separate channel called "errorSummaryReportChannel".

This channels writes userID + status to a file.

4. For all beans (with status = "SUCCESS" or status = "ERROR"), convert to JSON and log output.

WHERE I NEED HELP:

Step 3

If the userBean has status = "ERROR", write to "errorSummaryReportChannel". For all status, proceed with normal flow.

I think an output channel & discard channel must be added, but could not find any example.

My code:

@Configuration
public class CreateUserConfiguration {
    @Bean
    public IntegrationFlow createUser() {
        return IntegrationFlows.from(Files.inboundAdapter(new File(INPUT_DIR)))
                .enrichHeaders(h -> h.header("errorChannel", "exceptionChannel", true))
                .transform(csvToUserBeanTransformer, "convertCsvToUserBean")
                .split(userBeanSplitter, "splitUserBeans")
                .wireTap(flow -> flow.<PrimeAOBean>filter(primeAOBean -> primeAOBean.getStatus().equalsIgnoreCase("SUCCESS"), errorFlow -> errorFlow.discardChannel("errorSummaryReportGenerationChannel")))
                .transform(userBeanToJSONTransformer, "convertUserBeanToJSON")
                .handle(Files.outboundAdapter(new File(OUTPUT_SUCCESS_DIRECTORY)))
                .get();
    }

    @Bean
    public IntegrationFlow logErrorSummary() {
        return IntegrationFlows.from("errorSummaryReportGenerationChannel")
                .handle((p,h) -> {
                    return ((UserBean)(p)).getUserID() + "\t" + ((UserBean)(p)).getStatus();
                })
                .transform(Transformers.objectToString())
                .handle(Files.outboundAdapter(new File(OUTPUT_FAILED_REPORT_FILE_NAME)))
                .get();
    }

@Bean
public IntegrationFlow logError() {
    return IntegrationFlows.from("exceptionChannel")
            .enrichHeaders(h -> h.headerExpression("errorFileName", "payload.failedMessage.headers.fileName"))
            .transform(Transformers.toJson())
            .handle(Files.outboundAdapter(new File(generateOutputDirectory(OUTPUT_FAILED_DIRECTORY))))
            .get();
}

}

Can someone please help?


回答1:


The output channel for filter (or any other endpoint) in the flow is the one created automatically before the endpoint in the flow. So, in your case it is like this:

 .filter()
 .transform()

Whenever message has passed the filter, it is sent into that transformer. We can treat it as a SUCCESS process.

Now let's write a filter function first of all to make those message to flow properly!

There is a method like this for you:

/**
 * Populate a {@link MessageFilter} with {@link MethodInvokingSelector}
 * for the provided {@link GenericSelector}.
 * Typically used with a Java 8 Lambda expression:
 * <pre class="code">
 * {@code
 *  .filter("World"::equals)
 * }
 * </pre>
 * Use {@link #filter(Class, GenericSelector)} if you need to access the entire
 * message.
 * @param genericSelector the {@link GenericSelector} to use.
 * @param <P> the source payload type.
 * @return the current {@link IntegrationFlowDefinition}.
 */
public <P> B filter(GenericSelector<P> genericSelector) {

So, our task is to implement that GenericSelector for your UserBean:

 .<UserBean>filter(userBean -> userBean.getStatus().equalsIgnoreCase("SUCCESS"))

To send all other (ERROR) message to the discard channel (errorSummaryReportGenerationChannel), we need to add that discardChannel option into the filter somehow. And for this purpose there is an overloaded filter() method in Java DSL:

.<UserBean>filter(userBean -> userBean.getStatus().equalsIgnoreCase("SUCCESS"),
               endpoint -> endpoint.discardChannel("errorSummaryReportGenerationChannel"))

See their JavaDocs for more information.



来源:https://stackoverflow.com/questions/58103341/spring-integration-filter-send-messages-to-a-different-end-point

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!