Forum Stats

  • 3,855,382 Users
  • 2,264,500 Discussions
  • 7,905,981 Comments

Discussions

Killing a thread ( runnable ) - problem

938644
938644 Member Posts: 50
edited Oct 13, 2012 4:42AM in Complex Event Processing
Hi,
I already posted about this, and got a good answer.
But, I still have a probalem :
Here is a piece of my code :
// this is the bean which is in the EPN and I want it to create a thread that prints something :


public class EventListener implements StreamSink,com.bea.wlevs.ede.api.DisposableBean ,InitializingBean {
......
// the relevant methods:

RunnableTestBean runnableTestBean; // this is the thread which suppose to print ..

public void destroy() throws Exception {



System.out.println("destroydestroydestroydestroydestroydestroydestroy");
runnableTestBean.suspend();

}

public void afterPropertiesSet() throws Exception {
runnableTestBean=new RunnableTestBean();
runnableTestBean.run();
//System.out.println("afterPropertiesSetafterPropertiesSetafterPropertiesSet");

}

...

and the class RunnableTestBean :

public class RunnableTestBean implements RunnableBean {


boolean stopped=false;
public void run() {
while ( !stopped){
System.out.println(" printing...");
try {
wait(5000);
//Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println(" Stoping thread ??");
e.printStackTrace();
}
}

}

public void suspend() throws Exception {
stopped=true;

}

....

The thing is that when I use the method as above :

..
public void run() {
while ( !stopped){
System.out.println(" printing...");
try {
wait(5000);
//Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println(" Stoping thread ??");
e.printStackTrace();
}

When I undeploy the application , the destoy method ( in EventListener class ) is not called !
but,
if I implement the run "run" method like this :

public void run() {
..
System.out.println(" Something ");

}
than everything is fine!

The differrence as you can see , is that when I use an infinite loop - the destroy method never called ( seems that the caller waits for it to stop , and that's why the
destroy method not called )

When i don't use the infinaie loop- it's OK.

So, any help ?
Thanks ..

Answers

  • 722711
    722711 Member Posts: 91
    Looking at the code that you have posted, the problem is that you are calling runnablebean.run() method in afterPropertiesSet (instead of starting a thread in which the run method is called). So, it looks like it will get stuck in afterPropertiesSet for ever.

    The recommended approach for what you are trying to do is to make the EventListener class implement RunnableBean (instead of having a separate class that implements RunnableBean that gets called from afterPropertiesSet). This means that at application startup time, this EventListener's run() method will be called by OCEP framework code in a separate thread. You can still use the suspend() method to stop the thread similar to how you are doing in your code now.
  • vladodias
    vladodias Member Posts: 2,283 Gold Trophy
    Where you're doing runnableTestBean.run() you should do runnableTestBean.start()...
  • 938644
    938644 Member Posts: 50
    Hi
    I affraid that i misunderstood the sentence :
    " this EventListener's run() method will be called by OCEP framework code in a separate thread .."
    So, do I have to create a nother thread that will call the run method of the EventListener class ?
    If yes , So how can I do that ?
    Can you please give a small a example of how doing that ?
    Thanks.
  • 722711
    722711 Member Posts: 91
    No, you don't have to create the thread. OCEP will take care of creating the thread and using that to call the run method.

    Our helloworld sample, which comes with the installation, is a good example for this. HelloworldAdapter class implements RunnableBean, where the run method generates helloworld messages in its own thread.
  • 938644
    938644 Member Posts: 50
    Thank you very much .
    It works !
    It is much simpler than I thought ..
This discussion has been closed.