Hw to specify minimum value for module options in Spring XD - spring-xd

I need to specify a module option to be greater than 0. I know how to do a range, but how do I specify only the minimum value in a Spring XD java file?
#Range(min=0,max=??????)
public int getNumber()
{
return this.num;
}
Where can I find all possible annotations for module options? Thanks.

Can you use assertion to check if the num greater zero.
Something like this?

Related

Spring Boot | spring EL using with annotation attribute value to set any random value using properties

I am using one existing spring boot annotation and want to set the attribute value dynamically using spring expression and want to have one constant value as a prefix,
sample Code snippet
#KafkaListener(topics="${topic.name}", groupId="#{'consumergroup' + (int)(java.lang.Math).random() }")
public void consumerMethod(){
}
I want to create a new consumer group for each new container using the above kind of implementation.
But I am facing the below exception.
Expression parsing failed; nested exception is
org.springframework.expression.spel.SpelParseException: EL1041E: After
parsing a valid expression, there is still more data in the
expression: 'lparen(()
kindly help me to either use template spring EL or any other way I can use to set dynamic consumer group id with the constant prefix.
groupId="#{'consumergroup' + (100 * T(Math).random()).intValue() }"
There is no cast operator in SpEL. Just because it uses a ConversionService internally to convert from one type to another. I use intValue() any way because the result of random() is Double (not double) - SpEL does coercion into type wrappers for API convenience.
The java.lang is imported into SpEL context automatically. No need to add it for Math type.
100 *. See Math.random() JavaDocs: it returns the value between 0.0 and 1.0. So, casting to int would always bring you only 0. The casting doesn't do rounding.
Also see Spring Boot configuration properties feature where you can use random value for the groupId:
spring.kafka.consumer.group-id=consumergroup${random.int}
https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config.random-values

Difference between #Value and #ConfigurationProperties?

Difference between #Value and #ConfigurationProperties?
In which scenario should I use #Value or #ConfigurationProperties?
#RefreshScope will refresh properties value for both?
I was questioning the same, to myself, and I found a concrete reason and why is better to use ConfigurationProperties over Value.
The main reason is that you could end with some text in the code forcing you to do full-text search to understand where a Value property is used.
You can find the whole explanation in in this lightweith reading: https://tuhrig.de/why-using-springs-value-annotation-is-bad/
Insteead of using #Value annotation multiple time to read value from properties file .we can use #configurationProperties one time

Spring ConfigurationProperties validation of a Double value

I have a property value that should range between 0 and 1.
I like Spring's ConfigurationProperties to validate the property value.
So in my ConfigProperties class I added the #Validated annotation and wrote this:
#Min(0)
#Max(1)
Double fraction;
The strange thing is that the validation works in a manner that looks like flooring / roofing the value from the property file.
This is the outcome of different values I put in the conf file:
fraction=-2.1 -> Spring reports an error and stops (good!)
fraction=2.1 -> Spring reports an error and stops (good!)
fraction=-1.5 -> Spring doesn't report an error and starts (not good!)
fraction=1.5 -> Spring doesn't report an error and starts (not good!)
I also tried using the #Range annotation, but with the same outcomes
So here is the solution as described here:
#DecimalMax("1.0") #DecimalMin("0.0")
Double fraction;
As (clearly) stated by the documentation of both #Min and #Max. The same applies to #DecimalMin and #DecimalMax
Note that double and float are not supported due to rounding errors (some providers might provide some approximative support)
You can use a BigDecimal or BigInteger instead.

#Managedoperation alternative in XML configuration way in spring jmx

I am using JMX of spring Version 2.5 in which I am using JMX
as shown below..
#ManagedOperation(description = "Mark the Entry corresponding ABC flow")
#ManagedOperationParameters(value = {
#ManagedOperationParameter(name = "def", description = "Ids of the entries that needs to be STOP"),
#ManagedOperationParameter(name = "Comments", description = "Note on why these entries are being marked as stop") })
public void abcstop(String def, String gtr){
StringBuffer gfhtrPresent= jmxService.abcd(Ids, comments);
if(idsNotPresent.length()>0)
throw new IOARuntimeException("<font color=red><b>No data found for the following id/id's </b></font>"+idsNotPresent);
}
Now I want to remove the #Managedoperation annaotation and want to configure it with in XML , please advsie how can I configure the #Managedoperation , as i wan the same functionality to be run from xml itself, Please advise.
one way to achieve this is implement your own MBeanInfoAssembler (or subclass one of the standard ones). please advise is there any other way to achieve this, Any early help would be appreciated.
The simplest way might be to use a InterfaceBasedMBeanInfoAssembler.
First, expose the JMX interface as an explicitly-defined interface in your code. (Having such an interface is probably a good idea anyway.) Then you just tell the InterfaceBasedMBeanInfoAssembler to expose a particular interface (or interfaces) via its managedInterfaces property. Apart from the defining the interface in the first place (which you might or might not have already done) the rest is entirely possible from XML configuration. But you won't be able to supply very detailed metadata this way; it's a trade-off.
If you're going to stick with a MetadataMBeanInfoAssembler, you could instead try a custom JmxAttributeSource so that you're only reinventing half the wheel and not the whole lot…

How to decide which value a property takes in a spring bean based on a condition.

Can I set the property of "contentCaptureRegEx" to a regular expression say "x" based on whether an external value of an enum such as ContentType.docType.equals("regtext") ELSE set it to y.
SO,
if(ContentType.docType.equals("regtext"))
constructor-arg is x
else
constructor-arg is y
You can do things like that with the Spring Expression Language (SpEL).
There is a dedicated chapter for pretty much exactly what you want:
7.4 Expression support for defining bean definitions

Resources