Spring Async Annotation: Executor Id by Enum - java

I have a custom #Async annotation with the specified Executor ID.
#Retention(RetentionPolicy.RUNTIME)
#Target({ElementType.TYPE, ElementType.METHOD})
#Async("fixedThreadPool")
public #interface AsyncFixedPool {
}
I wonder is it possible to specify executor id by enum? For example, something like that.
public enum ExecutorType {
EXECUTOR_1, EXECUTOR_2
}
...
#Retention(RetentionPolicy.RUNTIME)
#Target({ElementType.TYPE, ElementType.METHOD})
#Async
public #interface AsyncFixedPool {
// value converts as Enum.name()
#AliasFor(annotation = Async.class, attribute = "value")
ExecutorType value();
}
...
#AsyncFixedPool(EXECUTOR_1)
public void myAsyncMethod() {...}

Related

#Inject interface with two implementation

I am using Microprofile and I have a question. I have an interface with a method inside:
public interface CheckData extends Serializable{
MyObject retrieveData(String name);
}
This interface is implemented by 2 different classes( Class A and Class B).
In the service class I need to use class A or class B based on a condition.
I did the #Inject of my interface:
#ApplicationScoped
public class MyService{
#Inject
private CheckData checkData;
public Response manageData{
...
if(condition)
checkData.retrieveData(name) // i needed Class A implementation
if(condition)
checkData.retrieveData(name) // i needed Class B implementation
}
}
how do you specify which implementation to use?
I solved it this way.
I have created a class with two qualifiers:
public class MyQualifier {
#Qualifier
#Retention(RetentionPolicy.RUNTIME)
#Target({ElementType.FIELD, ElementType.TYPE, ElementType.METHOD})
public #interface ClassifierOne {
}
#Qualifier
#Retention(RetentionPolicy.RUNTIME)
#Target({ElementType.FIELD, ElementType.TYPE,ElementType. METHOD})
public #interface ClassifierTwo {
}
}
later I added the qualifiers to the classes that implement the interface:
#ClassifierOne
public class A implements CheckData{
...
}
#ClassifierTwo
public class B implements CheckData{
...
}
Finally I injected the interface specifying the qualifier:
#Inject
#ClassifierOne
private CheckData myClassA;
#Inject
#ClassifierTwo
private CheckData myClassB;
I hope it is correct and can help others.
Thanks to #Turo and #Asif Bhuyan for the support

Spring DTO validation using ConstraintValidator

The DTO that I use is annotated with javax.validation annotations
For example
#JsonIgnoreProperties(ignoreUnknown = true)
public class StudentDTO {
#NotEmpty
private String name;
#Positive
private Long studentId;
}
What if I have to validate using ConstraintValidator for StudentDTO
Spring MVC has the ability to automatically validate #Controller
inputs. In previous versions it was up to the developer to manually
invoke validation logic.
But in your case , you are trying to validate a DTO object in which case , springboot might not be automatically binding your validator to your model and call the validator.So, in that case, you will need to manually bind the object to the validator.
or you can manually invoke the validator on a bean like :
#AutoWired
Validator validator;
...
validator.validate(book);
You can define a custom validator in springboot for model classes if you want and use annotations :
#Documented
#Constraint(validatedBy = CustomDataValidator.class)
#Target( { ElementType.METHOD, ElementType.FIELD })
#Retention(RetentionPolicy.RUNTIME)
public #interface CustomDataConstraint {
String message() default "Invalid data";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
and then define a validator class like :
public class CustomDataValidator implements
ConstraintValidator<CustomDataConstraint, String> {
#Override
public void initialize(CustomDataConstraint data) {
}
#Override
public boolean isValid(String field,
ConstraintValidatorContext cxt) {
return field!= null;
}
}
Your validator class must implement the ConstraintValidator interface and must implement the isValid method to define the validation rules, define the validation rules can be anything as you wish.Then, you can simply add the annotation to your field like :
#CustomDataConstraint
private String name;

Custom Annotations in Java using array with different array object values

can anybody help me to write code for below annotation?
#Encryption( sensitiveTypeInfo={sensitiveTag=9, logHidingScheme='MASK', databaseHidingScheme='ENCRYPT', sensitiveTypeDescription='First name '})
code snippet:
#Documented
#Retention (RetentionPolicy.RUNTIME)
#Target (ElementType.METHOD)
public #interface Encryption
{
String[] sensitiveTypeInfo() ;
}
Based on your comments you should probably try a solution with nested annotations:
#Retention(RetentionPolicy.RUNTIME)
#Target(ElementType.METHOD)
public #interface A
{
String author();
B[] nested();
}
#Retention(RetentionPolicy.RUNTIME)
#Target(ElementType.METHOD)
public #interface B
{
String label();
C[] moreNested();
}
#Retention(RetentionPolicy.RUNTIME)
#Target(ElementType.METHOD)
public #interface C
{
int value();
}
You can then do the following:
#A(author="Vijay", nested={
#B(label="First", moreNested=#C(5)),
#B(label="Second", moreNested={#C(6), #C(7)})
})

Inject EJB annotation

When I use annotation #EJB in my code all work fine
public class CatalogueDetailsPage extends AbstractBasePage {
#EJB(lookup = "java:global/mobile.bank.services-1.0.5/api.ejb-1.1/CatalogueFacadeBean!by.softclub.common.api.ICatalogueService")
private ICatalogueService iCatalogueService;
}
But when I want use #Inject & #Produces I have error null pointer
public class CatalogueDetailsPage extends AbstractBasePage {
#Inject
#EJBBean
private ICatalogueService iCatalogueService;
}
#Stateless
public class EJBFactory {
#EJB(lookup = "java:global/mobile.bank.services-1.0.5/api.ejb-
protected ICatalogueService iCatalogueService;
#Produces
#EJBBean
public ICatalogueService getiCatalogueService() {
return iCatalogueService;
}
}
#Qualifier
#Retention(RetentionPolicy.RUNTIME)
#Target({ ElementType.TYPE, ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER })
public #interface EJBBean {
}
}
Why is this happening?

How to check annotation before loading class?

I have my own class loader. I'd like to load class only if it has some specified annotation.
How to check annotation before loading the class?
Here's a simple example of class annotation:
#SomeAnnotation(name="someName", value="someValue")
public class Meta {}
The Meta definition:
#Retention(RetentionPolicy.RUNTIME)
#Target(ElementType.TYPE)
public #interface SomeAnnotation {
public String name();
public String value();
}
Access to the class annotation and load class:
Class clazz = Meta.class;
ClassLoader classLoader = clazz.getClassLoader();
Annotation annotation = clazz.getAnnotation(SomeAnnotation.class);
if (annotation instanceof SomeAnnotation) {
classLoader.loadClass("Meta");
}

Categories