Camunda DelegateExecution Event - spring-boot

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.

Related

Returning an object from a Spring Batch job / processor

I have a Spring Batch job that reads in a very large fixed length file and maps it just fine to an object. Validated all of the data in the associated processing task.
Being rather new to Spring and Spring Batch I am wondering if it is possible to get out of the job, a fully populated object to be used in a particular case when I am running the job as part of another process ( that I would like to have access to the data).
I realize that I could do the above without Batch, and it seems to be designed with scope limitations for its purpose.
I could serialize the objects in the processor and go that route but for my immediate satisfaction I am hoping there is a way to get around this.
Thanks
In my #configuration class for the batch processing, I created a class variable (it is a list of the object I want to get back) and instantiated with the no arg constructor.
My Step, ItemReader, LineMapper are setup to use a list for input. The custom FieldSetMapper takes that list instantiated from the constructor as a parameter and adds to the list as the file is read and mapped. Similarly my custom ItemProcessor takes the list as input and returns it.
Finally I created a ReturnObjectList bean that returns the populated list.
In my main I cast the AnnotationConfigApplicationContext getbean to the list of that object type. I am now able to use the list of objects generated from the fixed file in the scope of my main application.
Not sure if this is a healthy work around in terms of how Spring Java config is supposed to work, but it does give me what I need.

Spring AOP: around advice without calling proceed

I have application managed by Spring v4. I'd like to user AOP to add logging without code change but.. Generally I have tow component managed by Spring one is used for creating second one, let's call them A and B. During crating A method B.initialize is being called. To log the start of initializing I have Aspect component with appropriate pointut:
#Around("execution(* com.aop.B.initialize())")
So my problem: method initialize has a few nullable properties which will be initialized in the future with another framework so when I call proceed() the result is NullPointerException, but... when I comment proceed method and pointcut method are invoked everything works fine. Result is two records in log (those which should be before and after proceed method) and well initialized component A.
Could somebody explain me what happened here? I mean, does Around advice without direct proceed invoking works in the same way ans the Before one?

Setting a global map in Grails

I am building a Grails web app, I created a map in BootStrap and placed it in servletContext in order to make it available to my application from anywhere. On average this map should hold about 1000 entries with String keys and Date value.
I was wondering if that can impact my application performance and there is a better place to keep this map ? I want this map to work as a caching mechanism. I wanna put a unique Key in it and a date, and be able to retrieve that date object from anywhere such as within a controller, or service class by passing the key. I was thinking of using a caching mechanism to do that but haven't find one that can do this form. I appreciate it if anyone can suggest any plugin for Grails that can achieve this.
P.S: Is it possible to do this with Cache Plugin : http://grails-plugins.github.io/grails-cache/docs/manual/guide/usage.html#annotations
You could use a Service for this task. Service is a singleton, so it will be alive all the time. And it's much easier to access from other parts of app. To prepare data on application startup, you can implements InitializingBean.
Foe example:
class MyCacheService implements InitializingBean {
Map cache
void afterPropertiesSet() {
cache = [
a: 1,
b: 2,
// .....
]
}
}
About making the Map cache thread-safe, we can use ConcurrentReaderHashMap cache so that does mostly-concurrent reading, but exclusive writing. That way everyone can read it from the service but not everyone can write to it or modify it at the same time.
It is possible to use Synchronized block on the methods such as addToCache so that not two controllers can write at the same time, but for getFromCache we don't need that.
Sample code for ConcurrentReaderHashMap

How to add a custom ContentHander for JAXB2 support in Spring 3 (MVC)?

Scenario: I have a web application that uses Spring 3 MVC. Using the powerful new annotations in Spring 3 (#Controller, #ResponseBody etc), I have written some domain objects with #XML annotations for marhalling ajax calls to web clients. Everything works great. I declared my Controller class to have a return type #ResponseBody with root XML object - the payload gets marshalled correctly and sent to Client.
The problem is that some data in the content is breaking the XML compliance. I need to wrap this with CDATA when necessary. I saw a POST here How to generate CDATA block using JAXB? that recommends using a custom Content Handler. Ok, fantastic!
public class CDataContentHandler extends (SAXHandler|XMLSerializer|Other...) {
// see http://www.w3.org/TR/xml/#syntax
private static final Pattern XML_CHARS = Pattern.compile("[<>&]");
public void characters(char[] ch, int start, int length) throws SAXException {
boolean useCData = XML_CHARS.matcher(new String(c,start,length)).find();
if (useCData) super.startCDATA();
super.characters(ch, start, length);
if (useCData) super.endCDATA();
}
}
Using Spring MVC 3, how do I achieve this? Everything was "auto-magically" done for me with regards to the JAXB aspects of setup, Spring read the return type of the method, saw the annotations of the return type and picked up JAXB2 off the classpath to do the marshalling (Object to XML conversion). So where on earth is the "hook" that permits a user to register a custom Content Handler to the config?
Using EclipseLink JAXB implementation it is as easy as adding #XmlCDATA to the Object attribute concerned. Is there some smart way Spring can help out here / abstract this problem away into a minor configuration detail?
I know Spring isn't tied to any particular implementation but for the sake of this question, please can we assume I am using whatever the default implementation is. I tried the Docs here http://static.springsource.org/spring-ws/site/reference/html/oxm.html but it barely helped at all with this question from what I could understand.
Thanks all for any replies, be really appreciated.
Update:
Thanks for the suggested answer below Akshay. It was sufficient to put me on right tracks. Investigating further, I see there is a bit of history with this one between Spring version 3.05 and 3.2. In Spring 3.05 it used to be quite difficult to register a custom MessageConverter (this is really the goal here).
This conversation pretty much explains the thinking behind the development changes requested:
https://jira.springsource.org/browse/SPR-7504
Here is a link to the typically required class override to build a cusom solution:
http://static.springsource.org/spring/docs/3.1.0.M1/javadoc-api/org/springframework/http/converter/AbstractHttpMessageConverter.html
And the following Question on stack overflow is very similar to what I was asking for (except the #ResponseBody discussion relates to JSON and jackson) - the goal is basically the same.
Spring 3.2 and Jackson 2: add custom object mapper
So it looks like usage of , and overriding MarshallingHttpMessageConverter is needed, registering to AnnotationMethodHandlerAdapter. There is a recommended solution in link above to also get clever with this stuff and wrap the whole thing behind a custom defined Annotation.
I haven't yet developed a working solution but since I asked the questions, wanted to at least post something that may help others with the same sort of question, to get started. With all due respect, although this has all improved in Spring 3.2, it's still bit of a dogs dinner to get a little customization working... I really was expecting a one liner config change etc.
Rather than twist and bend Spring, perhaps the easiest answer for my particular issue is just to change JAXB2 implementation and use something like Eclipse Link JAXB that can do this out of the box.
Basically you need to create a custom HttpMessageConverter. Instead of relying on the Jaxb2RootElementHttpMessageConverter that spring uses by default.
Unfortunately, customizing one converter means you are telling spring that you will take care of loading all the converters you need! Which is fairly involved and can get complicated, based on whether you use annotations, component scanning, Spring 3.1 or earlier, etc.. The issue of how to add a custom converter is addressed here: Custom HttpMessageConverter with #ResponseBody to do Json things
In your custom message converter you are free to use any custom JAXB2 content handlers.
Another, simpler approach to solve your original problem would be to use a custom XmlJavaTypeAdapter. Create a custom implementation of javax.xml.bind.annotation.adapters.XmlAdapter to handle CDATA, in the marshal method wrap the return value with the cdata braces. Then in your mapped pojo, use the XmlAdapter annotation, pass it the class of your custom adapter and you should be done.
I have not myself implemented the adapter approach, so couldn't provide sample code. But it should work, and won't be a lot of work.
Hope this helps.

Update field annotated with #Value in runtime

Let's imagine we have such a component in Spring:
#Component
public class MyComponent {
#Value("${someProperty}")
private String text;
}
If we define the property placeholder:
<context:property-placeholder location="classpath:myProps.properties"/>
And myPropos.properties contains the value for someProperty the value will be injected to the text field when the context is initialized. That's quite simple and easy.
But let's say that I have a service that enables user to change the value of the someProperty:
public void changeProp(String name, String newValue);
Is there a chance I can re-inject the newValue to text field. I mean it should be quite straight forward.. Basically it's nothing different than the after-initialization injection. I can not imagine that Spring does not have support for this? Can I fire some event or something?
I could do this on my own basically, but I wander is it maybe something there already? If not does anyone know what Spring class is in fact handling the injections at the first place? I could probably reuse the code there do perform this on my own if a solution does not exists.
I expect spring does not have a support for this, because the normal injection is done while creating the bean, but not will it is put in service.
Anyway: in this blog entry "Reloadable Application Properties with Spring 3.1, Java 7 and Google Guava", you can find the idea for an solution.
The key idea is to use a post processor to build a list of all fields with property fields. And if the properties are changed on can use this list to update the fields.

Resources