Home > Software engineering >  Making a bean parameter optional
Making a bean parameter optional

Time:03-26

I have the below route and bean to get the message from the property file. In some message I have to replace the arguments in the message. Therefore I have looked for a solution without writing 2 methods in the bean one with arguments (getMessageWithArgs) and the second without arguments (getMessage).

Is it doable to get rid of getMessageWithArgs in the bean by using DSL in the route or to make the arguments in the getMessage optional so I have at the end one method in the bean?

Route

@Override
public void configure() throws Exception {

    from("activemq:queue:1234")
        .setHeader("functionId", simple(FUNCTION_ID))
        .process(new Processor() {
            @Override
            public void process(Exchange exchange) throws Exception {
                Message message = exchange.getIn();
                message.setHeader("messageId", "notice_1234_error_messageBox_1");
//              List<String> arguments = Arrays.asList("Test1", "Test2", "Test3");
//              message.setHeader("arguments", arguments);
            }   
        })
    .setHeader("message").method("messageBean", "getMessage( ${header.functionId}, ${header.messageId})")
    //.setHeader("message").method("messageBean", "getMessageWithArgs( ${header.functionId}, ${header.messageId}, ${header.arguments})") 
    .log("after calling getMessageWithoutValidation: ${body}");
}

Bean

@Component
public class MessageBean {

    public String getMessage(@Header("functionId") String functionId, 
            @Header("messageId") String messageId) {
        //Doing something to get the message
        return message;
    }


    public String getMessageWithArgs(@Header("functionId") String functionId,
            @Header("messageId") String messageId, 
            @Header("arguments") List<String>  arguments) {
        //Doing something to get the message
        return message;
    }
}

CodePudding user response:

I think Arbitrary Number of Arguments may work for you.

http://java.sun.com/docs/books/tutorial/java/javaOO/arguments.html#varargs

So the Bean now will be only one like:

public String getMessageWithArgs(@Header("functionId") String functionId, 
            @Header("arguments") String... arguments) {
        String messageId = arguments[0];
        //Doing something to get the message
        return message;
    }

I'm not sure if it can be like that, but it might be a way can help..

CodePudding user response:

If your goal is to keep only one method, keep only the method where you have all arguments knowing that if the header arguments has not been set, its value will be null.

public String getMessage(@Header("functionId") String functionId,
        @Header("messageId") String messageId, 
        @Header("arguments") List<String>  arguments) {
    if (arguments == null || arguments.isEmpty()) {
       // No arguments have been provided
    } else {
       // Arguments have been provided
    }
    return message;
}
  • Related