Using the Java Event Source in TIBCO
There are some palette resources we never or rarely use in TIBCO, and one of them is the Java Event Source. This activity is used to create a custom process starter in the process definition. I wonder what would be the use case to customize a process starter. Is there ever a functionality not available in TIBCO out-of-the-box? Nevertheless, I have gone exploring this function which also led me to some advanced topics in Java. So, this is how Java Event Source looks like.
You have two options when writing the code: you can modify the embedded source code in Designer or you can use an external class. Your external class needs to extend the abstract class JavaProcessStarter from the package com.tibco.plugin.java and export it as a jar file, which is kind of cumbersome since you need to include the external library in the class path so that you can compile the code. Preferably, writing the code in Designer is more convenient.
Conversely, what if writing the logic itself is more convenient to do outside of Designer? You might agree that writing, debugging, and testing the code in the default editor of Designer is far more difficult than doing the same in Eclipse. Thus said, you need to export the compiled code as a jar file and be able to import the packages in the Designer code. That's all right for now. Let's proceed with inspecting the JavaProcessStarter abstract class. This class defines four abstract methods:
- init() - used to initialize resources
- onStart() - used to activate listeners or kick off a process
- onStop() - used to deactivate listeners
- onShutdown() - used to release resources and for other cleanup methods
The JavaProcessStarter also defines three non-abstract methods:
- getGlobalInstance() - it returns the object of the defined Java Global Instance in the advanced tab of Java Event Source
- onEvent(Object object) - used to kick off a process instance
- onError(Throwable throwable) - not really sure how this is used but it would be good to try this some other time
The tricky part is how to use onEvent method in our external class. Fortunately, there is a workaround for that. Now let's think of some silly (and random) logic for this. I can think of the java.util.Timer class which, of course, can be done using the Timer activity. But the twist is that every time it runs at a specified interval, a random integer between 1 and 5 is generated and if it is equal to 3, a process instance kicks off. And this is something the Timer activity cannot do. See, this is silly and random. Our snippet of code would look like this:
public class JavaTimer {
public void startTimer(final Object bwengine) {
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
int random = (int) Math.round(Math.random() * 5);
if (bwengine != null && random == 3)
**********
}
}, delay, period);
}
}
So, how do we kick off the process instance? You would notice that the startTimer method accepts a parameter of Object type. This parameter refers to the instance of the class that extends JavaProcessStarter. Thus, we can invoke the onEvent method on this instance. However, it's not as simple as that in Java. Luckily, we have the concept of reflection (http://en.wikipedia.org/wiki/Reflection_(computer_programming).
On the asterisks portion, we can insert this line
bwengine.getClass().getMethod("onEvent", Object.class).invoke(bwengine, "");
The getMethod method accepts the name of the method to be invoked on the first parameter, and the class of the method parameters depending on the number of parameters the method has. The invoke method accepts the actual object and its arguments.
The getMethod method accepts the name of the method to be invoked on the first parameter, and the class of the method parameters depending on the number of parameters the method has. The invoke method accepts the actual object and its arguments.
That's pretty amazing. We can now use startTimer method within our abstract methods passing as argument the instance of CustomTimerJavaEventSource.
public class CustomTimerJavaEventSource extends JavaProcessStarter {
javaeventsource.JavaTimer timer = null;
public void init() throws Exception {
**********
timer = new javaeventsource.JavaTimer(delay, period);
}
public void onStart() throws Exception {
timer.startTimer(this);
}
public void onStop() throws Exception {
timer.getTimer().cancel();
}
}
That's not bad. But you cannot hardcode the values (in milliseconds) of delay and period. We need global variables to determine the actual values during deployment. You cannot map a global variable directly into the code but you can import the package com.tibco.pe.plugin.PluginProperties and use the getProperty method.
You can access these global variables using the following code:
int delay = Integer.parseInt(PluginProperties.getProperty("tibco.clientVar.JavaTimer/Delay"));
int period = Integer.parseInt(PluginProperties.getProperty("tibco.clientVar.JavaTimer/Period"));
Java Event Source does not allow us to use an Alias Library. So how do we include our external jar file in the class path? Well, you can copy the jar file in the Third Party Core Libraries (TPCL) lib folder. It can be usually found in C:\tibco\tpcl\<version>\lib.
Thank you for this wonderful article. This is really a helpful resource for TIBCO developers and beginners in the field. Readers of our blog will definitely like your site. I really hope you can recommend this blog to your readers as well http://www.xmarter.com/tibco-blog/ We have here free TIBCO tips, tutorials, training and other resources. You are a credible source of information and I know they would appreciate your recommendation. Thanks! Keep up the good work.
ReplyDeletetibco bpm online training| tibco bpm training| call us+ ...
ReplyDeletewww.21cssindia.com/courses/tibco-bpm-online-training-161.html
Tibco bpm online training institute in India, Tibco bpm online training institute in murlinagar, online training in Tibco bpm , Tibco bpm online training institute ...
tibco developer online training| tibco developer training| call ...
www.21cssindia.com/courses/tibco-developer-online-training-55.html
Tibco Developer Online Training, Tibco Developer training, Tibco Developer course contents, Tibco Developer , call us: +919000444287 ...
Tibco Hawk online training| Tibco Hawk training| call us+ ...
www.21cssindia.com/courses/tibco-hawk-online-training-240.html
Tibco Hawk Online Training, Tibco Hawk training, Tibco Hawk course contents, Tibco Hawk, call us: +919000444287, Tibco Hawk training institute India, Andhra ...
Tibco BE Training| Tibco BE Online Training |Tibco BE ...
www.21cssindia.com/courses/tibco-be-online-training-244.html
Tibco BE Training, Tibco BE Online Training, Tibco BE Corporate Training, Best Tibco BE Corporate Training, Expert Tibco BE Training, Best Tibco BE ...
tibco administration online training| tibco administration ...
www.21cssindia.com/courses/tibco-administration-online-training-54.html
Tibco Administration Online Training, Tibco Administration training, Tibco Administration course contents, Tibco Administration , call us: +919000444287 ...
Tibco BW/EMS Online Training
www.21cssindia.com/courses/tibco-bw-ems-online-training-163.html
Tibco BW/EMS Training , Tibco BW/EMS Online Training , Tibco BW/EMS Corporate Training, Best Tibco BW/EMS Training , Expert Tibco BW/EMS Training ...
Tibco Active Spaces online training
www.21cssindia.com/courses/tibco-active-spaces-online-training-239.html
Tibco Active Spaces Online Training, Tibco Active Spaces training, Tibco Active Spaces course contents, Tibco Active Spaces, call us: +919000444287, Tibco ...
Tibco Business Studio, AMX Online Training
www.21cssindia.com/.../tibco-business-studio-amx-online-training-162.h...
Tibco Business Studio, AMX online training institute in India, Tibco Business Studio, AMX online training institute in murlinagar, online training in Tibco Business ...
Valuable post useful for everyone.Keep on sharing.
ReplyDeleteTIBCO BW 6.x Training
TIBCO BW Online Training Course
I got fresh and exclusive information on this website. I always tried to learn something new. I am glad to see this post. Thank you so much for sharing this information with us.
ReplyDeleteTIBCO BW Container Edition Online Training
TIBCO BWCE training in Ameerpet
TIBCO BWCE training in Hyderabad
We at PROPERTY HUNTER shifted this service to a level much higher than the broker concept. Our highly trained and professional experts team will review and evaluate our client's needs and requirements. A cus-tomized consultancy option has been developed to respond to each client's needs, needs and capabilities and match them with appropriate opportunities. Property prices in Qatar Evaluate your Property Before pricing and advise about the value of any property, we do in-depth evaluation and market analysis, inspec-tion, in addition to the profes-sional advice from our experienced advisors
ReplyDeleteQatar is now one of the safest countries in the world and is ranked as one of the richest countries in the world. So many opportunities to work, environmental change, career development, investment and the willingness to offer Qatar.
So, if you are looking to invest in Qatar or buy a property to move to a new home, here are some perfect options for you. You can see more like this article here article
Property for sale Properties For Sale in The pearl Qatar