Creating an Esper long running process or service - service

I'd like to create an Esper engine long running process but I'm not sure of Esper's threading model nor the model I should implement to do this. Naively I tried the following:
public class EsperTest {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
//EPServiceProvider epService = EPServiceProviderManager.getDefaultProvider();
EPServiceProvider epService = EPServiceProviderManager.getProvider("CoreEngine");
epService.addServiceStateListener(new EPServiceStateListener() {
#Override
public void onEPServiceDestroyRequested(EPServiceProvider epsp) {
System.out.println("Service destroyed");
}
#Override
public void onEPServiceInitialized(EPServiceProvider epsp) {
System.out.println("System initialised");
}
});
epService.initialize();
}
}
But the code appears to execute to the end of the main() method and the JVM ends.
Referring to the Esper documentation, section 14.7 p456:
In the default configuration, each engine instance maintains a single timer thread (internal timer)
providing for time or schedule-based processing within the engine. The default resolution at which
the internal timer operates is 100 milliseconds. The internal timer thread can be disabled and
applications can instead send external time events to an engine instance to perform timer or
scheduled processing at the resolution required by an application.
Consequently I thought that by creating a an engine instance ("CoreEngine") at least one (timer) thread would be created and assuming this is not a daemon thread the main() method would not complete but this appears not to be the case.
Do I have to implement my own infinite loop in main() or is there a configuration which can be provided to Esper which will allow it to run 'forever.?

The timer threads is a daemon thread.
Instead of a loop use a latch like this.
final CountDownLatch shutdownLatch = new CountDownLatch(1);
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
shutdownLatch.countDown();
}
});
shutdownLatch.await();

Related

Kafka Streams: Program architecture for handling multiple streams

I am running multiple individual kafka streams. To this end I created a Stream Manager that holds these streams. Below is the SM class in essence.
public class StreamManager {
public Map<String, BaseStream> streamMap = new HashMap<String, BaseStream>();
public Map<String, ReadOnlyKeyValueStore<Long, BaseModel>> storeMap = new HashMap<String, ReadOnlyKeyValueStore<Long, BaseModel>>();
public StreamManager(String bootstrapServer){
initialize(bootstrapServer);
}
/**
* initialize streams. Right now hard coding creation of streams here.
* #param bootstrapServer
*/
public void initialize(String bootstrapServer){
Properties s1Props = this.GetStreamingProperties(bootstrapServer, "STREAM_1");
Properties s2Props = this.GetStreamingProperties(bootstrapServer, "STREAM_1");
BaseStream s1Stream = new CompositeInfoStream(new KStreamBuilder(), compositeInfoProps);
BaseStream s2Stream = new ImcInfoStream(new KStreamBuilder(), imcInfoProps);
streamMap.put(s1Stream.storeName, s1Stream);
streamMap.put(s2Stream.storeName, s2Stream);
/**
* Start all streams
*/
public void startStreams(){
for(BaseStream stream: streamMap.values()){
stream.start();
}
}
public static void main(String[] args) throws Exception {
StreamManager mgr = new StreamManager(StreamingConfig.instance().MESSAGE_SERVER);
StreamManager.startStreamServices(mgr);
Runtime.getRuntime().addShutdownHook(new Thread() {
#Override
public void run() {
try {
} finally {
mgr.closeStreams();
}
}
});
int i = 0;
while(true) {
if (i++ == 0)
mgr.logAllStreamStates();
Thread.sleep(60000);
if (i == 60) i = 0;
}
}
}
I initialize and start the streams and then let the process run in a loop. Now what I want is to have more control over individual streams. To start and kill them if need be (for some odd reason my streams go in REBALANCE mode often and don't come back. Currently, if one of the stream goes into REBALANCING I have to kill the entire SM (all streams) and restart. What I would like to do is only restart the individual stream.
I would like to get a sense of how my architecture should be. Does kafka streams provide a mechanism to manage a cluster of streams? Can I use multiprocessing to accomplish this and if so could you guide me to some resources to do so keeping in mind that we use windows for development and linux for deployment

Eclipse job with UI access

I have one situation.
I have one Eclipse job with following code:
private class ExecutionJob extends Job {
public static final String MY_FAMILY = "myJobFamily";
public ExecutionJob(String name) {
super(name);
}
#Override
protected IStatus run(IProgressMonitor monitor) {
monitor.beginTask("executing ...... ", IProgressMonitor.UNKNOWN);
methodForExecution();
monitor.done();
return Status.OK_STATUS;
}
#Override
public boolean belongsTo(Object family) {
return family == MY_FAMILY;
}
}
And this methodForExecution() has code as below :
public void methodForExecution(){
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView("view_id");
}
Now, the situation is, job opens up something like progressmonitor, and my method is trying to access UI which is actually behind this job's progressmonitor. And it gives NullPointerException as the progress monitor does not have ActiveWorkbenchWindow.
I can not use UIJob, as I have to execute this methodForExecution() asynchronously.
Can someone please help me resolving this.
The code you want to run must run in the UI thead.
If most of the work in the job is updating the UI and there is no long running non-UI code then you should use UIJob to run this. This is still scheduled as a job but the runInUIThread method is executed in the UI thread.
If you have a lot of non-UI code especially long running code then use a normal Job but you will have to use Display.asyncExec to run the method in the UI thread:
Display.getDefault().asyncExec(new Runnable()
{
#Override
public void run()
{
methodForExecution();
}
});
In Java 8 you could do:
Display.getDefault().asyncExec(this::methodForExecution);
You can also use syncExec instead of asyncExec to wait for the UI to update.
If the showView is all you want to do you could just do the asyncExec without using a Job.

Scheduling multiple jobs with dynamic parameter using Quartz 1.6.2 (simple trigger) + Spring 3.0

I am trying to create a sample scheduler program using quartz simple trigger. My objective is run a single method with different parameters passed dynamically by user (considered as new Job)and schedule it for user defined time which is also received dynamically.
eg:
public void printMe (String name, int Age, Date dob) {
system.out.println("Name:"+name+" Age:"+age+" DOB:"+dob);
}
How to schedule and execute this method based on dynamically received parameters and start date and time using quartz 1.6.2. I am new to quartz scheduler, I have no idea how to do this. Do anyone help me with the sample programs you have?
Here is example to use quartz job via simple trigger
Simple Trigger 1 minutes with 1 second delay for every run to execute:
public static void main(String[] args) throws Exception{
JobDetail job = new JobDetail();
job.setName("dummyJobName");
job.setJobClass(HelloJob.class);
//configure the scheduler time
SimpleTrigger trigger = new SimpleTrigger();
trigger.setStartTime(new Date(System.currentTimeMillis() + 1000));
trigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
trigger.setRepeatInterval(100000);// set time run again is 1 minutes
//schedule it
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
scheduler.start();
scheduler.scheduleJob(job, trigger);
}
You create class for execute class job
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class HelloJob implements Job
{
public void execute(JobExecutionContext context)
throws JobExecutionException {
printmMe("YourName",20,"12/15/2013");
}
public void printMe (String name, int Age, Date dob) {
system.out.println("Name:"+name+" Age:"+age+" DOB:"+dob);
}
}
Above, that's program use quartz schedule to execute job HellloClass

Is it possible to kill a current running Quartz Job?

I remember that we cannot kill the current running Quartz Job but we can interrupt and have a boolean check wherever is necessary whether we need to proceed further with the subsequent operations or not.
Even when we implement the InterruptableJob and call the scheduler.interrupt to interrupt the Job, the current executed job will be still running in the server.
Ex:
A named SQL query has been triggered by the job via Hibernate which takes a long time
A call has been made to a third party server where the third party server takes a long time to respond
http://neopatel.blogspot.in/2011/05/quartz-stop-job.html
http://forums.terracotta.org/forums/posts/list/3191.page
Could someone corrects my understanding and explain me how we can kill or stop the "currently" executing Job ?
you can create new abstract class called JobBase for example that implements IJob interface and insert abstract method:
public abstract void ExecuteJob(IJobExecutionContext context);
On JobBase you can implements method Execute like this
public abstract class JobBase : IJob,IInterruptableJob
{
private Thread currentThread;
private ILog logger;
public JobBase(ILog logger)
{
this.logger=logger;
}
public void Execute(IJobExecutionContext context)
{
var thread = new Thread(()=>
{
try
{
this.ExecuteJob(context);
}
catch(Exception ex)
{
this.logger.ErrorFormat("Unhandled exception {0}",ex.ToString());
}
});
thread.Start();
this.currentThread = thread;
this.currentThread.Join();
}
public abstract void ExecuteJob(IJobExecutionContext context);
public void Interrupt()
{
currentThread.Abort();
}
}
Each Job will implements JobExecute method.
public class TestJob :JobBase
{
private ILog logger;
public TeJob(ILog logger):base(logger)
{
}
public override ExecuteJob(IJobExecutionContext context)
{
}
}
Assumes that use some factory for creating a Job
For Stopping a Job you will call method scheduler.Interrupt(new JobKey(jobName));
As you told, there is no way to interrupt "brutally" a job in quartz, neither in JAVA.
You can encapsulate your job's logic in a separate Thread and run it with the ExecutorService.
Take a look to this example: https://stackoverflow.com/a/2275596/1517816
Assume your QuartzJob is the Test class and move your business logic in the Task class.
Hope it helps
I don't know why nobody mentioned this, or maybe this was not available at the time the question was asked.
There is a method called shutdown for a Scheduler instance.
SchedulerFactory factory = new StdSchedulerFactor();
Scheduler scheduler = factory.getScheduler();
The above is used to start a job like
scheduler.start();
Use a flag or something to know when to stop the job from running. Then use
scheduler.shutdown();
How I implemented my requirement:
if(flag==true)
{
scheduler.start();
scheduler.scheduleJob(jobDetail, simpleTrigger);
}
else if(flag==false)
{
scheduler.shutdown();
}
Where jobDetail and simpleTrigger are self explanatory.
Hope it helps. :)

Explicitly wait for asynchronous action to complete?

I have the following utility:
class Worker
{
public void DoWorkAsync(Action callback)
{
Action work = () => Thread.Sleep(3000);
AsyncCallback asyncCallback = (result) => callback();
work.BeginInvoke(asyncCallback, null);
}
}
I use it like the following:
static void Main(string[] args)
{
var worker = new Worker();
worker.DoWorkAsync(() => Console.WriteLine("Completed."));
Console.WriteLine("Hello world!");
Console.ReadKey();
}
This will of course print Hello world! before Completed., since the worker works asynchronously.
My question is how can I block my thread so it should wait until the action is completed, then move on.
I know I can do it like this:
static void Main(string[] args)
{
var worker = new Worker();
worker.DoWorkAsync(() =>
{
Console.WriteLine("Completed.");
MoveOn();
});
Console.ReadKey();
}
static void MoveOn()
{
Console.WriteLine("Hello world!");
}
But since I have a bunch of cascaded async callbacks that should be executed one after the other (conditionally), I want them all to execute synchronously, so is there a more elegant way to wait for an async method that takes a callback as a param?
Note: Just to make sure, I cannot alter the behavior of the Worker class, its an external utility and I don't have access to its code.
Update
In my particular scenario I'm trying to interact with user from the ViewModel and get responses from him. My code is executed in the view-model and there is a method controlling a chain of cascaded interactions, I want this method should decide whether to fire a certain interaction or not. I tried SLaxs' answer, and also tried this but it doesn't seem to work, any ideas on how to make the main method the only controller of the interactions?
Create a ManualResetEvent, call Set() in the callback, and call WaitOne() to wait for the operation to finish.