CompletableFuture.completedFuture() returns a CompletedFuture that is already completed with the given value.
How do we construct a CompletableFuture that is already completed exceptionally?
Meaning, instead of returning a value I want the future to throw an exception.
Unlike Java 9 and later, Java 8 does not provide a static factory method for this scenario. The default constructor can be used instead:
CompletableFuture<T> future = new CompletableFuture<>();
future.completeExceptionally(exception);
Java 9 provides CompletableFuture.failedFuture(Throwable ex) that does exactly that.
Related
I am new to Camunda.I am trying to trigger a event in spring boot application when I perform manual action like retry from Camunda BPM UI.
For that I am using Eventlistner.
#EventListener Public void onExecutionEvent(DelegateEvent executionDelegate){ // printing executionDelegate here }
I need the information present in executionDelegate object.But on printing it is giving me hash value.
Do anybody have complete object information as there are lot of class and interfaces in this Class.It would you be helpful if i am able to get a sample of complete object I information.
Thanks in advance.
The hash value is the default result of the "toString()" method which every class in java implements by default (by extending "Object").
The fact that you see a hash value means, that the DelegateEvent does not overwrite this method in way you maybe used to.
So the question is: what do you want to know from the delegateExecution? MOst often, you will be interested in the process variables ... you can just print out the result of the "getVariables()" method ...
If you want to learn more about the class and its values, put a break point in your eventListener method, start spring boot in debug mode and use the inspect/evaluate code feature of whatever IDE you use to just play around and get familiar.
My team has been using the #Cachable annotation in Spring and caching Optional<> in Java. We just upgraded to Spring 4.3 and started getting errors because or caches do not allow nulls and Spring was unwrapping the Optional and attempting to put in null when it was empty.
I tried looking in the docs but I could not find anywhere that explains how Spring behaves when it goes to the cache, finds null and is supposed to return an Optional<>. Can anyone provide some context; will it convert it to an empty Optional or will it throw any error?
Support for Optional was added to the Spring Cache Abstraction around version 4.3.3.RELEASE See the Conditional Caching section of this for an example.
The cache abstraction supports java.util.Optional, using its content as cached value only if it present. #result always refers to the business entity and never on a supported wrapper so the previous example can be rewritten as follows:
#Cacheable(cacheNames="book", condition="#name.length < 32", unless="#result.hardback")
public Optional<Book> findBook(String name)
Note that result still refers to Book and not Optional.
Also see this SO post.
I tested this and Spring will put the null value into the Optional.
A little bit late, I know. I found this question looking for the same problem.
I am not a native english speaker and was confused by the meaning of "unless". In the end I managed to solve it doing the opposite #Anand suggested:
unless = "#result == null"
like so:
#Cacheable(value = "clients", key = "#p0.concat(#p1)", unless = "#result == null")
Optional<Client> findByClientAndProfile(String idClient, String profile);
I am using Spring 4.3. Is it possible to get method parameter names and values passed to it? I believe this can be done using AOP (before advice) if possible could you please give me a source code.
The following works as expected (Java 8 + Spring 5.0.4 + AspectJ 1.8.13):
#Aspect
#Component
public class SomeAspect {
#Around("#annotation(SomeAnnotation)")
public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
CodeSignature codeSignature = (CodeSignature) joinPoint.getSignature();
System.out.println("First parameter's name: " + codeSignature.getParameterNames()[0]);
System.out.println("First argument's value: " + joinPoint.getArgs()[0]);
return joinPoint.proceed();
}
}
CodeSignature methodSignature = (CodeSignature) joinPoint.getSignature();
String[] sigParamNames = methodSignature.getParameterNames();
You can get method signature arguments names.
Unfortunately, you can't do this. It is a well-known limitation of bytecode - argument names can't be obtained using reflection, as they are not always stored in bytecode.
As workaround, you can add additional annotations like #ParamName(name = "paramName").
So that, you can get params names in the following way:
MethodSignature.getMethod().getParameterAnnotations()
UPDATE
Since Java 8 you can do this
You can obtain the names of the formal parameters of any method or constructor with the method java.lang.reflect.Executable.getParameters. (The classes Method and Constructor extend the class Executable and therefore inherit the method Executable.getParameters.) However, .class files do not store formal parameter names by default. This is because many tools that produce and consume class files may not expect the larger static and dynamic footprint of .class files that contain parameter names. In particular, these tools would have to handle larger .class files, and the Java Virtual Machine (JVM) would use more memory. In addition, some parameter names, such as secret or password, may expose information about security-sensitive methods.
To store formal parameter names in a particular .class file, and thus
enable the Reflection API to retrieve formal parameter names, compile
the source file with the -parameters option to the javac compiler.
https://docs.oracle.com/javase/tutorial/reflect/member/methodparameterreflection.html
In your AOP advice you can use methods of the JoinPoint to get access to methods and their parameters. There are multiple examples online and at stackoverflow.
Get method arguments using spring aop?
For getting arguments: https://docs.jboss.org/jbossaop/docs/2.0.0.GA/docs/aspect-framework/apidocs/org/jboss/aop/joinpoint/MethodInvocation.html#getArguments()
For getting method details: https://docs.jboss.org/jbossaop/docs/2.0.0.GA/docs/aspect-framework/apidocs/org/jboss/aop/joinpoint/MethodInvocation.html#getMethod%28%29
I’m using Spring 3.2.11.RELEASE and Java 6 (not an option to upgrade Java at this time). I’m writing my own annotation, in which one of the expressions could be expressed as a SpEL-like array …
key="{'org.mainco.subco.example.repo.MyDao', 'getExamples’}”
My question is, how, in Java, do I parse this into a String[] array? I figure there is already some pre-existing logic that Spring provides that will prevent me from having to write complex regular expressions to parse the text.
not sure if this works with Spring 3.2.11, tested it only with version 4.2.6.
String[] params = new SpelExpressionParser().parseExpression("{'foo', 'bar'}").getValue(String[].class);
Good luck!
This question already has an answer here:
Grails get session variable from domain validator
(1 answer)
Closed 9 years ago.
I want to store loggedInUser in a table. So i am accessing loggedInUser using session.But i am getting error like no such property session for this class. How to use session in domain?
class GenderAudit {
String name
User doneBy
def GenderAudit(Gender gender,String operation)
{
this.name=gender.name
this.doneBy = session.loggedInUser
}
}
Domain classes shouldn't know anything about the HTTP layer. Set the value from a service or controller which has access to that data.
Also, note that defining a constructor like that is fine, but you have to also define a no-arg constructor for Hibernate since it creates new empty instances and calls your setters. In general we don't use parameterized constructors in Grails since the map constructor added by Groovy is so convenient.
Use code in your controllers or services
new GenderAudit(name:gender.name, doneBy: session.loggedInUser).save()
I'm agree with Burt Beckwith. Remove all constructors from Domain classes.